84 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
| # 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.
 | |
| 
 | |
| import os
 | |
| import unittest
 | |
| from unittest.mock import patch
 | |
| 
 | |
| from opentelemetry.instrumentation._semconv import (
 | |
|     OTEL_SEMCONV_STABILITY_OPT_IN,
 | |
|     _OpenTelemetrySemanticConventionStability,
 | |
| )
 | |
| from opentelemetry.util.genai.environment_variables import (
 | |
|     OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
 | |
| )
 | |
| from opentelemetry.util.genai.types import ContentCapturingMode
 | |
| from opentelemetry.util.genai.utils import get_content_capturing_mode
 | |
| 
 | |
| 
 | |
| def patch_env_vars(stability_mode, content_capturing):
 | |
|     def decorator(test_case):
 | |
|         @patch.dict(
 | |
|             os.environ,
 | |
|             {
 | |
|                 OTEL_SEMCONV_STABILITY_OPT_IN: stability_mode,
 | |
|                 OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: content_capturing,
 | |
|             },
 | |
|         )
 | |
|         def wrapper(*args, **kwargs):
 | |
|             # Reset state.
 | |
|             _OpenTelemetrySemanticConventionStability._initialized = False
 | |
|             _OpenTelemetrySemanticConventionStability._initialize()
 | |
|             return test_case(*args, **kwargs)
 | |
| 
 | |
|         return wrapper
 | |
| 
 | |
|     return decorator
 | |
| 
 | |
| 
 | |
| class TestVersion(unittest.TestCase):
 | |
|     @patch_env_vars(
 | |
|         stability_mode="gen_ai_latest_experimental",
 | |
|         content_capturing="SPAN_ONLY",
 | |
|     )
 | |
|     def test_get_content_capturing_mode_parses_valid_envvar(self):  # pylint: disable=no-self-use
 | |
|         assert get_content_capturing_mode() == ContentCapturingMode.SPAN_ONLY
 | |
| 
 | |
|     @patch_env_vars(
 | |
|         stability_mode="gen_ai_latest_experimental", content_capturing=""
 | |
|     )
 | |
|     def test_empty_content_capturing_envvar(self):  # pylint: disable=no-self-use
 | |
|         assert get_content_capturing_mode() == ContentCapturingMode.NO_CONTENT
 | |
| 
 | |
|     @patch_env_vars(stability_mode="default", content_capturing="True")
 | |
|     def test_get_content_capturing_mode_raises_exception_when_semconv_stability_default(
 | |
|         self,
 | |
|     ):  # pylint: disable=no-self-use
 | |
|         with self.assertRaises(ValueError):
 | |
|             get_content_capturing_mode()
 | |
| 
 | |
|     @patch_env_vars(
 | |
|         stability_mode="gen_ai_latest_experimental",
 | |
|         content_capturing="INVALID_VALUE",
 | |
|     )
 | |
|     def test_get_content_capturing_mode_raises_exception_on_invalid_envvar(
 | |
|         self,
 | |
|     ):  # pylint: disable=no-self-use
 | |
|         with self.assertLogs(level="WARNING") as cm:
 | |
|             assert (
 | |
|                 get_content_capturing_mode() == ContentCapturingMode.NO_CONTENT
 | |
|             )
 | |
|         self.assertEqual(len(cm.output), 1)
 | |
|         self.assertIn("INVALID_VALUE is not a valid option for ", cm.output[0])
 |