Module: Daytona::Instrumentation::ClassMethods

Defined in:
lib/daytona/otel.rb

Instance Method Summary collapse

Instance Method Details

#instrument(*method_names, component:) ⇒ Object

Instruments the listed methods with OTel tracing/metrics. Must be called after all target methods are defined.

Parameters:

  • method_names (Array<Symbol>)

    methods to instrument

  • component (String)

    component name for span/metric attributes



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
# File 'lib/daytona/otel.rb', line 152

def instrument(*method_names, component:) # rubocop:disable Metrics/MethodLength
  method_names.each do |method_name|
    original = instance_method(method_name)

    # Detect original visibility
    visibility = if private_method_defined?(method_name, false)
                   :private
                 elsif protected_method_defined?(method_name, false)
                   :protected
                 else
                   :public
                 end

    define_method(method_name) do |*args, **kwargs, &blk|
      ::Daytona.with_instrumentation(otel_state, component, method_name.to_s) do
        original.bind_call(self, *args, **kwargs, &blk)
      end
    end

    # Restore visibility
    case visibility
    when :private   then private method_name
    when :protected then protected method_name
    end
  end
end