Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
73a764c
cherry pick changes from previous PR
keith-decker Sep 17, 2025
0d749a9
move span utils to new file
keith-decker Sep 17, 2025
77d9c3c
remove span state, use otel context for parent/child
keith-decker Sep 17, 2025
054ebe9
flatten LLMInvocation to use attributes instead of dict keys
keith-decker Sep 17, 2025
9d3926f
helper function and docstrings
keith-decker Sep 17, 2025
635b7f8
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 18, 2025
9837cf4
refactor: store span and context token in LLMInvocation instead of Sp…
keith-decker Sep 18, 2025
1a172d1
refactor: rename prompts/chat_generations to input_messages/output_me…
keith-decker Sep 19, 2025
465ca78
refactor: simplify TelemetryHandler API by moving invocation data man…
keith-decker Sep 19, 2025
cd5aaa6
refactor: update relative imports to absolute imports
keith-decker Sep 19, 2025
8347d17
Update handler to use a context manager instead of start_llm and stop…
keith-decker Sep 22, 2025
742a36f
resolve tox -e doc failure
keith-decker Sep 22, 2025
00e08a8
safeguard against empty request-model
keith-decker Sep 22, 2025
109625c
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 22, 2025
80c94bf
fix tox typecheck errors for utils
keith-decker Sep 22, 2025
f07c61f
refactor: move tracer to generator, clean up dead code
keith-decker Sep 22, 2025
d6f722f
remove unused linting hint
keith-decker Sep 22, 2025
fdc7f50
back off stricter request-model requirements
keith-decker Sep 22, 2025
da20448
reintroduce manual start/stop for langchain callback flow
keith-decker Sep 22, 2025
c1ff846
Merge branch 'main' into util-genai-inference-clean
aabmass Sep 23, 2025
4a08d7f
clean up context handler, clarify unit tests
keith-decker Sep 23, 2025
243bf8d
remove generator concept
keith-decker Sep 25, 2025
3d6c8da
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 25, 2025
5f8cd1c
update token types
keith-decker Sep 25, 2025
e987d66
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 26, 2025
51d28c0
code cleanup
keith-decker Sep 30, 2025
88d0149
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 30, 2025
188e84b
Merge branch 'main' into util-genai-inference-clean
keith-decker Oct 2, 2025
c264c46
Merge branch 'main' into util-genai-inference-clean
keith-decker Oct 6, 2025
61746ab
Merge branch 'main' into util-genai-inference-clean
aabmass Oct 7, 2025
d6001f5
Merge branch 'main' into util-genai-inference-clean
keith-decker Oct 8, 2025
b96eb20
Merge branch 'main' into util-genai-inference-clean
aabmass Oct 9, 2025
b1b2364
Refactor TestTelemetryHandler to use instance method for span exporte…
keith-decker Oct 9, 2025
51f0c8c
refactor: remove unused type properties
keith-decker Oct 9, 2025
7732024
refactor: update TelemetryHandler initialization to remove **kwargs
keith-decker Oct 9, 2025
839eeae
refactor: remove tracer variable
keith-decker Oct 9, 2025
4044274
refactor: code style updates
keith-decker Oct 9, 2025
4280eb9
refactor: replace json.dumps with gen_ai_json_dumps for message seria…
keith-decker Oct 9, 2025
be8620b
refactor: update span lifecycle to use sdk over setting context manually
keith-decker Oct 9, 2025
04d948a
refactor: don't reinvent span attribute assignment
keith-decker Oct 9, 2025
0d49e28
refactor: pylint update for python 3.13
keith-decker Oct 9, 2025
3a891b2
Revert "refactor: update span lifecycle to use sdk over setting conte…
keith-decker Oct 10, 2025
56ba760
Merge branch 'main' into util-genai-inference-clean
keith-decker Oct 10, 2025
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
1 change: 1 addition & 0 deletions docs/nitpick-exceptions.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ py-class=
psycopg.AsyncConnection
ObjectProxy
fastapi.applications.FastAPI
_contextvars.Token

