|
| 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 | +import os |
| 16 | +import unittest |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from opentelemetry.instrumentation._semconv import ( |
| 20 | + OTEL_SEMCONV_STABILITY_OPT_IN, |
| 21 | + _OpenTelemetrySemanticConventionStability, |
| 22 | +) |
| 23 | +from opentelemetry.util.genai.environment_variables import ( |
| 24 | + OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, |
| 25 | +) |
| 26 | +from opentelemetry.util.genai.types import ContentCapturingMode |
| 27 | +from opentelemetry.util.genai.utils import get_content_capturing_mode |
| 28 | + |
| 29 | + |
| 30 | +def patch_env_vars(stability_mode, content_capturing): |
| 31 | + def decorator(test_case): |
| 32 | + @patch.dict( |
| 33 | + os.environ, |
| 34 | + { |
| 35 | + OTEL_SEMCONV_STABILITY_OPT_IN: stability_mode, |
| 36 | + OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: content_capturing, |
| 37 | + }, |
| 38 | + ) |
| 39 | + def wrapper(*args, **kwargs): |
| 40 | + # Reset state. |
| 41 | + _OpenTelemetrySemanticConventionStability._initialized = False |
| 42 | + _OpenTelemetrySemanticConventionStability._initialize() |
| 43 | + return test_case(*args, **kwargs) |
| 44 | + |
| 45 | + return wrapper |
| 46 | + |
| 47 | + return decorator |
| 48 | + |
| 49 | + |
| 50 | +class TestVersion(unittest.TestCase): |
| 51 | + @patch_env_vars( |
| 52 | + stability_mode="gen_ai_latest_experimental", |
| 53 | + content_capturing="SPAN_ONLY", |
| 54 | + ) |
| 55 | + def test_get_content_capturing_mode_parses_valid_envvar(self): # pylint: disable=no-self-use |
| 56 | + assert get_content_capturing_mode() == ContentCapturingMode.SPAN_ONLY |
| 57 | + |
| 58 | + @patch_env_vars( |
| 59 | + stability_mode="gen_ai_latest_experimental", content_capturing="" |
| 60 | + ) |
| 61 | + def test_empty_content_capturing_envvar(self): # pylint: disable=no-self-use |
| 62 | + assert get_content_capturing_mode() == ContentCapturingMode.NO_CONTENT |
| 63 | + |
| 64 | + @patch_env_vars(stability_mode="default", content_capturing="True") |
| 65 | + def test_get_content_capturing_mode_raises_exception_when_semconv_stability_default( |
| 66 | + self, |
| 67 | + ): # pylint: disable=no-self-use |
| 68 | + with self.assertRaises(ValueError): |
| 69 | + get_content_capturing_mode() |
| 70 | + |
| 71 | + @patch_env_vars( |
| 72 | + stability_mode="gen_ai_latest_experimental", |
| 73 | + content_capturing="INVALID_VALUE", |
| 74 | + ) |
| 75 | + def test_get_content_capturing_mode_raises_exception_on_invalid_envvar( |
| 76 | + self, |
| 77 | + ): # pylint: disable=no-self-use |
| 78 | + with self.assertLogs(level="WARNING") as cm: |
| 79 | + assert ( |
| 80 | + get_content_capturing_mode() == ContentCapturingMode.NO_CONTENT |
| 81 | + ) |
| 82 | + self.assertEqual(len(cm.output), 1) |
| 83 | + self.assertIn("INVALID_VALUE is not a valid option for ", cm.output[0]) |
0 commit comments