-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: rack event baggage handling #965
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ module Middlewares | |
class EventHandler | ||
include ::Rack::Events::Abstract | ||
|
||
TOKENS_KEY = 'otel.context.tokens' | ||
TOKEN_KEY = 'otel.context.token' | ||
GOOD_HTTP_STATUSES = (100..499) | ||
|
||
# Creates a server span for this current request using the incoming parent context | ||
|
@@ -56,7 +56,9 @@ def on_start(request, _) | |
|
||
parent_context = extract_remote_context(request) | ||
span = create_span(parent_context, request) | ||
request.env[TOKENS_KEY] = register_current_span(span) | ||
span_ctx = OpenTelemetry::Trace.context_with_span(span, parent_context: parent_context) | ||
rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: span_ctx) | ||
request.env[TOKEN_KEY] = OpenTelemetry::Context.attach(rack_ctx) | ||
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
end | ||
|
@@ -108,7 +110,7 @@ def on_finish(request, response) | |
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
ensure | ||
detach_contexts(request) | ||
detach_context(request) | ||
end | ||
|
||
private | ||
|
@@ -191,11 +193,11 @@ def request_span_attributes(env) | |
attributes | ||
end | ||
|
||
def detach_contexts(request) | ||
request.env[TOKENS_KEY]&.reverse_each do |token| | ||
OpenTelemetry::Context.detach(token) | ||
OpenTelemetry::Trace.current_span.finish | ||
end | ||
def detach_context(request) | ||
return nil unless request.env[TOKEN_KEY] | ||
|
||
OpenTelemetry::Trace.current_span.finish | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lack of symmetry (with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would make sense to store a reference to the rack span in the rack request env so we can close it later, rather than relying on in the implicit With the current approach we're vulnerable to a scenario where some misconfigured or buggy instrumentation opening a span, setting the context, and not closing it... right? It's probably not likely, but still plausible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the Rack span is not the current span at this point then it means the context token is out of sync also and we will likely get a detach error. If this tracked the token and the If something went wrong with a different implicit "current" context not being return nil unless request.env[RACK_OTEL_CONTEXT_KEY]
[token, rack_ctx] = request.env[RACK_OTEL_CONTEXT_KEY]
OpenTelemetry::Trace.current_span(rack_ctx).finish
OpenTelemetry::Context.detach(token) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that makes sense to me, see 0b666b9 |
||
OpenTelemetry::Context.detach(request.env[TOKEN_KEY]) | ||
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
end | ||
|
@@ -244,15 +246,6 @@ def config | |
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.config | ||
end | ||
|
||
def register_current_span(span) | ||
ctx = OpenTelemetry::Trace.context_with_span(span) | ||
rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: ctx) | ||
|
||
contexts = [ctx, rack_ctx] | ||
contexts.compact! | ||
contexts.map { |context| OpenTelemetry::Context.attach(context) } | ||
end | ||
|
||
def create_span(parent_context, request) | ||
span = tracer.start_span( | ||
create_request_span_name(request), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to attach multiple context objects just a single context that captures:
The
*.context_with_span
calls make use of the set_value context interface which always returns a new ctx object.So instead of layering contexts, we're building a single one here that contains all the entries needed when we start a rack request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL