Sometimes it’s helpful to access whatever the current span is at a point in time so that you can enrich it with more data.

from opentelemetry import trace

current_span = trace.get_current_span()

# enrich 'current_span' with some information
current_span.set_attribute("example.attribute1", "value1")
current_span.set_attribute("example.attribute2", 123)
current_span.set_attribute("example.attribute3", True)

Get Current Tracer

Let’s explore how to work with tracers - the core components for span creation in OpenTelemetry.

# The following is used to get the tracer:
tracer = trace.get_tracer(__name__)

# Start a new span for the tool function handling
with tracer.start_as_current_span("HandleFunctionCall", attributes={
    SpanAttributes.FI_SPAN_KIND: FiSpanKindValues.TOOL.value,
    ToolCallAttributes.TOOL_CALL_FUNCTION_NAME: function_call_name,
    ToolCallAttributes.TOOL_CALL_FUNCTION_ARGUMENTS_JSON: str(arguments),
    SpanAttributes.INPUT_VALUE: function_call_name
})