Class: StackifyRubyAPM::InstrumenterHelper Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/instrumenter_helper.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

TRACETYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'TASK'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#custom_class_infoObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Lint/UselessAssignment rubocop:disable Security/Eval



12
13
14
# File 'lib/stackify_apm/instrumenter_helper.rb', line 12

def custom_class_info
  @custom_class_info
end

#custom_instrumentedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Lint/UselessAssignment rubocop:disable Security/Eval



12
13
14
# File 'lib/stackify_apm/instrumenter_helper.rb', line 12

def custom_instrumented
  @custom_instrumented
end

Class Method Details

.custom_instrumented_resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stackify_apm/instrumenter_helper.rb', line 17

def self.custom_instrumented_reset
  @custom_instrumented.each do |custom_instrument, values|
    current_class = custom_instrument
    module_consget = Module.const_get(current_class)
    values.each_key do |current_method|
      is_method_exists = StackifyRubyAPM::Spies.ismethod_exists(current_class, current_method)
      if is_method_exists
        @custom_instrumented[current_class.to_s][current_method.to_s] = false
      end
    end
  end
end

.m_class(tracked_func, current_class, class_path, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/stackify_apm/instrumenter_helper.rb', line 30

def self.m_class(tracked_func, current_class, class_path, **options)
  current_method = options[:current_method] || nil
  tracked_function_name = options[:tracked_function_name] || nil
  transaction = options[:is_transaction] || nil
  module_consget = Module.const_get(current_class)
  classspyitem = "#{current_class}Spy"
  module_consget_spy = Module.const_get(current_class)
  current_method_without = "_without_apm_#{current_method}"
  is_private_method = options[:is_private_method] || false
  is_protected_method = options[:is_protected_method] || false

  unless @custom_instrumented[current_class.to_s]
    @custom_instrumented[current_class.to_s] = {}
  end

  unless @custom_instrumented[current_class.to_s][current_method.to_s]
    @custom_instrumented[current_class.to_s][current_method.to_s] = false
  end

  unless @custom_class_info[current_class.to_s]
    @custom_class_info[current_class.to_s] = {}
  end

  unless @custom_class_info[current_class.to_s]['controller']
    @custom_class_info[current_class.to_s]['controller'] = false
  end

  unless @custom_class_info[current_class.to_s]['model']
    @custom_class_info[current_class.to_s]['model'] = false
  end

  return unless @custom_instrumented[current_class.to_s][current_method.to_s] == false

  eval <<-RUBY
    class #{classspyitem}
      def install
        #{current_class}.class_eval do
          alias_method "#{current_method_without}", "#{current_method}"
          
          #{
            if is_private_method
              then "private"
            elsif is_protected_method
              then "protected"
            end
          }
          def #{current_method}(*args, **kwargs, &block)
            if StackifyRubyAPM.current_transaction.nil? && #{!transaction.nil?}
              t = StackifyRubyAPM.transaction("custom.#{current_class}.#{current_method}", TRACETYPE)
              begin
                req = #{current_method_without}(*args, **kwargs, &block)
              rescue Exception => e
                StackifyRubyAPM.report(e)
                raise e
              ensure
                t.submit
              end
              return req
            elsif StackifyRubyAPM.current_transaction
              name = "Custom Instrument"
              type = "#{current_class}##{current_method}"
              ctx = if "#{tracked_func}" == 'true'
                      Span::Context.new(
                        CATEGORY: 'Ruby',
                        TRACKED_FUNC: "#{tracked_function_name}"
                      )
                    else
                      Span::Context.new(
                        CATEGORY: 'Ruby'
                      )
                    end

              StackifyRubyAPM.span name, type, context: ctx do
                #{current_method_without}(*args, **kwargs, &block)
              end
            else
              return #{current_method_without}(*args, **kwargs, &block)
            end
          end
        end
      end
    end
    StackifyRubyAPM::Spies.register current_class.to_s, class_path.to_s, #{classspyitem}.new, true, "#{current_method}", "#{tracked_func}", "#{tracked_function_name}"
  RUBY

  @custom_instrumented[current_class.to_s][current_method.to_s] = true
  @custom_class_info[current_class.to_s]['controller'] = true if class_path && class_path.include?('controllers')
  @custom_class_info[current_class.to_s]['model'] = true if class_path && class_path.include?('models')
end

