Skip to content

Commit 809a25c

Browse files
authored
Merge pull request #557 from future-agi/fix/rewrite-tracing-enriching-spans
rewrite manual tracing enriching spans
2 parents 5579dfb + 8dbf549 commit 809a25c

1 file changed

Lines changed: 31 additions & 29 deletions

File tree

src/pages/docs/observe/features/manual-tracing/add-attributes-metadata-tags.mdx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ title: "Enriching Spans with Attributes, Metadata, and Tags"
33
description: "Capture additional context beyond what standard frameworks provide by enriching your traces with custom attributes, metadata, tags, session IDs, user IDs, and prompt templates."
44
---
55

6-
## What it is
6+
## About
77

8-
**Enriching Spans** lets you attach custom key/value pairs and structured context to your OpenTelemetry spans so they carry the full picture of what's happening in your application. You can add raw attributes with `set_attribute()`, use traceAI Semantic Convention constants for structured LLM data, or use context managers (`using_metadata`, `using_tags`, `using_session`, `using_user`, `using_prompt_template`) to propagate attributes automatically to all child spans — without modifying every instrumented function.
8+
A trace with only timing and status tells what happened, but not why. Without attributes like experiment IDs, feature flags, or prompt versions, filtering and debugging in the dashboard requires guesswork. Enriching spans attaches this application-specific context directly to traces so they become searchable, filterable, and meaningful. There are three ways to do it: add key/value pairs directly with `set_attribute()`, use traceAI Semantic Convention constants for structured LLM data, or use context managers (`using_metadata`, `using_tags`, `using_session`, `using_user`, `using_prompt_template`) to propagate attributes automatically to all child spans in a block.
99

10-
## Use cases
10+
---
11+
12+
## When to use
1113

12-
- **Custom attributes** — Add business-specific key/value pairs to spans for filtering and debugging in the Future AGI dashboard.
13-
- **Semantic conventions**Use traceAI constants like `OUTPUT_VALUE` and `LLM_OUTPUT_MESSAGES` to capture LLM outputs in a structured, queryable schema.
14-
- **Metadata enrichment**Attach experiment metadata, feature flags, or A/B test identifiers to all spans in a code block.
15-
- **Session and user tracking** Associate spans with a session ID and user ID for session replay and per-user analytics.
16-
- **Prompt template tracking** Record which prompt template, version, and variables were used in each LLM call.
14+
- **Custom attributes for filtering**: Attach business-specific key/value pairs to spans so they can be filtered and searched in the dashboard.
15+
- **Structured LLM outputs**: Use traceAI constants like `OUTPUT_VALUE` and `LLM_OUTPUT_MESSAGES` to capture LLM responses in a queryable schema.
16+
- **Experiment and A/B test tracking**: Attach metadata like experiment IDs or feature flags to all spans in a code block.
17+
- **Session and user grouping**: Associate spans with a session ID and user ID for session replay and per-user analytics.
18+
- **Prompt template versioning**: Record which prompt template, version, and variables were used in each LLM call.
1719

1820
---
1921

@@ -35,7 +37,7 @@ description: "Capture additional context beyond what standard frameworks provide
3537
current_span.set_attribute("operation.other-stuff", [1, 2])
3638
```
3739

38-
```typescript JS/TS
40+
```javascript JS/TS
3941
import { trace, context } from "@opentelemetry/api";
4042

4143
const currentSpan = trace.getSpan(context.active());
@@ -55,11 +57,11 @@ description: "Capture additional context beyond what standard frameworks provide
5557

5658
<CodeGroup>
5759

58-
```bash Python
60+
```python Python
5961
pip install fi-instrumentation-otel
6062
```
6163

62-
```bash JS/TS
64+
```javascript JS/TS
6365
npm install @traceai/fi-core @opentelemetry/api
6466
```
6567

@@ -80,16 +82,16 @@ description: "Capture additional context beyond what standard frameworks provide
8082

8183
# This shows up under `output_messages` tab on the span page
8284
span.set_attribute(
83-
f"{SpanAttributes.LLM_OUTPUT_MESSAGES}.0.{MessageAttributes.MESSAGE_ROLE}",
85+
f"{SpanAttributes.GEN_AI_OUTPUT_MESSAGES}.0.{MessageAttributes.MESSAGE_ROLE}",
8486
"user",
8587
)
8688
span.set_attribute(
87-
f"{SpanAttributes.LLM_OUTPUT_MESSAGES}.0.{MessageAttributes.MESSAGE_CONTENT}",
89+
f"{SpanAttributes.GEN_AI_OUTPUT_MESSAGES}.0.{MessageAttributes.MESSAGE_CONTENT}",
8890
response,
8991
)
9092
```
9193

