|
| 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