Skip to content
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
00c2091
WIP initial code import
keith-decker Aug 15, 2025
40e6c48
remove references to tool types
keith-decker Aug 15, 2025
43526d6
add a simple unit test
keith-decker Aug 17, 2025
76dbd57
rename exporter to emitter.
keith-decker Aug 18, 2025
567e0a4
rename api file to client
keith-decker Aug 18, 2025
fd41dfb
Merge branch 'main' into util-genai-inference
keith-decker Aug 20, 2025
4bd72aa
WIP gen_ai chat refactor
keith-decker Aug 25, 2025
59414fa
Add provider.name, rename client to handler
keith-decker Aug 25, 2025
5127c39
add message to log functions
keith-decker Aug 25, 2025
66e0c26
remove events, emit structured logs
keith-decker Aug 26, 2025
4346580
update documentation
keith-decker Aug 27, 2025
4be03c4
first round of cleanup for lintcheck
keith-decker Aug 27, 2025
c19e4e6
typecheck fixes
keith-decker Aug 27, 2025
11d937b
typecheck fixes
keith-decker Aug 27, 2025
fdc71a3
linting fixes
keith-decker Aug 27, 2025
04d6e97
linting fixes, refactor for complexity
keith-decker Aug 27, 2025
a2e017a
typecheck fixes
keith-decker Aug 27, 2025
92cd5c8
update documentation
keith-decker Aug 28, 2025
770f878
rename context, update _to_semconv_dict name
keith-decker Sep 2, 2025
78bc6bf
refactor: rename emitters to generators and update method names for c…
keith-decker Sep 2, 2025
b7360f8
refactor: convert API LogRecord to SDK LogRecord, add unit test
keith-decker Sep 2, 2025
86152df
added changelog
keith-decker Sep 3, 2025
c0e03c4
Merge branch 'main' into util-genai-inference
keith-decker Sep 4, 2025
a5b04be
Merge branch 'main' into util-genai-inference
keith-decker Sep 5, 2025
3c94ecd
Wip Convert input messages
keith-decker Sep 5, 2025
a59ca1e
wip, refactor chat generation to output message
keith-decker Sep 5, 2025
a8bb486
cleanup
keith-decker Sep 5, 2025
f64bf69
merge test files
keith-decker Sep 5, 2025
4304819
lint updates
keith-decker Sep 8, 2025
f689ad1
remove logging, lower PR size
keith-decker Sep 8, 2025
b3bb75c
lint update
keith-decker Sep 8, 2025
fb30d45
check env for content capture
keith-decker Sep 8, 2025
fd45c43
check env for content capture on output
keith-decker Sep 8, 2025
aeaa319
lint updates
keith-decker Sep 8, 2025
6a19e04
remove metrics to reduce PR size
keith-decker Sep 8, 2025
ad062e3
remove metrics file
keith-decker Sep 8, 2025
fc0cde0
cleanup docs, remove meter/logger provider
keith-decker Sep 8, 2025
5cea4a6
remove overly complicated env check, add messages to test
keith-decker Sep 8, 2025
8e5c6ab
update file doc strings
keith-decker Sep 8, 2025
16f20b8
lint updates
keith-decker Sep 8, 2025
5ba84ef
remove unused properties
keith-decker Sep 9, 2025
381d38d
move utility function to utils
keith-decker Sep 9, 2025
b983dcd
Merge branch 'main' into util-genai-inference
keith-decker Sep 9, 2025
3a37f43
Additional span attributes
keith-decker Sep 9, 2025
b769ed6
Merge branch 'util-genai-inference' of github.com:zhirafovod/opentele…
keith-decker Sep 9, 2025
0776a7f
update span name
keith-decker Sep 9, 2025
7071d0f
Revert "Additional span attributes"
keith-decker Sep 9, 2025
bb184da
remove unused thread lock
keith-decker Sep 9, 2025
0b18708
update span name
keith-decker Sep 9, 2025
4f8e860
Merge branch 'main' into util-genai-inference
keith-decker Sep 9, 2025
e72a320
Update documentation, rename system to provider
keith-decker Sep 10, 2025
a704fc6
cleanup docs and unclear code
keith-decker Sep 10, 2025
54d753b
Merge branch 'main' into util-genai-inference
keith-decker Sep 11, 2025
0d4f204
add unit test for parent/child relationship
keith-decker Sep 11, 2025
82e4bff
cleanup some duplicated code
keith-decker Sep 11, 2025
d7c6187
cleanup duplicated start span code, move parent check into context ma…
keith-decker Sep 11, 2025
b12f865
remove unneccesary helper function
keith-decker Sep 11, 2025
b61437f
First commit for langchain instrumentation
wrisa Jun 4, 2025
1a11630
removed env
wrisa Jul 14, 2025
4f0e86c
added tool support and modified llm accordingly
wrisa Aug 10, 2025
4e07998
Added evaluation span and event. Added log api for event
wrisa Aug 12, 2025
0847f3b
added deepeval metric measure
wrisa Aug 12, 2025
b86d793
Fixing tests and adding tool call tests for langchain instrumentation
Aug 15, 2025
89388e1
Adding readme for test_langchain_llm.py
Aug 15, 2025
b26952d
WIP initial code import
keith-decker Aug 15, 2025
8578d76
remove references to tool types
keith-decker Aug 15, 2025
765758e
add a simple unit test
keith-decker Aug 17, 2025
3dbe121
rename exporter to emitter.
keith-decker Aug 18, 2025
2743109
rename api file to client
keith-decker Aug 18, 2025
5c009b3
WIP gen_ai chat refactor
keith-decker Aug 25, 2025
2b4d96c
Add provider.name, rename client to handler
keith-decker Aug 25, 2025
5e723c4
add message to log functions
keith-decker Aug 25, 2025
4404c20
remove events, emit structured logs
keith-decker Aug 26, 2025
a9f0f64
update documentation
keith-decker Aug 27, 2025
9eb69bf
first round of cleanup for lintcheck
keith-decker Aug 27, 2025
c92fdec
typecheck fixes
keith-decker Aug 27, 2025
91b18ad
typecheck fixes
keith-decker Aug 27, 2025
09d6f4c
linting fixes
keith-decker Aug 27, 2025
840e9fd
linting fixes, refactor for complexity
keith-decker Aug 27, 2025
70e8d1d
typecheck fixes
keith-decker Aug 27, 2025
a1485d2
update documentation
keith-decker Aug 28, 2025
a734de7
rename context, update _to_semconv_dict name
keith-decker Sep 2, 2025
c5bc930
refactor: rename emitters to generators and update method names for c…
keith-decker Sep 2, 2025
ab9f170
refactor: convert API LogRecord to SDK LogRecord, add unit test
keith-decker Sep 2, 2025
521824f
added changelog
keith-decker Sep 3, 2025
7c017c4
Merge branch 'main' into util-genai-inference
keith-decker Sep 16, 2025
cab5ca0
clean up extra functions
keith-decker Sep 16, 2025
d97fdca
Merge branch 'genai-utils-e2e-dev' into util-genai-inference
zhirafovod Sep 16, 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
11 changes: 10 additions & 1 deletion util/opentelemetry-util-genai/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
## [Unreleased]

