Module: Brut::Instrumentation::Methods::ClassMethods

Defined in:
lib/brut/instrumentation/methods.rb

Instance Method Summary collapse

Instance Method Details

#__module_for_instrumented_methodsObject



51
52
53
54
55
56
57
# File 'lib/brut/instrumentation/methods.rb', line 51

def __module_for_instrumented_methods
  @__module_for_instrumented_methods ||= begin
    mod = Module.new
    prepend mod
    mod
  end
end

#instrument(*method_names) ⇒ Object

Instrument methods that have already been defined.

Parameters:

  • method_names (Array<Symbol>)

    the method names to instrument. They must exist and they must not already have been instrumented.

Raises:

  • (ArgumentError)

    if any method does not exist or has already been instrumented



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
# File 'lib/brut/instrumentation/methods.rb', line 64

def instrument(*method_names)

  method_names.each do |method_name|
    if __module_for_instrumented_methods.method_defined?(method_name)
      raise ArgumentError, "Method #{method_name} is already instrumented in #{self}"
    end

    method_defined = method_defined?(method_name) ||
                     private_method_defined?(method_name) ||
                     protected_method_defined?(method_name)

    if !method_defined
      raise ArgumentError, "Method #{method_name} is not defined in #{self.name}"
    end

    visibility = if private_method_defined?(method_name)
                   :private
                 elsif protected_method_defined?(method_name)
                   :protected
                 else
                   :public
                 end

    __module_for_instrumented_methods.module_eval do
      define_method(method_name) do |*args, **kwargs, &blk|
        span_name = "#{self.class.name}##{method_name}"
        span_attrs = {
          "brut.class" => self.class.name,
          "brut.method"  => method_name.to_s,
        }

        Brut.container.instrumentation.span(span_name, attributes: span_attrs) do |span|
          super(*args, **kwargs, &blk)
        end
      end

      # match original visibility
      send(visibility, method_name)
    end
  end
  nil
end

#instrument_allObject

Instrument all methods, public, protected, and private, other than initialize. This will also instrument any methods defined in this class after it's called, meaning you can it at the top of the class. Do note that this will not instrument any methods brought in via modules.



111
112
113
114
115
116
117
118
# File 'lib/brut/instrumentation/methods.rb', line 111

def instrument_all
  @__instrument_all = true
  method_names = instance_methods(false) + private_instance_methods(false) - [ :initialize ]

  instrument(*method_names)

  nil
end

#instrument_publicObject

Instrument all public and protected methods, other than initialize. This will also instrument any public or protected methods defined in this class after it's called, meaning you can it at the top of the class. Do note that this will not instrument any methods brought in via modules.



124
125
126
127
128
129
130
131
# File 'lib/brut/instrumentation/methods.rb', line 124

def instrument_public
  @__instrument_public = true
  method_names = instance_methods(false) - [ :initialize ]

  instrument(*method_names)

  nil
end

#method_added(meth) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/brut/instrumentation/methods.rb', line 133

def method_added(meth)
  if !@__instrument_all && !@__instrument_public
    return
  end
  if @__adding__
    return
  end
  if meth == :initialize
    return
  end
  if !@__instrument_all && private_instance_methods(false).include?(meth)
    return
  end

  @__adding__ = true
  instrument(meth)
  @__adding__ = false
end