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

## Version 0.1b0 (2025-03-05)
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
@@ -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.


# Prefix to use for LLM model request attributes that are unique GCP
# (or that have not yet been formally defined in the GenAI/LLM SIG).
CUSTOM_LLM_REQUEST_PREFIX = "gen_ai.gcp.request"
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
# 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 json
from typing import Any, Callable, Dict, Optional, Sequence, Set, Union

Primitive = Union[bool, str, int, float]
BoolList = list[bool]
StringList = list[str]
IntList = list[int]
FloatList = list[float]
HomogenousPrimitiveList = Union[BoolList, StringList, IntList, FloatList]
FlattenedValue = Union[Primitive, HomogenousPrimitiveList]
FlattenedDict = Dict[str, FlattenedValue]


def _concat_key(prefix: Optional[str], suffix: str):
if not prefix:
return suffix
return f"{prefix}.{suffix}"


def _is_primitive(v):
for t in [str, bool, int, float]:
if isinstance(v, t):
return True
return False


def _is_homogenous_primitive_list(v):
if not isinstance(v, list):
return False
if len(v) == 0:
return True
if not _is_primitive(v[0]):
return False
first_entry_value_type = type(v[0])
for entry in v[1:]:
if not isinstance(entry, first_entry_value_type):
return False
return True


def _get_flatten_func(
flatten_functions: Dict[str, Callable], key_names: set[str]
):
for key in key_names:
flatten_func = flatten_functions.get(key)
if flatten_func is not None:
return flatten_func
return None


def _flatten_compound_value(
key: str,
value: Any,
exclude_keys: Set[str],
rename_keys: Dict[str, str],
flatten_functions: Dict[str, Callable],
key_names: Set[str],
_from_json=False,
) -> FlattenedDict:
flatten_func = _get_flatten_func(flatten_functions, key_names)
if flatten_func is not None:
func_output = flatten_func(
key,
value,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
if func_output is not None:
return {key: func_output}
if isinstance(value, dict):
return _flatten_dict(
value,
key_prefix=key,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
if isinstance(value, list):
if _is_homogenous_primitive_list(value):
return {key: value}
return _flatten_list(
value,
key_prefix=key,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
if hasattr(value, "model_dump"):
return _flatten_dict(
value.model_dump(),
key_prefix=key,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
if _from_json:
raise ValueError(
f"Cannot flatten value with key {key}; value: {value}"
)
json_value = json.loads(json.dumps(value))
return _flatten_value(
key,
json_value,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
_from_json=True,
)


def _flatten_value(
key: str,
value: Any,
exclude_keys: Set[str],
rename_keys: Dict[str, str],
flatten_functions: Dict[str, Callable],
_from_json=False,
) -> FlattenedDict:
if value is None:
return {}
key_names = set([key])
renamed_key = rename_keys.get(key)
if renamed_key is not None:
key_names.add(renamed_key)
key = renamed_key
if key_names & exclude_keys:
return {}
if _is_primitive(value):
return {key: value}
return _flatten_compound_value(
key=key,
value=value,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
key_names=key_names,
_from_json=_from_json,
)


def _flatten_dict(
d: Dict[str, Any],
key_prefix: str,
exclude_keys: Set[str],
rename_keys: Dict[str, str],
flatten_functions: Dict[str, Callable],
) -> FlattenedDict:
result = {}
for key, value in d.items():
if key in exclude_keys:
continue
full_key = _concat_key(key_prefix, key)
flattened = _flatten_value(
full_key,
value,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
result.update(flattened)
return result


def _flatten_list(
lst: list[Any],
key_prefix: str,
exclude_keys: Set[str],
rename_keys: Dict[str, str],
flatten_functions: Dict[str, Callable],
) -> FlattenedDict:
result = {}
result[_concat_key(key_prefix, "length")] = len(lst)
for index, value in enumerate(lst):
full_key = f"{key_prefix}[{index}]"
flattened = _flatten_value(
full_key,
value,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
result.update(flattened)
return result


def flatten_dict(
d: Dict[str, Any],
key_prefix: Optional[str] = None,
exclude_keys: Optional[Sequence[str]] = None,
rename_keys: Optional[Dict[str, str]] = None,
flatten_functions: Optional[Dict[str, Callable]] = None,
):
key_prefix = key_prefix or ""
if exclude_keys is None:
exclude_keys = set()
elif isinstance(exclude_keys, list):
exclude_keys = set(exclude_keys)
rename_keys = rename_keys or {}
flatten_functions = flatten_functions or {}
return _flatten_dict(
d,
key_prefix=key_prefix,
exclude_keys=exclude_keys,
rename_keys=rename_keys,
flatten_functions=flatten_functions,
)
Loading