Class: OpenTelemetry::Instrumentation::Rack::Middlewares::EventHandler
- Inherits:
-
Object
- Object
- OpenTelemetry::Instrumentation::Rack::Middlewares::EventHandler
- Includes:
- Rack::Events::Abstract
- Defined in:
- lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb
Overview
OTel Rack Event Handler
This seeds the root context for this service with the server span as the current_span
allowing for callers later in the stack to reference it using Trace.current_span
It also registers the server span in a context dedicated to this instrumentation that users may look up using OpenTelemetry::Instrumentation::Rack.current_span, which makes it possible for users to mutate the span, e.g. add events or update the span name like in the ActionPack instrumentation.
Constant Summary collapse
- TOKENS_KEY =
'otel.context.tokens'
- GOOD_HTTP_STATUSES =
(100..499)
Instance Method Summary collapse
-
#on_commit(request, response) ⇒ void
Optionally adds debugging response headers injected from #response_propagators.
-
#on_error(request, _, error) ⇒ Object
Records Unexpected Exceptions on the Rack span and set the Span Status to Error.
-
#on_finish(request, response) ⇒ Object
Finishes the span making it eligible to be exported and cleans up existing contexts.
-
#on_start(request, _) ⇒ void
Creates a server span for this current request using the incoming parent context and registers them as the current_span.
Instance Method Details
#on_commit(request, response) ⇒ void
This method returns an undefined value.
Optionally adds debugging response headers injected from #response_propagators
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb', line 69 def on_commit(request, response) span = OpenTelemetry::Instrumentation::Rack.current_span return unless span.recording? response_propagators&.each do |propagator| propagator.inject(response.headers) rescue StandardError => e OpenTelemetry.handle_error(message: 'Unable to inject response propagation headers', exception: e) end rescue StandardError => e OpenTelemetry.handle_error(exception: e) end |
#on_error(request, _, error) ⇒ Object
does nothing if the span is a non-recording span
Records Unexpected Exceptions on the Rack span and set the Span Status to Error
88 89 90 91 92 93 94 95 96 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb', line 88 def on_error(request, _, error) span = OpenTelemetry::Instrumentation::Rack.current_span return unless span.recording? span.record_exception(error) span.status = OpenTelemetry::Trace::Status.error rescue StandardError => e OpenTelemetry.handle_error(exception: e) end |
#on_finish(request, response) ⇒ Object
does nothing if the span is a non-recording span
Finishes the span making it eligible to be exported and cleans up existing contexts
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb', line 103 def on_finish(request, response) span = OpenTelemetry::Instrumentation::Rack.current_span return unless span.recording? add_response_attributes(span, response) if response rescue StandardError => e OpenTelemetry.handle_error(exception: e) ensure detach_contexts(request) end |
#on_start(request, _) ⇒ void
This method returns an undefined value.
Creates a server span for this current request using the incoming parent context and registers them as the current_span
54 55 56 57 58 59 60 61 62 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/event_handler.rb', line 54 def on_start(request, _) return if untraced_request?(request.env) parent_context = extract_remote_context(request) span = create_span(parent_context, request) request.env[TOKENS_KEY] = register_current_span(span) rescue StandardError => e OpenTelemetry.handle_error(exception: e) end |