.m_singleton(tracked_func, current_class, class_path, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/stackify_apm/instrumenter_helper.rb', line 120

def self.m_singleton(tracked_func, current_class, class_path, **options)
  current_method = options[:current_method] || nil
  tracked_function_name = options[:tracked_function_name] || nil
  transaction = options[:is_transaction] || nil
  module_consget = Module.const_get(current_class)
  classspyitem = "#{current_class}Spy"

  unless @custom_instrumented[current_class.to_s]
    @custom_instrumented[current_class.to_s] = {}
  end

  unless @custom_instrumented[current_class.to_s][current_method.to_s]
    @custom_instrumented[current_class.to_s][current_method.to_s] = false
  end

  unless @custom_class_info[current_class.to_s]
    @custom_class_info[current_class.to_s] = {}
  end

  unless @custom_class_info[current_class.to_s]['controller']
    @custom_class_info[current_class.to_s]['controller'] = false
  end

  unless @custom_class_info[current_class.to_s]['model']
    @custom_class_info[current_class.to_s]['model'] = false
  end

  return unless @custom_instrumented[current_class.to_s][current_method.to_s] == false

  eval <<-RUBY
    class #{classspyitem}
      def install
        #{current_class}.class_eval do
          singleton_class.send(:alias_method, :"_self_without_apm_#{current_method}", :"#{current_method}")

          def self.#{current_method}(*args, **kwargs, &block)
            if StackifyRubyAPM.current_transaction.nil? && #{!transaction.nil?}
              t = StackifyRubyAPM.transaction("custom.#{current_class}.#{current_method}", TRACETYPE)
              begin
                req = _self_without_apm_#{current_method}(*args, **kwargs, &block)
              rescue Exception => e
                StackifyRubyAPM.report(e)
                raise e
              ensure
                t.submit
              end
              return req
            elsif StackifyRubyAPM.current_transaction
              name = "Custom Instrument"
              type = "#{current_class}##{current_method}"
              ctx = if "#{tracked_func}" == 'true'
                      Span::Context.new(
                        CATEGORY: 'Ruby',
                        TRACKED_FUNC: "#{tracked_function_name}"
                      )
                    else
                      Span::Context.new(
                        CATEGORY: 'Ruby'
                      )
                    end

              StackifyRubyAPM.span name, type, context: ctx do
                _self_without_apm_#{current_method}(*args, **kwargs, &block)
              end
            else
              return _self_without_apm_#{current_method}(*args, **kwargs, &block)
            end
          end

          def self.get_class_name
            return self.class.name
          end
        end
      end
    end
    StackifyRubyAPM::Spies.register current_class.to_s, class_path.to_s, #{classspyitem}.new, true, "#{current_method}", "#{tracked_func}", "#{tracked_function_name}"
  RUBY
  @custom_instrumented[current_class.to_s][current_method.to_s] = true
  @custom_class_info[current_class.to_s]['controller'] = true if class_path && class_path.include?('controllers')
  @custom_class_info[current_class.to_s]['model'] = true if class_path && class_path.include?('models')
end

.patched_module(tracked_func, current_module, class_path, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Monkey patch the single ruby module file.

tracked_func - trackedFunction variable in stackify.json current_module - module variable in stackify.json class_path - file_path variable in stackify.json options - other options such as trackedFunctionName



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/stackify_apm/instrumenter_helper.rb', line 208

def self.patched_module(tracked_func, current_module, class_path, **options)
  current_method = options[:current_method] || nil
  tracked_function_name = options[:tracked_function_name] || nil
  transaction = options[:is_transaction] || nil
  mod_spy = "#{current_module}Spy"

  unless @custom_instrumented[current_module.to_s]
    @custom_instrumented[current_module.to_s] = {}
  end

  unless @custom_instrumented[current_module.to_s][current_method.to_s]
    @custom_instrumented[current_module.to_s][current_method.to_s] = false
  end

  unless @custom_class_info[current_module.to_s]
    @custom_class_info[current_module.to_s] = {}
  end

  unless @custom_class_info[current_module.to_s]['controller']
    @custom_class_info[current_module.to_s]['controller'] = false
  end

  unless @custom_class_info[current_module.to_s]['model']
    @custom_class_info[current_module.to_s]['model'] = false
  end

  return unless @custom_instrumented[current_module.to_s][current_method.to_s] == false

  eval <<-RUBY
    module #{mod_spy}
      def self.install
        #{current_module}.class_eval do
          class<< self
            alias_method "#{current_method}_without_apm", "#{current_method}"

            def #{current_method}(*args, **kwargs, &block)
              if StackifyRubyAPM.current_transaction.nil? && #{!transaction.nil?}
                t = StackifyRubyAPM.transaction("custom.#{current_module}.#{current_method}", TRACETYPE)
                begin
                  req = #{current_method}_without_apm(*args, **kwargs, &block)
                rescue Exception => e
                  StackifyRubyAPM.report(e)
                  raise e
                ensure
                  t.submit
                end
                return req
              elsif StackifyRubyAPM.current_transaction
                name = "Custom Instrument"
                type = "#{current_module}##{current_method}"
                ctx = if "#{tracked_func}" == 'true'
                        Span::Context.new(
                          CATEGORY: 'Ruby',
                          TRACKED_FUNC: "#{tracked_function_name}"
                        )
                      else
                        Span::Context.new(
                          CATEGORY: 'Ruby'
                        )
                      end

                StackifyRubyAPM.span name, type, context: ctx do
                  #{current_method}_without_apm(*args, **kwargs, &block)
                end
              else
                return #{current_method}_without_apm(*args, **kwargs, &block)
              end
            end
          end
        end
      end
    end

    StackifyRubyAPM::Spies.register current_module.to_s, class_path.to_s, #{mod_spy}, true, "#{current_method}", "#{tracked_func}", "#{tracked_function_name}"
  RUBY
  @custom_instrumented[current_module.to_s][current_method.to_s] = true
  @custom_class_info[current_module.to_s]['controller'] = true if class_path && class_path.include?('controllers')
  @custom_class_info[current_module.to_s]['model'] = true if class_path && class_path.include?('models')
end