Skip to content

Commit 26256f2

Browse files
committed
Fix overwritten log attributes in vertexai instrumentation
1 parent fecd8af commit 26256f2

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import time
32
from typing import Any
43

54
import fsspec
@@ -274,6 +273,14 @@ def test_tool_events_with_completion_hook(
274273
assert len(logs) == 1
275274
# File upload takes a few seconds sometimes.
276275
time.sleep(3)
276+
277+
# Both log and span have the same reference attributes from upload hook
278+
for key in "gen_ai.input.messages_ref", "gen_ai.output.messages_ref":
279+
assert spans[0].attributes.get(key) is not None
280+
assert logs[0].log_record.attributes.get(key) is not None
281+
282+
assert spans[0].attributes[key] == logs[0].log_record.attributes[key]
283+
277284
assert_fsspec_equal(
278285
spans[0].attributes["gen_ai.output.messages_ref"],
279286
[

0 commit comments

Comments
 (0)