forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add traceloop translator #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
91pavan
wants to merge
6
commits into
genai-utils-e2e-dev
Choose a base branch
from
feat/custom-translator-processor
base: genai-utils-e2e-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+518
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2326ccd
feat: Add traceloop translator
91pavan 45df77a
feat: update examples
91pavan d44f959
feat: updates
91pavan 471ed5b
feat: update README file name
91pavan 10578ca
feat: rewrite, switch to emitters
91pavan 256a759
feat: update examples
91pavan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
util/opentelemetry-util-genai-dev/README.traceloop_translator.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Traceloop -> GenAI Semantic Convention Translator Emitter | ||
|
||
This optional emitter promotes legacy `traceloop.*` attributes attached to an `LLMInvocation` into | ||
Semantic Convention (or forward-looking custom `gen_ai.*`) attributes **before** the standard | ||
Semantic Convention span emitter runs. It does **not** create its own span. | ||
|
||
## Why Use It? | ||
If you have upstream code (or the Traceloop compat emitter) producing `traceloop.*` keys but you | ||
want downstream dashboards/tools to rely on GenAI semantic conventions, enabling this translator | ||
lets you transition without rewriting upstream code immediately. | ||
|
||
## What It Does | ||
At `on_start` of an `LLMInvocation` it scans `invocation.attributes` for keys beginning with | ||
`traceloop.` and (non-destructively) adds corresponding keys: | ||
|
||
| Traceloop Key (prefixed or raw) | Added Key | Notes | | ||
|---------------------------------|---------------------------|-------| | ||
| `traceloop.workflow.name` / `workflow.name` | `gen_ai.workflow.name` | Custom (not yet in spec) | | ||
| `traceloop.entity.name` / `entity.name` | `gen_ai.agent.name` | Approximates entity as agent name | | ||
| `traceloop.entity.path` / `entity.path` | `gen_ai.workflow.path` | Custom placeholder | | ||
| `traceloop.callback.name` / `callback.name` | `gen_ai.callback.name` | Also sets `gen_ai.operation.source` if absent | | ||
| `traceloop.callback.id` / `callback.id` | `gen_ai.callback.id` | Custom | | ||
| `traceloop.entity.input` / `entity.input` | `gen_ai.input.messages` | Serialized form already present | | ||
| `traceloop.entity.output` / `entity.output` | `gen_ai.output.messages`| Serialized form already present | | ||
|
||
Existing `gen_ai.*` keys are never overwritten. | ||
|
||
## Enabling | ||
Fast path (no entry point needed): | ||
|
||
```bash | ||
export OTEL_GENAI_ENABLE_TRACELOOP_TRANSLATOR=1 | ||
export OTEL_INSTRUMENTATION_GENAI_EMITTERS=span,traceloop_compat | ||
|
||
Optional (remove original traceloop.* after promotion): | ||
export OTEL_GENAI_TRACELOOP_TRANSLATOR_STRIP_LEGACY=1 | ||
``` | ||
|
||
The flag auto-prepends the translator before the semantic span emitter. You can still add | ||
`traceloop_translator` explicitly once an entry point is created. | ||
|
||
You can also load this emitter the same way as other extra emitters. There are two common patterns: | ||
|
||
### 1. Via `OTEL_INSTRUMENTATION_GENAI_EMITTERS` with an extra token | ||
If your emitter loading logic supports extra entry-point based names directly (depending on branch state), add the translator token (e.g. `traceloop_translator`). Example: | ||
|
||
```bash | ||
export OTEL_INSTRUMENTATION_GENAI_EMITTERS=span,traceloop_translator,traceloop_compat | ||
``` | ||
|
||
Ordering is important: we request placement `before=semconv_span` in the spec, but if your environment override reorders span emitters you can enforce explicitly (see next section). | ||
|
||
### 2. Using Category Override Environment Variable | ||
If your build supports category overrides (as implemented in `configuration.py`), you can prepend: | ||
|
||
```bash | ||
export OTEL_INSTRUMENTATION_GENAI_EMITTERS=span,traceloop_compat | ||
export OTEL_INSTRUMENTATION_GENAI_EMITTERS_SPAN=prepend:TraceloopTranslator | ||
``` | ||
|
||
The override ensures the translator emitter runs before the semantic span emitter regardless of default resolution order. | ||
|
||
## Example | ||
Minimal Python snippet (assuming emitters are loaded via entry points and the translator is installed): | ||
|
||
```python | ||
from opentelemetry.util.genai.handler import get_telemetry_handler | ||
from opentelemetry.util.genai.types import LLMInvocation, InputMessage, OutputMessage, Text | ||
|
||
inv = LLMInvocation( | ||
request_model="gpt-4", | ||
input_messages=[InputMessage(role="user", parts=[Text("Hello")])], | ||
attributes={ | ||
"traceloop.entity.name": "ChatLLM", | ||
"traceloop.workflow.name": "user_flow", | ||
"traceloop.callback.name": "root_chain", | ||
"traceloop.entity.input": "[{'role':'user','content':'Hello'}]", | ||
}, | ||
) | ||
handler = get_telemetry_handler() | ||
handler.start_llm(inv) | ||
inv.output_messages = [OutputMessage(role="assistant", parts=[Text("Hi")], finish_reason="stop")] | ||
handler.stop_llm(inv) | ||
# Result: final semantic span contains gen_ai.agent.name, gen_ai.workflow.name, gen_ai.input.messages, etc. | ||
``` | ||
|
||
## Non-Goals | ||
- It does not remove or rename original `traceloop.*` attributes (no destructive behavior yet). | ||
- It does not attempt deep semantic inference; mappings are intentionally conservative. | ||
- It does not serialize messages itself—relies on upstream emitters to have placed serialized content already. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Translator | ||
|
||
## Automatic Span Processing (Recommended) | ||
|
||
Add `TraceloopSpanProcessor` to your TracerProvider to automatically transform all matching spans: | ||
|
||
```python | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.util.genai.processors import TraceloopSpanProcessor | ||
|
||
# Set up tracer provider | ||
provider = TracerProvider() | ||
|
||
# Add processor - transforms all matching spans automatically | ||
processor = TraceloopSpanProcessor( | ||
attribute_transformations={ | ||
"remove": ["debug_info"], | ||
"rename": {"model_ver": "llm.model.version"}, | ||
"add": {"service.name": "my-llm"} | ||
}, | ||
name_transformations={"chat *": "llm.openai.chat"}, | ||
traceloop_attributes={ | ||
"traceloop.entity.name": "MyLLMEntity" | ||
} | ||
) | ||
provider.add_span_processor(processor) | ||
trace.set_tracer_provider(provider) | ||
|
||
``` | ||
|
||
## Transformation Rules | ||
|
||
### Attributes | ||
- **Remove**: `"remove": ["field1", "field2"]` | ||
- **Rename**: `"rename": {"old_name": "new_name"}` | ||
- **Add**: `"add": {"key": "value"}` | ||
|
||
### Span Names | ||
- **Direct**: `"old name": "new name"` | ||
- **Pattern**: `"chat *": "llm.chat"` (wildcard matching) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Translator | ||
|
||
## Automatic Span Processing (Recommended) | ||
|
||
Add `TraceloopSpanProcessor` to your TracerProvider to automatically transform all matching spans: | ||
|
||
```python | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.util.genai.processors import TraceloopSpanProcessor | ||
|
||
# Set up tracer provider | ||
provider = TracerProvider() | ||
|
||
# Add processor - transforms all matching spans automatically | ||
processor = TraceloopSpanProcessor( | ||
attribute_transformations={ | ||
"remove": ["debug_info"], | ||
"rename": {"model_ver": "llm.model.version"}, | ||
"add": {"service.name": "my-llm"} | ||
}, | ||
name_transformations={"chat *": "llm.openai.chat"}, | ||
traceloop_attributes={ | ||
"traceloop.entity.name": "MyLLMEntity" | ||
} | ||
) | ||
provider.add_span_processor(processor) | ||
trace.set_tracer_provider(provider) | ||
|
||
``` | ||
|
||
## Transformation Rules | ||
|
||
### Attributes | ||
- **Remove**: `"remove": ["field1", "field2"]` | ||
- **Rename**: `"rename": {"old_name": "new_name"}` | ||
- **Add**: `"add": {"key": "value"}` | ||
|
||
### Span Names | ||
- **Direct**: `"old name": "new name"` | ||
- **Pattern**: `"chat *": "llm.chat"` (wildcard matching) |
59 changes: 59 additions & 0 deletions
59
...elemetry-util-genai-dev/examples/traceloop_span_transformation/traceloop_rules_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from __future__ import annotations | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import ( | ||
SimpleSpanProcessor, | ||
ConsoleSpanExporter, | ||
) | ||
|
||
from opentelemetry.util.genai.handler import get_telemetry_handler | ||
from opentelemetry.util.genai.types import ( | ||
LLMInvocation, | ||
InputMessage, | ||
OutputMessage, | ||
Text, | ||
) | ||
|
||
|
||
def run_example(): | ||
provider = TracerProvider() | ||
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter())) | ||
trace.set_tracer_provider(provider) | ||
|
||
# Build a telemetry handler (singleton) – emitters are chosen via env vars | ||
handler = get_telemetry_handler(tracer_provider=provider) | ||
|
||
# Include a few illustrative Traceloop-style attributes. | ||
# These will be mapped/prefixed automatically by the Traceloop compat emitter. | ||
invocation = LLMInvocation( | ||
request_model="gpt-4", | ||
input_messages=[InputMessage(role="user", parts=[Text("Hello")])], | ||
attributes={ | ||
"custom.attribute": "value", # arbitrary user attribute | ||
"traceloop.entity.name": "ChatLLM", | ||
"traceloop.workflow.name": "main_flow", | ||
"traceloop.entity.path": "root/branch/leaf", | ||
"traceloop.entity.input": "Hi" | ||
}, | ||
) | ||
|
||
handler.start_llm(invocation) | ||
# Simulate model output | ||
invocation.output_messages = [ | ||
OutputMessage( | ||
role="assistant", parts=[Text("Hi there!")], finish_reason="stop" | ||
) | ||
] | ||
handler.stop_llm(invocation) | ||
|
||
print("\nInvocation complete. Check exporter output above for:" | ||
"\n * SemanticConvention span containing promoted gen_ai.* keys" | ||
"\n * Traceloop compat span (legacy format)" | ||
"\nIf translator emitter enabled, attributes like gen_ai.agent.name should be present.\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
run_example() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.