Skip to main content
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)
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 Current Tracer

Let’s explore how to work with tracers - the core components for span creation in OpenTelemetry.
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();
    }
});