any=
; API
Expand Down
5 changes: 5 additions & 0 deletions util/opentelemetry-util-genai/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3763](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3763))
- Add a utility to parse the `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` environment variable.
Add `gen_ai_latest_experimental` as a new value to the Sem Conv stability flag ([#3716](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3716)).

### Added

- Generate Spans for LLM invocations
- Helper functions for starting and finishing LLM invocations
19 changes: 19 additions & 0 deletions util/opentelemetry-util-genai/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ The GenAI Utils package will include boilerplate and helpers to standardize inst
This package will provide APIs and decorators to minimize the work needed to instrument genai libraries,
while providing standardization for generating both types of otel, "spans and metrics" and "spans, metrics and events"

This package relies on environment variables to configure capturing of message content.
By default, message content will not be captured.
Set the environment variable `OTEL_SEMCONV_STABILITY_OPT_IN` to `gen_ai_latest_experimental` to enable experimental features.
And set the environment variable `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` to `SPAN_ONLY` or `SPAN_AND_EVENT` to capture message content in spans.

This package provides these span attributes:

- `gen_ai.provider.name`: Str(openai)
- `gen_ai.operation.name`: Str(chat)
- `gen_ai.request.model`: Str(gpt-3.5-turbo)
- `gen_ai.response.finish_reasons`: Slice(["stop"])
- `gen_ai.response.model`: Str(gpt-3.5-turbo-0125)
- `gen_ai.response.id`: Str(chatcmpl-Bz8yrvPnydD9pObv625n2CGBPHS13)
- `gen_ai.usage.input_tokens`: Int(24)
- `gen_ai.usage.output_tokens`: Int(7)
- `gen_ai.input.messages`: Str('[{"role": "Human", "parts": [{"content": "hello world", "type": "text"}]}]')
- `gen_ai.output.messages`: Str('[{"role": "AI", "parts": [{"content": "hello back", "type": "text"}], "finish_reason": "stop"}]')


Installation
------------

Expand Down
6 changes: 3 additions & 3 deletions util/opentelemetry-util-genai/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dynamic = ["version"]
description = "OpenTelemetry GenAI Utils"
readme = "README.rst"
license = "Apache-2.0"
requires-python = ">=3.8"
requires-python = ">=3.9"
authors = [
{ name = "OpenTelemetry Authors", email = "[email protected]" },
]
Expand All @@ -25,8 +25,8 @@ classifiers = [
"Programming Language :: Python :: 3.13",
]
dependencies = [
"opentelemetry-instrumentation ~= 0.51b0",
"opentelemetry-semantic-conventions ~= 0.51b0",
"opentelemetry-instrumentation ~= 0.57b0",
"opentelemetry-semantic-conventions ~= 0.57b0",
"opentelemetry-api>=1.31.0",
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Telemetry handler for GenAI invocations.
This module exposes the `TelemetryHandler` class, which manages the lifecycle of
GenAI (Generative AI) invocations and emits telemetry data (spans and related attributes).
It supports starting, stopping, and failing LLM invocations.
Classes:
- TelemetryHandler: Manages GenAI invocation lifecycles and emits telemetry.
Functions:
- get_telemetry_handler: Returns a singleton `TelemetryHandler` instance.
Usage:
handler = get_telemetry_handler()
# Create an invocation object with your request data
# The span and context_token attributes are set by the TelemetryHandler, and
# managed by the TelemetryHandler during the lifecycle of the span.
# Use the context manager to manage the lifecycle of an LLM invocation.
with handler.llm(invocation) as invocation:
# Populate outputs and any additional attributes
invocation.output_messages = [...]
invocation.attributes.update({"more": "attrs"})
# Or, if you prefer to manage the lifecycle manually
invocation = LLMInvocation(
request_model="my-model",
input_messages=[...],
provider="my-provider",
attributes={"custom": "attr"},
)
# Start the invocation (opens a span)
handler.start_llm(invocation)
# Populate outputs and any additional attributes, then stop (closes the span)
invocation.output_messages = [...]
invocation.attributes.update({"more": "attrs"})
handler.stop_llm(invocation)
# Or, in case of error
handler.fail_llm(invocation, Error(type="...", message="..."))
"""

from __future__ import annotations

from contextlib import contextmanager
from typing import Iterator, Optional

from opentelemetry import context as otel_context
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAI,
)
from opentelemetry.semconv.schemas import Schemas
from opentelemetry.trace import (
SpanKind,
TracerProvider,
get_tracer,
set_span_in_context,
)
from opentelemetry.util.genai.span_utils import (
_apply_error_attributes,
_apply_finish_attributes,
)
from opentelemetry.util.genai.types import Error, LLMInvocation
from opentelemetry.util.genai.version import __version__


class TelemetryHandler:
"""
High-level handler managing GenAI invocation lifecycles and emitting
them as spans, metrics, and events.
"""

def __init__(self, tracer_provider: TracerProvider | None = None):
self._tracer = get_tracer(
__name__,
__version__,
tracer_provider,
schema_url=Schemas.V1_36_0.value,
)

def start_llm(
self,
invocation: LLMInvocation,
) -> LLMInvocation:
"""Start an LLM invocation and create a pending span entry."""
# Create a span and attach it as current; keep the token to detach later
span = self._tracer.start_span(
name=f"{GenAI.GenAiOperationNameValues.CHAT.value} {invocation.request_model}",
kind=SpanKind.CLIENT,
)
invocation.span = span
invocation.context_token = otel_context.attach(
set_span_in_context(span)
)
return invocation

def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation: # pylint: disable=no-self-use
"""Finalize an LLM invocation successfully and end its span."""
if invocation.context_token is None or invocation.span is None:
# TODO: Provide feedback that this invocation was not started
return invocation

_apply_finish_attributes(invocation.span, invocation)
# Detach context and end span
otel_context.detach(invocation.context_token)
invocation.span.end()
return invocation

def fail_llm( # pylint: disable=no-self-use
self, invocation: LLMInvocation, error: Error
) -> LLMInvocation:
"""Fail an LLM invocation and end its span with error status."""
if invocation.context_token is None or invocation.span is None:
# TODO: Provide feedback that this invocation was not started
return invocation

_apply_error_attributes(invocation.span, error)
# Detach context and end span
otel_context.detach(invocation.context_token)
invocation.span.end()
return invocation

@contextmanager
def llm(
self, invocation: Optional[LLMInvocation] = None
) -> Iterator[LLMInvocation]:
"""Context manager for LLM invocations.
Only set data attributes on the invocation object, do not modify the span or context.
Starts the span on entry. On normal exit, finalizes the invocation and ends the span.
If an exception occurs inside the context, marks the span as error, ends it, and
re-raises the original exception.
"""
if invocation is None:
invocation = LLMInvocation(
request_model="",
)
self.start_llm(invocation)
try:
yield invocation
except Exception as exc:
self.fail_llm(invocation, Error(message=str(exc), type=type(exc)))
raise
self.stop_llm(invocation)


def get_telemetry_handler(
tracer_provider: TracerProvider | None = None,
) -> TelemetryHandler:
"""
Returns a singleton TelemetryHandler instance.
"""
handler: Optional[TelemetryHandler] = getattr(
get_telemetry_handler, "_default_handler", None
)
if handler is None:
handler = TelemetryHandler(tracer_provider=tracer_provider)
setattr(get_telemetry_handler, "_default_handler", handler)
return handler
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import asdict
from typing import List

from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAI,
)
from opentelemetry.semconv.attributes import (
error_attributes as ErrorAttributes,
)
from opentelemetry.trace import (
Span,
)
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.util.genai.types import (
Error,
InputMessage,
LLMInvocation,
OutputMessage,
)
from opentelemetry.util.genai.utils import (
ContentCapturingMode,
gen_ai_json_dumps,
get_content_capturing_mode,
is_experimental_mode,
)


def _apply_common_span_attributes(
span: Span, invocation: LLMInvocation
) -> None:
"""Apply attributes shared by finish() and error() and compute metrics.

Returns (genai_attributes) for use with metrics.
"""
span.update_name(
f"{GenAI.GenAiOperationNameValues.CHAT.value} {invocation.request_model}".strip()
)
span.set_attribute(
GenAI.GEN_AI_OPERATION_NAME, GenAI.GenAiOperationNameValues.CHAT.value
)
if invocation.request_model:
span.set_attribute(
GenAI.GEN_AI_REQUEST_MODEL, invocation.request_model
)
if invocation.provider is not None:
# TODO: clean provider name to match GenAiProviderNameValues?
span.set_attribute(GenAI.GEN_AI_PROVIDER_NAME, invocation.provider)

if invocation.output_messages:
span.set_attribute(
GenAI.GEN_AI_RESPONSE_FINISH_REASONS,
[gen.finish_reason for gen in invocation.output_messages],
)

if invocation.response_model_name is not None:
span.set_attribute(
GenAI.GEN_AI_RESPONSE_MODEL, invocation.response_model_name
)
if invocation.response_id is not None:
span.set_attribute(GenAI.GEN_AI_RESPONSE_ID, invocation.response_id)
if invocation.input_tokens is not None:
span.set_attribute(
GenAI.GEN_AI_USAGE_INPUT_TOKENS, invocation.input_tokens
)
if invocation.output_tokens is not None:
span.set_attribute(
GenAI.GEN_AI_USAGE_OUTPUT_TOKENS, invocation.output_tokens
)


def _maybe_set_span_messages(
span: Span,
input_messages: List[InputMessage],
output_messages: List[OutputMessage],
) -> None:
if not is_experimental_mode() or get_content_capturing_mode() not in (
ContentCapturingMode.SPAN_ONLY,
ContentCapturingMode.SPAN_AND_EVENT,
):
return
if input_messages:
span.set_attribute(
GenAI.GEN_AI_INPUT_MESSAGES,
gen_ai_json_dumps([asdict(message) for message in input_messages]),
)
if output_messages:
span.set_attribute(
GenAI.GEN_AI_OUTPUT_MESSAGES,
gen_ai_json_dumps(
[asdict(message) for message in output_messages]
),
)


def _apply_finish_attributes(span: Span, invocation: LLMInvocation) -> None:
"""Apply attributes/messages common to finish() paths."""
_apply_common_span_attributes(span, invocation)
_maybe_set_span_messages(
span, invocation.input_messages, invocation.output_messages
)
span.set_attributes(invocation.attributes)


def _apply_error_attributes(span: Span, error: Error) -> None:
"""Apply status and error attributes common to error() paths."""
span.set_status(Status(StatusCode.ERROR, error.message))
if span.is_recording():
span.set_attribute(ErrorAttributes.ERROR_TYPE, error.type.__qualname__)


__all__ = [
"_apply_finish_attributes",
"_apply_error_attributes",
]
Loading