|
14 | 14 |
|
15 | 15 |
|
16 | 16 | # pylint: disable=import-outside-toplevel,no-name-in-module |
17 | | - |
18 | 17 | import importlib |
19 | 18 | import logging |
20 | 19 | import sys |
|
25 | 24 | from unittest.mock import MagicMock, patch |
26 | 25 |
|
27 | 26 | import fsspec |
28 | | -from fsspec.implementations.memory import MemoryFileSystem |
29 | 27 |
|
| 28 | +from opentelemetry._logs import LogRecord |
30 | 29 | from opentelemetry.test.test_base import TestBase |
31 | 30 | from opentelemetry.util.genai import types |
32 | 31 | from opentelemetry.util.genai._fsspec_upload.fsspec_hook import ( |
@@ -201,23 +200,93 @@ def test_upload(self): |
201 | 200 |
|
202 | 201 | class TestFsspecUploadHookIntegration(TestBase): |
203 | 202 | def setUp(self): |
204 | | - MemoryFileSystem.store.clear() |
| 203 | + super().setUp() |
| 204 | + self.hook = FsspecUploadHook(base_path=BASE_PATH) |
| 205 | + |
| 206 | + def tearDown(self): |
| 207 | + super().tearDown() |
| 208 | + self.hook.shutdown() |
205 | 209 |
|
206 | 210 | def assert_fsspec_equal(self, path: str, value: str) -> None: |
207 | 211 | with fsspec.open(path, "r") as file: |
208 | 212 | self.assertEqual(file.read(), value) |
209 | 213 |
|
210 | 214 | def test_upload_completions(self): |
211 | | - hook = FsspecUploadHook( |
212 | | - base_path=BASE_PATH, |
| 215 | + tracer = self.tracer_provider.get_tracer(__name__) |
| 216 | + log_record = LogRecord() |
| 217 | + |
| 218 | + with tracer.start_as_current_span("chat mymodel") as span: |
| 219 | + self.hook.upload( |
| 220 | + inputs=FAKE_INPUTS, |
| 221 | + outputs=FAKE_OUTPUTS, |
| 222 | + system_instruction=FAKE_SYSTEM_INSTRUCTION, |
| 223 | + span=span, |
| 224 | + log_record=log_record, |
| 225 | + ) |
| 226 | + self.hook.shutdown() |
| 227 | + |
| 228 | + finished_spans = self.get_finished_spans() |
| 229 | + self.assertEqual(len(finished_spans), 1) |
| 230 | + span = finished_spans[0] |
| 231 | + |
| 232 | + # span attributes, log attributes, and log body have refs |
| 233 | + for attributes in [ |
| 234 | + span.attributes, |
| 235 | + log_record.attributes, |
| 236 | + ]: |
| 237 | + for ref_key in [ |
| 238 | + "gen_ai.input.messages_ref", |
| 239 | + "gen_ai.output.messages_ref", |
| 240 | + "gen_ai.system_instructions_ref", |
| 241 | + ]: |
| 242 | + self.assertIn(ref_key, attributes) |
| 243 | + |
| 244 | + self.assert_fsspec_equal( |
| 245 | + span.attributes["gen_ai.input.messages_ref"], |
| 246 | + '[{"role":"user","parts":[{"content":"What is the capital of France?","type":"text"}]}]', |
213 | 247 | ) |
214 | | - hook.upload( |
| 248 | + self.assert_fsspec_equal( |
| 249 | + span.attributes["gen_ai.output.messages_ref"], |
| 250 | + '[{"role":"assistant","parts":[{"content":"Paris","type":"text"}],"finish_reason":"stop"}]', |
| 251 | + ) |
| 252 | + self.assert_fsspec_equal( |
| 253 | + span.attributes["gen_ai.system_instructions_ref"], |
| 254 | + '[{"content":"You are a helpful assistant.","type":"text"}]', |
| 255 | + ) |
| 256 | + |
| 257 | + def test_stamps_empty_log(self): |
| 258 | + log_record = LogRecord() |
| 259 | + self.hook.upload( |
215 | 260 | inputs=FAKE_INPUTS, |
216 | 261 | outputs=FAKE_OUTPUTS, |
217 | 262 | system_instruction=FAKE_SYSTEM_INSTRUCTION, |
| 263 | + log_record=log_record, |
| 264 | + ) |
| 265 | + |
| 266 | + # stamp on both body and attributes |
| 267 | + self.assertIn("gen_ai.input.messages_ref", log_record.attributes) |
| 268 | + self.assertIn("gen_ai.output.messages_ref", log_record.attributes) |
| 269 | + self.assertIn("gen_ai.system_instructions_ref", log_record.attributes) |
| 270 | + |
| 271 | + def test_upload_bytes(self) -> None: |
| 272 | + log_record = LogRecord() |
| 273 | + self.hook.upload( |
| 274 | + inputs=[ |
| 275 | + types.InputMessage( |
| 276 | + role="user", |
| 277 | + parts=[ |
| 278 | + types.Text(content="What is the capital of France?"), |
| 279 | + {"type": "generic_bytes", "bytes": b"hello"}, |
| 280 | + ], |
| 281 | + ) |
| 282 | + ], |
| 283 | + outputs=FAKE_OUTPUTS, |
| 284 | + system_instruction=FAKE_SYSTEM_INSTRUCTION, |
| 285 | + log_record=log_record, |
218 | 286 | ) |
219 | | - hook.shutdown() |
| 287 | + self.hook.shutdown() |
220 | 288 |
|
221 | | - fs = fsspec.open(BASE_PATH).fs |
222 | | - self.assertEqual(len(fs.ls(BASE_PATH)), 3) |
223 | | - # TODO: test stamped telemetry |
| 289 | + self.assert_fsspec_equal( |
| 290 | + log_record.attributes["gen_ai.input.messages_ref"], |
| 291 | + '[{"role":"user","parts":[{"content":"What is the capital of France?","type":"text"},{"type":"generic_bytes","bytes":"aGVsbG8="}]}]', |
| 292 | + ) |
0 commit comments