|
15 | 15 | from unittest.mock import patch
|
16 | 16 |
|
17 | 17 | import google.genai.types as genai_types
|
| 18 | +from opentelemetry.instrumentation._semconv import ( |
| 19 | + _OpenTelemetrySemanticConventionStability, |
| 20 | + _OpenTelemetryStabilitySignalType, |
| 21 | + _StabilityMode, |
| 22 | +) |
| 23 | +from opentelemetry.util.genai.types import ContentCapturingMode |
18 | 24 |
|
19 | 25 | from .base import TestCase
|
20 | 26 |
|
@@ -275,3 +281,126 @@ def somefunction(x, y=2):
|
275 | 281 | self.assertNotIn(
|
276 | 282 | "code.function.return.value", generated_span.attributes
|
277 | 283 | )
|
| 284 | + |
| 285 | + def test_new_semconv_tool_calls_record_parameter_values(self): |
| 286 | + for mode in ContentCapturingMode: |
| 287 | + patched_environ = patch.dict( |
| 288 | + "os.environ", |
| 289 | + { |
| 290 | + "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT": mode.name, |
| 291 | + "OTEL_SEMCONV_STABILITY_OPT_IN": "gen_ai_latest_experimental", |
| 292 | + }, |
| 293 | + ) |
| 294 | + patched_otel_mapping = patch.dict( |
| 295 | + _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING, |
| 296 | + { |
| 297 | + _OpenTelemetryStabilitySignalType.GEN_AI: _StabilityMode.GEN_AI_LATEST_EXPERIMENTAL |
| 298 | + }, |
| 299 | + ) |
| 300 | + with self.subTest(f'mode: {mode}', patched_environ=patched_environ): |
| 301 | + self.setUp() |
| 302 | + with patched_environ, patched_otel_mapping: |
| 303 | + calls = [] |
| 304 | + |
| 305 | + def handle(*args, **kwargs): |
| 306 | + calls.append((args, kwargs)) |
| 307 | + return "some result" |
| 308 | + |
| 309 | + def somefunction(someparam, otherparam=2): |
| 310 | + print("someparam=%s, otherparam=%s", someparam, otherparam) |
| 311 | + |
| 312 | + self.mock_generate_content.side_effect = handle |
| 313 | + self.client.models.generate_content( |
| 314 | + model="some-model-name", |
| 315 | + contents="Some content", |
| 316 | + config={ |
| 317 | + "tools": [somefunction], |
| 318 | + }, |
| 319 | + ) |
| 320 | + self.assertEqual(len(calls), 1) |
| 321 | + config = calls[0][1]["config"] |
| 322 | + tools = config.tools |
| 323 | + wrapped_somefunction = tools[0] |
| 324 | + wrapped_somefunction(123, otherparam="abc") |
| 325 | + self.otel.assert_has_span_named("execute_tool somefunction") |
| 326 | + generated_span = self.otel.get_span_named("execute_tool somefunction") |
| 327 | + self.assertEqual( |
| 328 | + generated_span.attributes[ |
| 329 | + "code.function.parameters.someparam.type" |
| 330 | + ], |
| 331 | + "int", |
| 332 | + ) |
| 333 | + self.assertEqual( |
| 334 | + generated_span.attributes[ |
| 335 | + "code.function.parameters.otherparam.type" |
| 336 | + ], |
| 337 | + "str", |
| 338 | + ) |
| 339 | + if mode in [ |
| 340 | + ContentCapturingMode.SPAN_ONLY, |
| 341 | + ContentCapturingMode.SPAN_AND_EVENT, |
| 342 | + ]: |
| 343 | + self.assertEqual(generated_span.attributes["code.function.parameters.someparam.value"], 123) |
| 344 | + self.assertEqual(generated_span.attributes["code.function.parameters.otherparam.value"], "abc") |
| 345 | + else: |
| 346 | + self.assertNotIn("code.function.parameters.someparam.value", generated_span.attributes) |
| 347 | + self.assertNotIn("code.function.parameters.otherparam.value", generated_span.attributes) |
| 348 | + self.tearDown() |
| 349 | + |
| 350 | + def test_new_semconv_tool_calls_record_return_values(self): |
| 351 | + for mode in ContentCapturingMode: |
| 352 | + patched_environ = patch.dict( |
| 353 | + "os.environ", |
| 354 | + { |
| 355 | + "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT": mode.name, |
| 356 | + "OTEL_SEMCONV_STABILITY_OPT_IN": "gen_ai_latest_experimental", |
| 357 | + }, |
| 358 | + ) |
| 359 | + patched_otel_mapping = patch.dict( |
| 360 | + _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING, |
| 361 | + { |
| 362 | + _OpenTelemetryStabilitySignalType.GEN_AI: _StabilityMode.GEN_AI_LATEST_EXPERIMENTAL |
| 363 | + }, |
| 364 | + ) |
| 365 | + with self.subTest(f'mode: {mode}', patched_environ=patched_environ): |
| 366 | + self.setUp() |
| 367 | + with patched_environ, patched_otel_mapping: |
| 368 | + calls = [] |
| 369 | + |
| 370 | + def handle(*args, **kwargs): |
| 371 | + calls.append((args, kwargs)) |
| 372 | + return "some result" |
| 373 | + |
| 374 | + def somefunction(x, y=2): |
| 375 | + return x + y |
| 376 | + |
| 377 | + self.mock_generate_content.side_effect = handle |
| 378 | + self.client.models.generate_content( |
| 379 | + model="some-model-name", |
| 380 | + contents="Some content", |
| 381 | + config={ |
| 382 | + "tools": [somefunction], |
| 383 | + }, |
| 384 | + ) |
| 385 | + self.assertEqual(len(calls), 1) |
| 386 | + config = calls[0][1]["config"] |
| 387 | + tools = config.tools |
| 388 | + wrapped_somefunction = tools[0] |
| 389 | + wrapped_somefunction(123) |
| 390 | + self.otel.assert_has_span_named("execute_tool somefunction") |
| 391 | + generated_span = self.otel.get_span_named("execute_tool somefunction") |
| 392 | + self.assertEqual( |
| 393 | + generated_span.attributes["code.function.return.type"], "int" |
| 394 | + ) |
| 395 | + if mode in [ |
| 396 | + ContentCapturingMode.SPAN_ONLY, |
| 397 | + ContentCapturingMode.SPAN_AND_EVENT, |
| 398 | + ]: |
| 399 | + self.assertIn( |
| 400 | + "code.function.return.value", generated_span.attributes |
| 401 | + ) |
| 402 | + else: |
| 403 | + self.assertNotIn( |
| 404 | + "code.function.return.value", generated_span.attributes |
| 405 | + ) |
| 406 | + self.tearDown() |
0 commit comments