|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import unittest |
| 17 | + |
| 18 | +import google.genai.types as genai_types |
| 19 | + |
| 20 | +from .base import TestCase |
| 21 | + |
| 22 | +class ToolCallInstrumentationTestCase(TestCase): |
| 23 | + |
| 24 | + def test_tool_calls_with_config_dict_outputs_spans(self): |
| 25 | + calls = [] |
| 26 | + def handle(*args, **kwargs): |
| 27 | + calls.append((args, kwargs)) |
| 28 | + return "some result" |
| 29 | + def somefunction(somearg): |
| 30 | + print("somearg=%s", somearg) |
| 31 | + self.mock_generate_content.side_effect = handle |
| 32 | + self.client.models.generate_content( |
| 33 | + model="some-model-name", |
| 34 | + contents="Some content", |
| 35 | + config={ |
| 36 | + "tools": [somefunction], |
| 37 | + }, |
| 38 | + ) |
| 39 | + self.assertEqual(len(calls), 1) |
| 40 | + config = calls[0][1]["config"] |
| 41 | + tools = config.tools |
| 42 | + wrapped_somefunction = tools[0] |
| 43 | + |
| 44 | + self.assertIsNone( |
| 45 | + self.otel.get_span_named("tool_call somefunction")) |
| 46 | + wrapped_somefunction(somearg="foo") |
| 47 | + self.otel.assert_has_span_named("tool_call somefunction") |
| 48 | + generated_span = self.otel.get_span_named("tool_call somefunction") |
| 49 | + self.assertEqual( |
| 50 | + generated_span.attributes["code.function.name"], |
| 51 | + "somefunction") |
| 52 | + |
| 53 | + def test_tool_calls_with_config_object_outputs_spans(self): |
| 54 | + calls = [] |
| 55 | + def handle(*args, **kwargs): |
| 56 | + calls.append((args, kwargs)) |
| 57 | + return "some result" |
| 58 | + def somefunction(somearg): |
| 59 | + print("somearg=%s", somearg) |
| 60 | + self.mock_generate_content.side_effect = handle |
| 61 | + self.client.models.generate_content( |
| 62 | + model="some-model-name", |
| 63 | + contents="Some content", |
| 64 | + config=genai_types.GenerateContentConfig( |
| 65 | + tools = [somefunction], |
| 66 | + ) |
| 67 | + ) |
| 68 | + self.assertEqual(len(calls), 1) |
| 69 | + config = calls[0][1]["config"] |
| 70 | + tools = config.tools |
| 71 | + wrapped_somefunction = tools[0] |
| 72 | + |
| 73 | + self.assertIsNone( |
| 74 | + self.otel.get_span_named("tool_call somefunction")) |
| 75 | + wrapped_somefunction(somearg="foo") |
| 76 | + self.otel.assert_has_span_named("tool_call somefunction") |
| 77 | + generated_span = self.otel.get_span_named("tool_call somefunction") |
| 78 | + self.assertEqual( |
| 79 | + generated_span.attributes["code.function.name"], |
| 80 | + "somefunction") |
| 81 | + |
| 82 | + def test_tool_calls_record_parameter_values_on_span_if_enabled(self): |
| 83 | + os.environ["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = "true" |
| 84 | + calls = [] |
| 85 | + def handle(*args, **kwargs): |
| 86 | + calls.append((args, kwargs)) |
| 87 | + return "some result" |
| 88 | + def somefunction(foo, bar=2): |
| 89 | + print("foo=%s, bar=%s", foo, bar) |
| 90 | + self.mock_generate_content.side_effect = handle |
| 91 | + self.client.models.generate_content( |
| 92 | + model="some-model-name", |
| 93 | + contents="Some content", |
| 94 | + config={ |
| 95 | + "tools": [somefunction], |
| 96 | + }, |
| 97 | + ) |
| 98 | + self.assertEqual(len(calls), 1) |
| 99 | + config = calls[0][1]["config"] |
| 100 | + tools = config.tools |
| 101 | + wrapped_somefunction = tools[0] |
| 102 | + wrapped_somefunction(123, bar="abc") |
| 103 | + self.otel.assert_has_span_named("tool_call somefunction") |
| 104 | + generated_span = self.otel.get_span_named("tool_call somefunction") |
| 105 | + self.assertEqual( |
| 106 | + generated_span.attributes["code.function.params.foo"], |
| 107 | + "123") |
| 108 | + self.assertEqual( |
| 109 | + generated_span.attributes["code.function.params.bar"], |
| 110 | + "'abc'") |
| 111 | + |
| 112 | + def test_tool_calls_do_not_record_parameter_values_if_not_enabled(self): |
| 113 | + os.environ["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = "false" |
| 114 | + calls = [] |
| 115 | + def handle(*args, **kwargs): |
| 116 | + calls.append((args, kwargs)) |
| 117 | + return "some result" |
| 118 | + def somefunction(foo, bar=2): |
| 119 | + print("foo=%s, bar=%s", foo, bar) |
| 120 | + self.mock_generate_content.side_effect = handle |
| 121 | + self.client.models.generate_content( |
| 122 | + model="some-model-name", |
| 123 | + contents="Some content", |
| 124 | + config={ |
| 125 | + "tools": [somefunction], |
| 126 | + }, |
| 127 | + ) |
| 128 | + self.assertEqual(len(calls), 1) |
| 129 | + config = calls[0][1]["config"] |
| 130 | + tools = config.tools |
| 131 | + wrapped_somefunction = tools[0] |
| 132 | + wrapped_somefunction(123, bar="abc") |
| 133 | + self.otel.assert_has_span_named("tool_call somefunction") |
| 134 | + generated_span = self.otel.get_span_named("tool_call somefunction") |
| 135 | + self.assertNotIn( |
| 136 | + "code.function.params.foo", generated_span.attributes) |
| 137 | + self.assertNotIn( |
| 138 | + "code.function.params.bar", generated_span.attributes) |
| 139 | + |
| 140 | + def test_tool_calls_record_return_values_on_span_if_enabled(self): |
| 141 | + os.environ["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = "true" |
| 142 | + calls = [] |
| 143 | + def handle(*args, **kwargs): |
| 144 | + calls.append((args, kwargs)) |
| 145 | + return "some result" |
| 146 | + def somefunction(x, y=2): |
| 147 | + return x + y |
| 148 | + self.mock_generate_content.side_effect = handle |
| 149 | + self.client.models.generate_content( |
| 150 | + model="some-model-name", |
| 151 | + contents="Some content", |
| 152 | + config={ |
| 153 | + "tools": [somefunction], |
| 154 | + }, |
| 155 | + ) |
| 156 | + self.assertEqual(len(calls), 1) |
| 157 | + config = calls[0][1]["config"] |
| 158 | + tools = config.tools |
| 159 | + wrapped_somefunction = tools[0] |
| 160 | + wrapped_somefunction(123) |
| 161 | + self.otel.assert_has_span_named("tool_call somefunction") |
| 162 | + generated_span = self.otel.get_span_named("tool_call somefunction") |
| 163 | + self.assertEqual( |
| 164 | + generated_span.attributes["code.function.return_value"], |
| 165 | + "125") |
| 166 | + |
| 167 | + def test_tool_calls_do_not_record_return_values_if_not_enabled(self): |
| 168 | + os.environ["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = "false" |
| 169 | + calls = [] |
| 170 | + def handle(*args, **kwargs): |
| 171 | + calls.append((args, kwargs)) |
| 172 | + return "some result" |
| 173 | + def somefunction(x, y=2): |
| 174 | + return x + y |
| 175 | + self.mock_generate_content.side_effect = handle |
| 176 | + self.client.models.generate_content( |
| 177 | + model="some-model-name", |
| 178 | + contents="Some content", |
| 179 | + config={ |
| 180 | + "tools": [somefunction], |
| 181 | + }, |
| 182 | + ) |
| 183 | + self.assertEqual(len(calls), 1) |
| 184 | + config = calls[0][1]["config"] |
| 185 | + tools = config.tools |
| 186 | + wrapped_somefunction = tools[0] |
| 187 | + wrapped_somefunction(123) |
| 188 | + self.otel.assert_has_span_named("tool_call somefunction") |
| 189 | + generated_span = self.otel.get_span_named("tool_call somefunction") |
| 190 | + self.assertNotIn( |
| 191 | + "code.function.return_value", generated_span.attributes) |
0 commit comments