Hi I have a question about manual instrument. I'm using auto instrument by default and I want to insert RETRIEVAL span and need to get a parent id from somewhere. Can I have a sample code for that somewhere?
Sean L. We have used another approach as well to instrumentation if the "with clause instrumentation" was not as easy to use in a certain location.
import datetime
from opentelemetry import trace
from opentelemetry.trace import set_span_in_context, SpanKind as OtelSpanKind
def create_span(tracer, parent_span, span_name, the_id):
# Set the parent span in the current context
parent_context = set_span_in_context(parent_span)
# Construct the unique span name
unique_span_name = (
span_name + "_" + the_id[-10:] + "-" + datetime.datetime.now().strftime("%m-%d_%H-%M:%S")
)
# Start a new child span
child_span = tracer.start_span(
unique_span_name,
kind=OtelSpanKind.INTERNAL,
context=parent_context, # Explicitly set the parent context
)
# Use the child span as needed...
return child_span
# Example usage:
tracer = trace.get_tracer(__name__)
parent_span = tracer.start_span("parent_span_name")
child_span = create_span(tracer, parent_span, "child_span_name", "example_id_1234567890")