Skip to content

Commit 057d956

Browse files
committed
test: fix event recording test.
1 parent 6276543 commit 057d956

File tree

1 file changed

+30
-13
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content

1 file changed

+30
-13
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/nonstreaming_base.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
from unittest.mock import patch
1818

1919
from google.genai.types import GenerateContentConfig
20+
from opentelemetry._events import Event
2021
from opentelemetry.instrumentation._semconv import (
2122
_OpenTelemetrySemanticConventionStability,
2223
_OpenTelemetryStabilitySignalType,
2324
_StabilityMode,
2425
)
26+
from opentelemetry.semconv._incubating.attributes import (
27+
gen_ai_attributes,
28+
)
2529
from opentelemetry.util.genai.types import ContentCapturingMode
2630

2731
from .base import TestCase
@@ -204,20 +208,33 @@ def test_new_semconv_record_completion_as_log(self):
204208
_OpenTelemetryStabilitySignalType.GEN_AI: _StabilityMode.GEN_AI_LATEST_EXPERIMENTAL
205209
},
206210
)
207-
with self.subTest(f'mode: {mode}', patched_environ=patched_environ):
211+
content = "Some input"
212+
output = "Some response content"
213+
sys_instr = "System instruction"
214+
with self.subTest(f"mode: {mode}", patched_environ=patched_environ):
208215
self.setUp()
209216
with patched_environ, patched_otel_mapping:
210-
self.configure_valid_response(text="Some response content")
211-
self.generate_content(model="gemini-2.0-flash", contents="Some input")
212-
217+
self.configure_valid_response(text=output)
218+
self.generate_content(model="gemini-2.0-flash", contents=content, config=GenerateContentConfig(system_instruction=sys_instr))
219+
self.otel.assert_has_event_named("gen_ai.client.inference.operation.details")
220+
event = self.otel.get_event_named("gen_ai.client.inference.operation.details")
213221
if mode in [
214222
ContentCapturingMode.NO_CONTENT,
215223
ContentCapturingMode.SPAN_ONLY,
216224
]:
217-
self.otel.assert_does_not_have_event_named("gen_ai.client.inference.operation.details")
225+
self.assertNotIn(gen_ai_attributes.GEN_AI_INPUT_MESSAGES, event.attributes)
226+
self.assertNotIn(gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES, event.attributes)
227+
self.assertNotIn(gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS, event.attributes)
218228
else:
219-
self.otel.assert_has_event_named("gen_ai.client.inference.operation.details")
220-
229+
attrs = {
230+
gen_ai_attributes.GEN_AI_INPUT_MESSAGES: ({"role": "user", "parts": ({"content": content, "type": "text"},)},),
231+
gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES: ({"role": "assistant", "parts": ({"content": output, "type": "text"},), "finish_reason": ""},),
232+
gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS: ({"content": sys_instr, "type": "text"},)
233+
}
234+
expected_event = Event("gen_ai.client.inference.operation.details", attributes=attrs)
235+
self.assertEqual(event.attributes[gen_ai_attributes.GEN_AI_INPUT_MESSAGES], expected_event.attributes[gen_ai_attributes.GEN_AI_INPUT_MESSAGES])
236+
self.assertEqual(event.attributes[gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES], expected_event.attributes[gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES])
237+
self.assertEqual(event.attributes[gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS], expected_event.attributes[gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS])
221238
self.tearDown()
222239

223240
def test_new_semconv_record_completion_in_span(self):
@@ -245,13 +262,13 @@ def test_new_semconv_record_completion_in_span(self):
245262
ContentCapturingMode.SPAN_ONLY,
246263
ContentCapturingMode.SPAN_AND_EVENT,
247264
]:
248-
self.assertEqual(span.attributes["gen_ai.input.messages"], '[{"role": "user", "parts": [{"content": "Some input", "type": "text"}]}]')
249-
self.assertEqual(span.attributes["gen_ai.output.messages"], '[{"role": "assistant", "parts": [{"content": "Some response content", "type": "text"}], "finish_reason": ""}]')
250-
self.assertEqual(span.attributes["gen_ai.system_instructions"], '[{"content": "System instruction", "type": "text"}]')
265+
self.assertEqual(span.attributes[gen_ai_attributes.GEN_AI_INPUT_MESSAGES], '[{"role": "user", "parts": [{"content": "Some input", "type": "text"}]}]')
266+
self.assertEqual(span.attributes[gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES], '[{"role": "assistant", "parts": [{"content": "Some response content", "type": "text"}], "finish_reason": ""}]')
267+
self.assertEqual(span.attributes[gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS], '[{"content": "System instruction", "type": "text"}]')
251268
else:
252-
self.assertNotIn("gen_ai.input.messages", span.attributes)
253-
self.assertNotIn("gen_ai.output.messages", span.attributes)
254-
self.assertNotIn("gen_ai.system_instructions", span.attributes)
269+
self.assertNotIn(gen_ai_attributes.GEN_AI_INPUT_MESSAGES, span.attributes)
270+
self.assertNotIn(gen_ai_attributes.GEN_AI_OUTPUT_MESSAGES, span.attributes)
271+
self.assertNotIn(gen_ai_attributes.GEN_AI_SYSTEM_INSTRUCTIONS, span.attributes)
255272

256273
self.tearDown()
257274

0 commit comments

Comments
 (0)