from kafka import KafkaProducer
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.instrumentation.kafka import KafkaInstrumentor
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
resource = Resource(
attributes={SERVICE_NAME: "sender"}
)
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)
KafkaInstrumentor().instrument()
tracer = trace.get_tracer(__name__)
producer = KafkaProducer()
with tracer.start_as_current_span("kafka.sender") as span:
producer.send("my_topic", b"send message1") # send message
print(f"current traceId: {trace.get_current_span().get_span_context().trace_id}") # current traceId: 64964881610839088643312061818953950807
producer.close()
consumer code:
from kafka import KafkaConsumer
from opentelemetry import trace
from opentelemetry.instrumentation.kafka import KafkaInstrumentor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from opentelemetry.sdk.trace import TracerProvider
resource = Resource(
attributes={SERVICE_NAME: "consumer"}
)
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)
KafkaInstrumentor().instrument()
kafka_consumer = KafkaConsumer("my_topic", group_id="my_group_id")
for message in kafka_consumer:
print(f"traceId: {trace.get_current_span().get_span_context().trace_id}") # traceId: 0 The expected result is 64964881610839088643312061818953950807
What is the expected behavior?
The traceId of the expected consumption to the message acquisition is 64964881610839088643312061818953950807
What is the actual behavior?
The actual traceId is 0
Describe your environment python 3.11 Kafka-python 2.0.2 opentelemetry-instrumentation-kafka-python 0.37b0 opentelemetry-api 1.16.0 opentelemetry-sdk 1.16.0
Steps to reproduce producer code:
consumer code:
What is the expected behavior? The traceId of the expected consumption to the message acquisition is 64964881610839088643312061818953950807
What is the actual behavior? The actual traceId is 0
Additional context Please note: kafka consumer headers traceparent exists