open-telemetry / opentelemetry-python-contrib

OpenTelemetry instrumentation for Python modules
https://opentelemetry.io
Apache License 2.0
740 stars 615 forks source link

opentelemetry-instrumentation-kafka-python question, Kafka consumer cannot get traceId #1732

Open ronalgo7 opened 1 year ago

ronalgo7 commented 1 year ago

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:

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

Additional context Please note: kafka consumer headers traceparent exists

Lexas228 commented 11 months ago

@ronalgo7 Hi did u found any solution? Maybe some workaround?

sawogus29 commented 1 month ago

I have same problem! The consumer creates span (I can see kafka consume span on jaeger), but then trace ends. So there's no trace available after consume.

Is there anyone who can fix this bug? Please 🙏