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.
Copy
from opentelemetry import tracecurrent_span = trace.get_current_span()# enrich 'current_span' with some informationcurrent_span.set_attribute("example.attribute1", "value1")current_span.set_attribute("example.attribute2", 123)current_span.set_attribute("example.attribute3", True)
Let’s explore how to work with tracers - the core components for span creation in OpenTelemetry.
Copy
from opentelemetry import trace# Assuming SpanAttributes, FiSpanKindValues, ToolCallAttributes,# function_call_name, and arguments variables are defined externally.tracer = trace.get_tracer(__name__)# Start a new span for the tool function handlingwith 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}) as span: pass