92-
```typescript JS/TS
94+
```javascript JS/TS
9395
import { trace, context } from "@opentelemetry/api";
9496

9597
// Assume 'response' variable is defined, e.g.:
@@ -133,7 +135,7 @@ description: "Capture additional context beyond what standard frameworks provide
133135
pass # Your code here
134136
```
135137

136-
```typescript JS/TS
138+
```javascript JS/TS
137139
import { context, propagation } from "@opentelemetry/api";
138140

139141
// Assuming value_1, value_2 are defined
@@ -189,7 +191,7 @@ description: "Capture additional context beyond what standard frameworks provide
189191
pass # Your code here
190192
```
191193

192-
```typescript JS/TS
194+
```javascript JS/TS
193195
import { context, propagation } from "@opentelemetry/api";
194196

195197
// Assuming tags list is defined, e.g.:
@@ -241,7 +243,7 @@ description: "Capture additional context beyond what standard frameworks provide
241243
pass # Your code here
242244
```
243245

244-
```typescript JS/TS
246+
```javascript JS/TS
245247
import { context, propagation } from "@opentelemetry/api";
246248

247249
// Assuming session_id is defined, e.g.:
@@ -293,7 +295,7 @@ description: "Capture additional context beyond what standard frameworks provide
293295
pass # Your code here
294296
```
295297

296-
```typescript JS/TS
298+
```javascript JS/TS
297299
import { context, propagation } from "@opentelemetry/api";
298300

299301
// Assuming user_id is defined, e.g.:
@@ -353,7 +355,7 @@ description: "Capture additional context beyond what standard frameworks provide
353355
pass # Your code here
354356
```
355357

356-
```typescript JS/TS
358+
```javascript JS/TS
357359
import { context, propagation } from "@opentelemetry/api";
358360

359361
// Assuming template, version, and variables are defined, e.g.:
@@ -425,7 +427,7 @@ description: "Capture additional context beyond what standard frameworks provide
425427
pass # Your code here
426428
```
427429

428-
```typescript JS/TS
430+
```javascript JS/TS
429431
import { context, propagation } from "@opentelemetry/api";
430432

431433
const metadata = {"experiment": "A/B test", "version": "2.1"};
@@ -460,18 +462,18 @@ description: "Capture additional context beyond what standard frameworks provide
460462

461463
## Key concepts
462464

463-
- **`set_attribute()`**Attaches a key/value pair directly to the active span. Supports strings, numbers, and booleans. Prefix custom attributes with your company name to avoid naming conflicts.
464-
- **Semantic Conventions**Structured attribute names defined by traceAI for common LLM data (messages, prompt templates, token counts). Use `SpanAttributes` and `MessageAttributes` constants from `fi_instrumentation.fi_types`.
465-
- **Context attributes (Baggage)**Set at the OpenTelemetry context level so they propagate automatically to all child spans within the block, without modifying instrumented functions.
466-
- **`using_metadata`**Attaches a JSON-serialized metadata dictionary to all spans in the context as the `metadata` attribute.
467-
- **`using_tags`**Attaches a JSON-serialized list of tag strings to all spans as `tag.tags`.
468-
- **`using_session`**Sets `session.id` on all spans in the context for session grouping.
469-
- **`using_user`**Sets `user.id` on all spans in the context for per-user tracking.
470-
- **`using_prompt_template`**Sets `llm.prompt_template.template`, `llm.prompt_template.version`, and `llm.prompt_template.variables` on all spans in the context.
465+
- **`set_attribute()`**:Attaches a key/value pair directly to the active span. Supports strings, numbers, and booleans. Prefix custom attributes with your company name to avoid naming conflicts.
466+
- **Semantic Conventions**:Structured attribute names defined by traceAI for common LLM data (messages, prompt templates, token counts). Use `SpanAttributes` and `MessageAttributes` constants from `fi_instrumentation.fi_types`.
467+
- **Context attributes (Baggage)**:Set at the OpenTelemetry context level so they propagate automatically to all child spans within the block, without modifying instrumented functions.
468+
- **`using_metadata`**:Attaches a JSON-serialized metadata dictionary to all spans in the context as the `metadata` attribute.
469+
- **`using_tags`**:Attaches a JSON-serialized list of tag strings to all spans as `tag.tags`.
470+
- **`using_session`**:Sets `session.id` on all spans in the context for session grouping.
471+
- **`using_user`**:Sets `user.id` on all spans in the context for per-user tracking.
472+
- **`using_prompt_template`**:Sets `llm.prompt_template.template`, `llm.prompt_template.version`, and `llm.prompt_template.variables` on all spans in the context.
471473

472474
---
473475

474-
## What you can do next
476+
## Next Steps
475477

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

0 commit comments

Comments
 (0)