0

I am trying to trace the execution of a task in Celery through the Publisher -> Queue -> Consumer lifecycle. I am using OpenTelemetry 1.16.0 in Python 3.9.15 in order to instrument the application. I already know how to propagate a trace through the Queue as described in the answers to this stackoverflow question.

OpenTelemetry Python - How to instanciate a new span as a child span for a given trace_id

In addition to propagating the trace through the Queue I want to create a span in the trace for the time spent on the Queue. Right now, that period of time just appears as a gap in the trace as viewed in Grafana Tempo. I should be able to manually create this span assuming that I have trace id, parent span id, timestamp for when task is added to Queue, and timestamp for when task is taken from Queue.

Matthew Thomas
  • 594
  • 7
  • 13

1 Answers1

1

You use start_span to create your own spans. It accepts start_time (or uses current if not provided). Like wise, there is an end to end the span which takes the end_time to indicate when span ended.

It would look like this

import time

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
)
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    BatchExportSpanProcessor(ConsoleSpanExporter())
)



tracer = trace.get_tracer(__name__)

span = tracer.start_span("foo", start_time=...)

...

span.end(end_time=...)
Srikanth Chekuri
  • 1,944
  • 1
  • 9
  • 19