2525from unittest .mock import MagicMock , patch
2626
2727import fsspec
28- from fsspec .implementations .memory import MemoryFileSystem
2928
29+ from opentelemetry ._logs import LogRecord
3030from opentelemetry .test .test_base import TestBase
3131from opentelemetry .util .genai import types
3232from opentelemetry .util .genai ._fsspec_upload import (
@@ -195,9 +195,6 @@ def test_upload(self):
195195
196196
197197class TestFsspecUploadHookIntegration (TestBase ):
198- def setUp (self ):
199- MemoryFileSystem .store .clear ()
200-
201198 def assert_fsspec_equal (self , path : str , value : str ) -> None :
202199 with fsspec .open (path , "r" ) as file :
203200 self .assertEqual (file .read (), value )
@@ -207,12 +204,87 @@ def test_upload_completions(self):
207204 uploader = FsspecUploader (),
208205 base_path = BASE_PATH ,
209206 )
207+ tracer = self .tracer_provider .get_tracer (__name__ )
208+ log_record = LogRecord ()
209+
210+ with tracer .start_as_current_span ("chat mymodel" ) as span :
211+ hook .upload (
212+ inputs = FAKE_INPUTS ,
213+ outputs = FAKE_OUTPUTS ,
214+ system_instruction = FAKE_SYSTEM_INSTRUCTION ,
215+ span = span ,
216+ log_record = log_record ,
217+ )
218+ hook .shutdown ()
219+
220+ finished_spans = self .get_finished_spans ()
221+ self .assertEqual (len (finished_spans ), 1 )
222+ span = finished_spans [0 ]
223+
224+ # span attributes, log attributes, and log body have refs
225+ for attributes in [
226+ span .attributes ,
227+ log_record .attributes ,
228+ log_record .body ,
229+ ]:
230+ for ref_key in [
231+ "gen_ai.input.messages_ref" ,
232+ "gen_ai.output.messages_ref" ,
233+ "gen_ai.system_instructions_ref" ,
234+ ]:
235+ self .assertIn (ref_key , attributes )
236+
237+ self .assert_fsspec_equal (
238+ span .attributes ["gen_ai.input.messages_ref" ],
239+ '[{"role":"user","parts":[{"content":"What is the capital of France?","type":"text"}]}]' ,
240+ )
241+ self .assert_fsspec_equal (
242+ span .attributes ["gen_ai.output.messages_ref" ],
243+ '[{"role":"assistant","parts":[{"content":"Paris","type":"text"}],"finish_reason":"stop"}]' ,
244+ )
245+ self .assert_fsspec_equal (
246+ span .attributes ["gen_ai.system_instructions_ref" ],
247+ '[{"content":"You are a helpful assistant.","type":"text"}]' ,
248+ )
249+
250+ @staticmethod
251+ def upload_with_log (log_record : LogRecord ):
252+ hook = FsspecUploadHook (
253+ uploader = FsspecUploader (),
254+ base_path = BASE_PATH ,
255+ )
256+
210257 hook .upload (
211258 inputs = FAKE_INPUTS ,
212259 outputs = FAKE_OUTPUTS ,
213260 system_instruction = FAKE_SYSTEM_INSTRUCTION ,
261+ log_record = log_record ,
214262 )
215-
216- fs = fsspec .open (BASE_PATH ).fs
217- self .assertEqual (len (fs .ls (BASE_PATH )), 3 )
218- # TODO: test stamped telemetry
263+ hook .shutdown ()
264+
265+ def test_stamps_empty_log (self ):
266+ log_record = LogRecord ()
267+ self .upload_with_log (log_record )
268+
269+ # stamp on both body and attributes
270+ self .assertIn ("gen_ai.input.messages_ref" , log_record .attributes )
271+ self .assertIn ("gen_ai.output.messages_ref" , log_record .attributes )
272+ self .assertIn ("gen_ai.system_instructions_ref" , log_record .attributes )
273+ self .assertIn ("gen_ai.input.messages_ref" , log_record .body )
274+ self .assertIn ("gen_ai.output.messages_ref" , log_record .body )
275+ self .assertIn ("gen_ai.system_instructions_ref" , log_record .body )
276+
277+ def test_stamps_log_with_map_body (self ):
278+ log_record = LogRecord (body = {"hello" : "world" })
279+ self .upload_with_log (log_record )
280+
281+ # stamp on both body and attributes, preserving existing
282+ self .assertEqual (log_record .body ["hello" ], "world" )
283+ self .assertIn ("gen_ai.input.messages_ref" , log_record .body )
284+ self .assertIn ("gen_ai.output.messages_ref" , log_record .body )
285+ self .assertIn ("gen_ai.system_instructions_ref" , log_record .body )
286+
287+ def test_doesnt_stamp_log_string_body (self ):
288+ log_record = LogRecord (body = "hello world" )
289+ self .upload_with_log (log_record )
290+ self .assertEqual (log_record .body , "hello world" )
0 commit comments