Skip to content

Include more request configuration options into the span attributes for the Google GenAI SDK instrumentation #3374

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
14ab0b4
Create a utility to simplify recording request attributes.
michaelsafyan Mar 6, 2025
cd5a36a
Merge branch 'open-telemetry:main' into google_genai_attribute_improv…
michaelsafyan Mar 14, 2025
2fc0ad1
Update recording mechanism to record more request options.
michaelsafyan Mar 14, 2025
969c003
Merge branch 'open-telemetry:main' into google_genai_attribute_improv…
michaelsafyan Mar 19, 2025
88b7e45
Improve the recording of span request attributes.
michaelsafyan Mar 19, 2025
744ef1c
Reformat with ruff.
michaelsafyan Mar 19, 2025
76c84c3
Update TODOs to reflect change made here.
michaelsafyan Mar 19, 2025
3953ea1
Update changelog now that PR has been created and can be referenced.
michaelsafyan Mar 19, 2025
d6f5f36
Merge branch 'open-telemetry:main' into google_genai_attribute_improv…
michaelsafyan Mar 25, 2025
7354e6f
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Mar 26, 2025
d3526fa
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Mar 27, 2025
80c8df1
Fix lint issues.
michaelsafyan Mar 27, 2025
43987e4
Reformat with ruff.
michaelsafyan Mar 27, 2025
6b8c599
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Mar 28, 2025
aaaa017
Add more documentation comments requested in the pull request.
michaelsafyan Mar 28, 2025
3d911b6
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Mar 28, 2025
5153080
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Mar 31, 2025
031369b
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Apr 2, 2025
fa8fa60
Add tests and comments that provide some additional clarity regarding…
michaelsafyan Apr 4, 2025
2a08ecd
Add tests and comments that provide some additional clarity regarding…
michaelsafyan Apr 4, 2025
97dab62
Handle corner case where flatten function returns compound output.
michaelsafyan Apr 4, 2025
3107e56
Update prefix to match currently proposed SemConv.
michaelsafyan Apr 4, 2025
cb4ca3b
Update to specify attributes from SemConv constants per PR feedback.
michaelsafyan Apr 4, 2025
0b9c5bb
Use an allowlist for dynamic keys per PR feedback.
michaelsafyan Apr 7, 2025
ff73608
Reformat with ruff.
michaelsafyan Apr 7, 2025
d0f5444
Fix lint issues.
michaelsafyan Apr 7, 2025
ffb898b
Reformat with ruff.
michaelsafyan Apr 7, 2025
f583614
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Apr 8, 2025
d6fe6a7
Handle flattening errors more gracefully.
michaelsafyan Apr 8, 2025
c4a7d12
Add support for more wildcards in the allowlist.
michaelsafyan Apr 8, 2025
f23216f
Add a clearer type for the flatten functions.
michaelsafyan Apr 8, 2025
d522ea0
Simplify 'exclude_keys' initialization per PR feedback.
michaelsafyan Apr 8, 2025
0e441b2
Simplify AllowList constructor type annotation per PR feedback.
michaelsafyan Apr 8, 2025
5902b35
Reformat with ruff.
michaelsafyan Apr 8, 2025
a609776
Resolve lint error concerning too many returns.
michaelsafyan Apr 8, 2025
f92758b
Reformat with ruff.
michaelsafyan Apr 8, 2025
72bc998
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Apr 10, 2025
c8f033a
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Apr 15, 2025
676c251
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Apr 15, 2025
3458eab
Merge branch 'main' into google_genai_attribute_improvements
michaelsafyan Apr 17, 2025
49f7bd2
Update name to reflect requested changes in Semantic Conventions pull…
michaelsafyan Apr 17, 2025
58eae49
Add test to verify correct handling of Unicode.
michaelsafyan Apr 17, 2025
755e641
Merge branch 'open-telemetry:main' into google_genai_attribute_improv…
michaelsafyan Apr 21, 2025
79b082e
Reformat with ruff.
michaelsafyan Apr 22, 2025
b2a6cd1
Remove deuplicated test.
michaelsafyan Apr 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Add more request configuration options to the span attributes ([#3374](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3374))
- Restructure tests to keep in line with repository conventions ([#3344](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3344))

- Fix [bug](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3416) where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

Here are some TODO items required to achieve stability for this package:

- Add more span-level attributes for request configuration
- Add more span-level attributes for response information
- Verify and correct formatting of events:
- Including the 'role' field for message events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ classifiers = [
"Programming Language :: Python :: 3.12"
]
dependencies = [
"opentelemetry-api >=1.30.0, <2",
"opentelemetry-instrumentation >=0.51b0, <2",
"opentelemetry-semantic-conventions >=0.51b0, <2"
"opentelemetry-api >=1.31.1, <2",
"opentelemetry-instrumentation >=0.52b1, <2",
"opentelemetry-semantic-conventions >=0.52b1, <2"
]

[project.optional-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# 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 re
from typing import Iterable, Optional, Set

ALLOWED = True
DENIED = False


def _parse_env_list(s: str) -> Set[str]:
result = set()
for entry in s.split(","):
stripped_entry = entry.strip()
if not stripped_entry:
continue
result.add(stripped_entry)
return result


class _CompoundMatcher:
def __init__(self, entries: Set[str]):
self._match_all = "*" in entries
self._entries = entries
self._regex_matcher = None
regex_entries = []
for entry in entries:
if "*" not in entry:
continue
if entry == "*":
continue
entry = entry.replace("[", "\\[")
entry = entry.replace("]", "\\]")
entry = entry.replace(".", "\\.")
entry = entry.replace("*", ".*")
regex_entries.append(f"({entry})")
if regex_entries:
joined_regex = "|".join(regex_entries)
regex_str = f"^({joined_regex})$"
self._regex_matcher = re.compile(regex_str)

@property
def match_all(self):
return self._match_all

def matches(self, x):
if self._match_all:
return True
if x in self._entries:
return True
if (self._regex_matcher is not None) and (
self._regex_matcher.fullmatch(x)
):
return True
return False


class AllowList:
def __init__(
self,
includes: Optional[Iterable[str]] = None,
excludes: Optional[Iterable[str]] = None,
):
self._includes = _CompoundMatcher(set(includes or []))
self._excludes = _CompoundMatcher(set(excludes or []))
assert (not self._includes.match_all) or (
not self._excludes.match_all
), "Can't have '*' in both includes and excludes."

def allowed(self, x: str):
if self._excludes.match_all:
return self._includes.matches(x)
if self._includes.match_all:
return not self._excludes.matches(x)
return self._includes.matches(x) and not self._excludes.matches(x)

@staticmethod
def from_env(
includes_env_var: str, excludes_env_var: Optional[str] = None
):
includes = _parse_env_list(os.getenv(includes_env_var) or "")
excludes = set()
if excludes_env_var:
excludes = _parse_env_list(os.getenv(excludes_env_var) or "")
return AllowList(includes=includes, excludes=excludes)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.


# Semantic Convention still being defined in:
# https://github.com/open-telemetry/semantic-conventions/pull/2125
GCP_GENAI_OPERATION_CONFIG = "gcp.gen_ai.operation.config"
Loading