Class: RailsOtelContext::CallContextProcessor

Inherits:
Object
  • Object
show all
Includes:
SourceLocation
Defined in:
lib/rails_otel_context/call_context_processor.rb

Overview

SpanProcessor that enriches all spans with the calling Ruby class and method name, and optionally with user-defined custom attributes.

Sets span attributes (unless the call stack yields no app-code frame):

- code.namespace  – the class name, e.g. "OrderService", "InvoiceJob"
- code.function   – the method name, e.g. "create", "perform"
- code.filepath   – app-relative source file
- code.lineno     – source line number

Three-tier call-context resolution (fastest to slowest):

1. Pushed frame  — O(1) thread-local read. Set by Railtie around_action for
                   controllers, or manually via RailsOtelContext.with_frame.
2. Stack walk    — O(stack depth). Falls back here when no frame is pushed.
                   DB adapters (Trilogy, PG, MySQL2) additionally overwrite
                   code.* attributes post-query from a shallower stack position,
                   giving the exact call site (e.g. UserRepository#find_active:23).

Custom attributes (configured via custom_span_attributes) are applied to every span. The callable must return a Hash (or nil) and must be fast — it runs in the hot path of every span creation. Exceptions in the callable are silently rescued to avoid disrupting application request processing.

Constant Summary collapse

SPAN_CONTROLLER_ATTR =
'request.controller'
SPAN_ACTION_ATTR =
'request.action'
AR_MODEL_ATTR =
'code.activerecord.model'
AR_METHOD_ATTR =
'code.activerecord.method'
AR_SCOPE_ATTR =
'code.activerecord.scope'
AR_QUERY_COUNT_ATTR =
'db.query_count'
AR_ASYNC_ATTR =
'db.async'
ORIG_NAME_ATTR =
'l9.orig.name'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SourceLocation

#apply_call_site_to_span, #apply_source_to_span, #call_site_for_app, #source_location_for_app

Constructor Details

#initialize(app_root:, config: RailsOtelContext.configuration) ⇒ CallContextProcessor

Returns a new instance of CallContextProcessor.



40
41
42
43
44
45
# File 'lib/rails_otel_context/call_context_processor.rb', line 40

def initialize(app_root:, config: RailsOtelContext.configuration)
  @app_root = app_root.to_s
  @request_context_enabled = config.request_context_enabled
  @custom_span_attributes = config.custom_span_attributes
  @span_name_formatter = config.span_name_formatter
end

Instance Attribute Details

#app_rootObject (readonly)

Exposed so SourceLocation mixin can use it for the stack-walk path.



38
39
40
# File 'lib/rails_otel_context/call_context_processor.rb', line 38

def app_root
  @app_root
end

Instance Method Details

#force_flush(timeout: nil) ⇒ Object



56
# File 'lib/rails_otel_context/call_context_processor.rb', line 56

def force_flush(timeout: nil); end

#on_finish(_span) ⇒ Object



54
# File 'lib/rails_otel_context/call_context_processor.rb', line 54

def on_finish(_span); end

#on_start(span, _parent_context) ⇒ Object



47
48
49
50
51
52
# File 'lib/rails_otel_context/call_context_processor.rb', line 47

def on_start(span, _parent_context)
  apply_call_context(span)
  apply_request_context(span) if @request_context_enabled
  apply_db_context(span)
  apply_custom_attributes(span) if @custom_span_attributes
end

#shutdown(timeout: nil) ⇒ Object



58
# File 'lib/rails_otel_context/call_context_processor.rb', line 58

def shutdown(timeout: nil); end