Skip to content

Commit 4175da2

Browse files
committed
Add common gen AI utils into opentelemetry-instrumentation
1 parent 07c97ea commit 4175da2

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from os import environ
16+
from typing import Mapping
17+
18+
from opentelemetry.semconv._incubating.attributes import (
19+
gen_ai_attributes as GenAIAttributes,
20+
)
21+
from opentelemetry.semconv.attributes import (
22+
error_attributes as ErrorAttributes,
23+
)
24+
from opentelemetry.trace import Span
25+
from opentelemetry.trace.status import Status, StatusCode
26+
from opentelemetry.util.types import AttributeValue
27+
28+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = (
29+
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
30+
)
31+
32+
33+
def is_content_enabled() -> bool:
34+
capture_content = environ.get(
35+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false"
36+
)
37+
38+
return capture_content.lower() == "true"
39+
40+
41+
def get_span_name(span_attributes: Mapping[str, AttributeValue]) -> str:
42+
name = span_attributes.get(GenAIAttributes.GEN_AI_OPERATION_NAME, "")
43+
model = span_attributes.get(GenAIAttributes.GEN_AI_REQUEST_MODEL, "")
44+
return f"{name} {model}"
45+
46+
47+
def handle_span_exception(span: Span, error: Exception) -> None:
48+
span.set_status(Status(StatusCode.ERROR, str(error)))
49+
if span.is_recording():
50+
span.set_attribute(
51+
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
52+
)
53+
span.end()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest.mock import patch
16+
17+
from opentelemetry.instrumentation.genai_utils import (
18+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
19+
get_span_name,
20+
handle_span_exception,
21+
is_content_enabled,
22+
)
23+
from opentelemetry.sdk.trace import ReadableSpan
24+
from opentelemetry.test.test_base import TestBase
25+
from opentelemetry.trace.status import StatusCode
26+
27+
28+
class MyTestException(Exception):
29+
pass
30+
31+
32+
class TestGenaiUtils(TestBase):
33+
@patch.dict(
34+
"os.environ",
35+
{},
36+
)
37+
def test_is_content_enabled_default(self):
38+
self.assertFalse(is_content_enabled())
39+
40+
def test_is_content_enabled_true(self):
41+
for env_value in "true", "TRUE", "True", "tRue":
42+
with patch.dict(
43+
"os.environ",
44+
{
45+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: env_value
46+
},
47+
):
48+
self.assertTrue(is_content_enabled())
49+
50+
def test_is_content_enabled_false(self):
51+
for env_value in "false", "FALSE", "False", "fAlse":
52+
with patch.dict(
53+
"os.environ",
54+
{
55+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: env_value
56+
},
57+
):
58+
self.assertFalse(is_content_enabled())
59+
60+
def test_get_span_name(self):
61+
span_attributes = {
62+
"gen_ai.operation.name": "chat",
63+
"gen_ai.request.model": "mymodel",
64+
}
65+
self.assertEqual(get_span_name(span_attributes), "chat mymodel")
66+
67+
span_attributes = {
68+
"gen_ai.operation.name": "chat",
69+
}
70+
self.assertEqual(get_span_name(span_attributes), "chat ")
71+
72+
span_attributes = {
73+
"gen_ai.request.model": "mymodel",
74+
}
75+
self.assertEqual(get_span_name(span_attributes), " mymodel")
76+
77+
span_attributes = {}
78+
self.assertEqual(get_span_name(span_attributes), " ")
79+
80+
def test_handle_span_exception(self):
81+
tracer = self.tracer_provider.get_tracer("test_handle_span_exception")
82+
with tracer.start_as_current_span("foo") as span:
83+
handle_span_exception(span, MyTestException())
84+
85+
self.assertEqual(len(self.get_finished_spans()), 1)
86+
finished_span: ReadableSpan = self.get_finished_spans()[0]
87+
self.assertEqual(finished_span.name, "foo")
88+
self.assertIs(finished_span.status.status_code, StatusCode.ERROR)
89+
self.assertEqual(
90+
finished_span.attributes["error.type"], "MyTestException"
91+
)

0 commit comments

Comments
 (0)