Skip to content

Commit da63c96

Browse files
authored
Fix overwritten log attributes in vertexai instrumentation (#3925)
1 parent fecd8af commit da63c96

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
9+
- Fix overwritten log attributes in vertexai instrumentation
10+
([#3925](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3925))
911

1012
## Version 2.1b0 (2025-10-16)
1113

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/patch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ def handle_response(
172172
| prediction_service_v1beta1.GenerateContentResponse
173173
| None,
174174
) -> None:
175-
event = LogRecord(
176-
event_name="gen_ai.client.inference.operation.details",
177-
)
178175
attributes = (
179176
get_server_attributes(instance.api_endpoint) # type: ignore[reportUnknownMemberType]
180177
| request_attributes
181178
| get_genai_response_attributes(response)
182179
)
180+
event = LogRecord(
181+
event_name="gen_ai.client.inference.operation.details",
182+
)
183+
event.attributes = attributes.copy()
183184
system_instructions, inputs, outputs = [], [], []
184185
if params.system_instruction:
185186
system_instructions = convert_content_to_message_parts(
@@ -237,7 +238,6 @@ def handle_response(
237238
for k, v in content_attributes.items()
238239
}
239240
)
240-
event.attributes = attributes
241241
if capture_content in (
242242
ContentCapturingMode.SPAN_AND_EVENT,
243243
ContentCapturingMode.EVENT_ONLY,

instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/test_function_calling_experimental.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ def test_tool_events_with_completion_hook(
274274
assert len(logs) == 1
275275
# File upload takes a few seconds sometimes.
276276
time.sleep(3)
277+
278+
# Both log and span have the same reference attributes from upload hook
279+
for key in "gen_ai.input.messages_ref", "gen_ai.output.messages_ref":
280+
assert spans[0].attributes.get(key)
281+
assert logs[0].log_record.attributes.get(key)
282+
283+
assert spans[0].attributes[key] == logs[0].log_record.attributes[key]
284+
277285
assert_fsspec_equal(
278286
spans[0].attributes["gen_ai.output.messages_ref"],
279287
[

0 commit comments

Comments
 (0)