Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ title: "Get Current Tracer and Span"
description: "Access the active span or tracer at any point in your code to enrich it with additional attributes and context."
---

## What it is
## About

**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.
Spans and tracers are usually created at the top of a request, but the functions that need to add data to them sit deeper in the call stack. Instead of passing the span or tracer through every function argument, OpenTelemetry stores the active span in context.

## Use cases
- `trace.get_current_span()` returns the active span from anywhere so attributes, metadata, or status can be added without a direct reference.
- `trace.get_tracer()` returns a tracer for starting new child spans from helper functions, middleware, or shared libraries.

- **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.
---

## When to use

- **Enrich spans from deep in the call stack**: Add attributes to the active span from a utility function without passing the span through every caller.
- **Create tool call spans from shared code**: Get a tracer and start a new span with tool-specific attributes like function name and arguments.
- **Add context to auto-instrumented spans**: Attach extra attributes to spans created by auto-instrumentors without modifying the library code.

---

Expand Down Expand Up @@ -59,14 +64,14 @@ description: "Access the active span or tracer at any point in your code to enri

```python Python
from opentelemetry import trace
# Assuming SpanAttributes, FiSpanKindValues, ToolCallAttributes,
# Assuming FiSpanKindValues, SpanAttributes, 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,
SpanAttributes.GEN_AI_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
Expand Down Expand Up @@ -119,13 +124,13 @@ description: "Access the active span or tracer at any point in your code to enri

## 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.
- **`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
## Next Steps

<CardGroup cols={2}>
<Card title="Set Up Tracing" icon="gear" href="/docs/observe/features/manual-tracing/set-up-tracing">
Expand Down