lib/opentelemetry/instrumentation/ruby_kafka/patches/producer.rb in opentelemetry-instrumentation-ruby_kafka-0.20.1 vs lib/opentelemetry/instrumentation/ruby_kafka/patches/producer.rb in opentelemetry-instrumentation-ruby_kafka-0.20.2
- old
+ new
@@ -15,12 +15,18 @@
'messaging.system' => 'kafka',
'messaging.destination' => topic,
'messaging.destination_kind' => 'topic'
}
- tracer.in_span("#{topic} send", attributes: attributes, kind: :producer) do
- OpenTelemetry.propagation.inject(headers)
- super
+ # If trace context is present in headers, extract and use it as parent. If there is _no_ trace context key
+ # in the headers, OpenTelemetry.propagation.extract will return an unmodified copy of the the current
+ # Thread's context, so the next two lines preserve the correct Thread-local context.
+ ctx = OpenTelemetry.propagation.extract(headers)
+ OpenTelemetry::Context.with_current(ctx) do
+ tracer.in_span("#{topic} send", attributes: attributes, kind: :producer) do
+ OpenTelemetry.propagation.inject(headers)
+ super
+ end
end
end
private