### Added

- Generate Spans for LLM invocations
- Generate Metrics for LLM invocations
- Generate Logs for LLM invocations
- Helper functions for starting and finishing LLM invocations


34 changes: 33 additions & 1 deletion util/opentelemetry-util-genai/README.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
OpenTelemetry Util for GenAI
=======================
============================


The GenAI Utils package will include boilerplate and helpers to standardize instrumentation for Generative AI.
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 provides these span attributes.
-> gen_ai.provider.name: Str(openai)
-> gen_ai.operation.name: Str(chat)
-> gen_ai.framework: Str(langchain)
-> gen_ai.system: Str(openai) # deprecated
-> 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\": \"user\", \"content\": \"hello world\"}]")


This package also provides these metric attributes.
Token Usage Metrics:
-> gen_ai.provider.name: Str(openai)
-> gen_ai.operation.name: Str(chat)
-> gen_ai.framework: Str(langchain)
-> gen_ai.request.model: Str(gpt-3.5-turbo)
-> gen_ai.response.model: Str(gpt-3.5-turbo-0125)
-> gen_ai.usage.input_tokens: Int(24)
-> gen_ai.usage.output_tokens: Int(7)
-> gen_ai.token.type: Str(input|output)

Duration Metrics:
-> gen_ai.provider.name: Str(openai)
-> gen_ai.operation.name: Str(chat)
-> gen_ai.framework: Str(langchain)
-> gen_ai.request.model: Str(gpt-3.5-turbo)
-> gen_ai.response.model: Str(gpt-3.5-turbo-0125)

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 @@ -25,9 +25,9 @@ classifiers = [
"Programming Language :: Python :: 3.13",
]
dependencies = [
"opentelemetry-instrumentation ~= 0.51b0",
"opentelemetry-semantic-conventions ~= 0.51b0",
"opentelemetry-api>=1.31.0",
"opentelemetry-instrumentation ~= 0.57b0",
"opentelemetry-semantic-conventions ~= 0.57b0",
"opentelemetry-api>=1.36.0",
]

[project.optional-dependencies]
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,69 @@
# 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 dataclass
from typing import List, Literal, Optional, Type, TypedDict


class TextPart(TypedDict):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very similar to #3718 @DylanRussell

type: Literal["text"]
content: str


# Keep room for future parts without changing the return type
# addition of tools can use Part = Union[TextPart, ToolPart]
Part = TextPart


class OtelMessage(TypedDict):
role: str
# role: Literal["user", "assistant", "system", "tool", "tool_message"] # TODO: check semconvs for allowed roles
parts: List[Part]


@dataclass
class Message:
content: str
type: str
name: str

def _to_semconv_dict(self) -> OtelMessage:
"""Convert the message to a dictionary suitable for OpenTelemetry semconvs.
Ref: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/gen-ai.md#gen-ai-input-messages
"""

# TODO: Support tool_call and tool_call response
return {
"role": self.type,
"parts": [
{
"content": self.content,
"type": "text",
}
],
}


@dataclass
class ChatGeneration:
content: str
type: str
finish_reason: Optional[str] = None


@dataclass
class Error:
message: str
type: Type[BaseException]
Loading