Get Current Tracer and Span

Access the active span or tracer at any point in your code to enrich it with additional attributes and context.

What it is

Get Current Tracer and Span lets you access the active OpenTelemetry span or tracer at any point in your code — without passing them explicitly through your call stack. You use trace.get_current_span() to grab whatever span is active and add attributes to it, or trace.get_tracer() to get a tracer for creating new spans with custom attributes like span kind, tool call metadata, and input values.

Use cases

  • Enrich spans mid-execution — Add attributes to the current span from inside a helper function without passing the span as a parameter.
  • Tool call tracing — Get a tracer and start a span with tool-specific attributes (fi.span.kind, tool.call.function.name, arguments) for structured tool call visibility.
  • Non-invasive instrumentation — Add context to spans created by auto-instrumentors without modifying the instrumented code.

How to

Access the active span or tracer

Choose whether to grab the currently active span or get a tracer to create new spans.

Access the active span and add attributes to it at any point in your code.

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)
import { trace, context } from "@opentelemetry/api";

const currentSpan = trace.getSpan(context.active());

if (currentSpan) {
    currentSpan.setAttribute("example.attribute1", "value1");
    currentSpan.setAttribute("example.attribute2", 123);
    currentSpan.setAttribute("example.attribute3", true);
}

Get a tracer and use it to start spans with custom attributes.

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 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
}) as span:
    pass
const { trace, context, SpanStatusCode } = require("@opentelemetry/api");
const { AsyncLocalStorageContextManager } = require("@opentelemetry/context-async-hooks");
const { register } = require("@traceai/fi-core");
const { ProjectType } = require("@traceai/fi-core");
const { registerInstrumentations } = require("@opentelemetry/instrumentation");

const tracerProvider = register({
    projectName: "manual-instrumentation-example",
    projectType: ProjectType.OBSERVE,
    sessionName: "manual-instrumentation-example-session"
});

const tracer = tracerProvider.getTracer("manual-instrumentation-example");

tracer.startActiveSpan("HandleFunctionCall", {
    attributes: {
        "fi.span.kind": "tool",
        "tool.call.function.name": functionCallName,
        "tool.call.function.arguments_json": JSON.stringify(receivedArguments),
        "input.value": functionCallName
    }
}, (span) => {
    try {
        span.setStatus({ code: SpanStatusCode.OK });
    } catch (error) {
        span.recordException(error);
        span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
        throw error;
    } finally {
        span.end();
    }
});

Key concepts

  • trace.get_current_span() — Returns the span that is currently active in the context. If no span is active, returns a no-op span.
  • trace.get_tracer(__name__) — Returns a tracer scoped to the current module. Use this to create new spans anywhere without a reference to the tracer provider.
  • trace.getSpan(context.active()) — JS/TS equivalent of get_current_span(). Returns undefined if no span is active, so always check before setting attributes.

What you can do next

Was this page helpful?

Questions & Discussion