-
Notifications
You must be signed in to change notification settings - Fork 798
Add gen_ai_latest_experimental
to the Sem Conv stability flag. Add ContentCapturingMode
enum
#3716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
ec42339
initial commit
DylanRussell 353243a
lint
DylanRussell fafa959
Merge branch 'main' into gen_ai_flags
DylanRussell 01cabcd
Fix tests
DylanRussell 45cdaff
Fix typecheck
DylanRussell b2c5b19
Update opentelemetry-instrumentation/src/opentelemetry/instrumentatio…
DylanRussell 96f2052
Update util/opentelemetry-util-genai/src/opentelemetry/util/genai/uti…
DylanRussell 748f1ca
Use ValueError
DylanRussell 787df1d
Merge branch 'main' into gen_ai_flags
emdneto 185d782
Rename enum
DylanRussell 06bbeb9
Update util/opentelemetry-util-genai/tests/test_utils.py
DylanRussell 80eeea8
Update util/opentelemetry-util-genai/tests/test_utils.py
DylanRussell 338bfd9
Update util/opentelemetry-util-genai/tests/test_utils.py
DylanRussell 695740a
Update util/opentelemetry-util-genai/tests/test_utils.py
DylanRussell a75d51b
Default env var to NO_CONTENT when invalid envvar
DylanRussell 727dd70
Merge branch 'gen_ai_flags' of github.com:DylanRussell/opentelemetry-…
DylanRussell 6a66251
Address comments
DylanRussell 1b60ec1
Address comment
DylanRussell 1387c6c
Fix typecheck
DylanRussell a8640cc
don't change typecheck,
DylanRussell e9adb53
Fix linter
DylanRussell d6c106e
Merge branch 'main' into gen_ai_flags
DylanRussell f6aef69
Address comments
DylanRussell 9645a46
Merge branch 'main' into gen_ai_flags
DylanRussell 691eeec
Fix order of args..
DylanRussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
util/opentelemetry-util-genai/src/opentelemetry/util/genai/environment_variables.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 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. | ||
|
||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = ( | ||
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" | ||
) |
26 changes: 26 additions & 0 deletions
26
util/opentelemetry-util-genai/src/opentelemetry/util/genai/types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 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 enum import Enum | ||
|
||
|
||
class ContentCapturingMode(Enum): | ||
# Do not capture content (default). | ||
NO_CONTENT = 0 | ||
# Only capture content in spans. | ||
SPAN_ONLY = 1 | ||
# Only capture content in events. | ||
EVENT_ONLY = 2 | ||
# Capture content in both spans and events. | ||
SPAN_AND_EVENT = 3 |
52 changes: 52 additions & 0 deletions
52
util/opentelemetry-util-genai/src/opentelemetry/util/genai/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 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 logging | ||
import os | ||
|
||
from opentelemetry.instrumentation._semconv import ( | ||
_OpenTelemetrySemanticConventionStability, | ||
_OpenTelemetryStabilitySignalType, | ||
_StabilityMode, | ||
) | ||
from opentelemetry.util.genai.environment_variables import ( | ||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, | ||
) | ||
from opentelemetry.util.genai.types import ContentCapturingMode | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_content_capturing_mode() -> ContentCapturingMode: | ||
DylanRussell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
envvar = os.environ.get(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT) | ||
if ( | ||
_OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode( | ||
_OpenTelemetryStabilitySignalType.GEN_AI, | ||
) | ||
== _StabilityMode.DEFAULT | ||
): | ||
raise ValueError( | ||
"This function should never be called when StabilityMode is default." | ||
) | ||
if not envvar: | ||
return ContentCapturingMode.NO_CONTENT | ||
try: | ||
return ContentCapturingMode[envvar.upper()] | ||
except KeyError: | ||
logger.warning( | ||
"%s is not a valid option for `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` environment variable. Must be one of %s. Defaulting to `NO_COTENT`.", | ||
DylanRussell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
envvar, | ||
", ".join(e.name for e in ContentCapturingMode), | ||
) | ||
return ContentCapturingMode.NO_CONTENT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# 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): | ||
DylanRussell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.