Skip to content

Commit

Permalink
feat: support context propagation through links
Browse files Browse the repository at this point in the history
  • Loading branch information
gwuah committed Jan 29, 2024
1 parent f5f21c6 commit a1373da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :record_frontend_span, default: false, validate: :boolean
option :untraced_endpoints, default: [], validate: :array
option :url_quantization, default: nil, validate: :callable
option :propagate_with_link, default: nil, validate: :callable
option :untraced_requests, default: nil, validate: :callable
option :response_propagators, default: [], validate: :array
# This option is only valid for applications using Rack 2.0 or greater
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ def on_start(request, _)
return if untraced_request?(request.env)

parent_context = extract_remote_context(request)
span = create_span(parent_context, request)
links = nil

fn = propagate_with_link
if fn && fn.call(request.env)
links = prepare_span_links(parent_context)
parent_context = OpenTelemetry::Context.empty
end

span = create_span(parent_context, request, links)
request.env[TOKENS_KEY] = register_current_span(span)
rescue StandardError => e
OpenTelemetry.handle_error(exception: e)
Expand Down Expand Up @@ -224,6 +232,10 @@ def url_quantization
config[:url_quantization]
end

def propagate_with_link
config[:propagate_with_link]
end

def response_propagators
config[:response_propagators]
end
Expand Down Expand Up @@ -253,17 +265,23 @@ def register_current_span(span)
contexts.map { |context| OpenTelemetry::Context.attach(context) }
end

def create_span(parent_context, request)
def create_span(parent_context, request, links)
span = tracer.start_span(
create_request_span_name(request),
with_parent: parent_context,
kind: :server,
attributes: request_span_attributes(request.env)
attributes: request_span_attributes(request.env),
links: links,
)
request_start_time = OpenTelemetry::Instrumentation::Rack::Util::QueueTime.get_request_start(request.env)
span.add_event('http.proxy.request.started', timestamp: request_start_time) unless request_start_time.nil?
span
end

def prepare_span_links(ctx)
span_context = OpenTelemetry::Trace.current_span(ctx).context
span_context.valid? ? [OpenTelemetry::Trace::Link.new(span_context)] : []
end
end
end
end
Expand Down

0 comments on commit a1373da

Please sign in to comment.