You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/docs/observe/features/manual-tracing/get-current-span-context.mdx
+17-12Lines changed: 17 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,20 @@ title: "Get Current Tracer and Span"
3
3
description: "Access the active span or tracer at any point in your code to enrich it with additional attributes and context."
4
4
---
5
5
6
-
## What it is
6
+
## About
7
7
8
-
**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.
8
+
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.
9
9
10
-
## Use cases
10
+
-`trace.get_current_span()` returns the active span from anywhere so attributes, metadata, or status can be added without a direct reference.
11
+
-`trace.get_tracer()` returns a tracer for starting new child spans from helper functions, middleware, or shared libraries.
11
12
12
-
-**Enrich spans mid-execution** — Add attributes to the current span from inside a helper function without passing the span as a parameter.
13
-
-**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.
14
-
-**Non-invasive instrumentation** — Add context to spans created by auto-instrumentors without modifying the instrumented code.
13
+
---
14
+
15
+
## When to use
16
+
17
+
-**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.
18
+
-**Create tool call spans from shared code**: Get a tracer and start a new span with tool-specific attributes like function name and arguments.
19
+
-**Add context to auto-instrumented spans**: Attach extra attributes to spans created by auto-instrumentors without modifying the library code.
15
20
16
21
---
17
22
@@ -59,14 +64,14 @@ description: "Access the active span or tracer at any point in your code to enri
@@ -119,13 +124,13 @@ description: "Access the active span or tracer at any point in your code to enri
119
124
120
125
## Key concepts
121
126
122
-
-**`trace.get_current_span()`** — Returns the span that is currently active in the context. If no span is active, returns a no-op span.
123
-
-**`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.
124
-
-**`trace.getSpan(context.active())`** — JS/TS equivalent of `get_current_span()`. Returns `undefined` if no span is active, so always check before setting attributes.
127
+
-**`trace.get_current_span()`**: Returns the span that is currently active in the context. If no span is active, returns a no-op span.
128
+
-**`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.
129
+
-**`trace.getSpan(context.active())`**: JS/TS equivalent of `get_current_span()`. Returns `undefined` if no span is active, so always check before setting attributes.
125
130
126
131
---
127
132
128
-
## What you can do next
133
+
## Next Steps
129
134
130
135
<CardGroupcols={2}>
131
136
<Cardtitle="Set Up Tracing"icon="gear"href="/docs/observe/features/manual-tracing/set-up-tracing">
0 commit comments