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 .fsspec_hook import (
@@ -200,9 +200,6 @@ def test_upload(self):
200200
201201
202202class TestFsspecUploadHookIntegration (TestBase ):
203- def setUp (self ):
204- MemoryFileSystem .store .clear ()
205-
206203 def assert_fsspec_equal (self , path : str , value : str ) -> None :
207204 with fsspec .open (path , "r" ) as file :
208205 self .assertEqual (file .read (), value )
@@ -211,13 +208,67 @@ def test_upload_completions(self):
211208 hook = FsspecUploadHook (
212209 base_path = BASE_PATH ,
213210 )
211+ tracer = self .tracer_provider .get_tracer (__name__ )
212+ log_record = LogRecord ()
213+
214+ with tracer .start_as_current_span ("chat mymodel" ) as span :
215+ hook .upload (
216+ inputs = FAKE_INPUTS ,
217+ outputs = FAKE_OUTPUTS ,
218+ system_instruction = FAKE_SYSTEM_INSTRUCTION ,
219+ span = span ,
220+ log_record = log_record ,
221+ )
222+ hook .shutdown ()
223+
224+ finished_spans = self .get_finished_spans ()
225+ self .assertEqual (len (finished_spans ), 1 )
226+ span = finished_spans [0 ]
227+
228+ # span attributes, log attributes, and log body have refs
229+ for attributes in [
230+ span .attributes ,
231+ log_record .attributes ,
232+ ]:
233+ for ref_key in [
234+ "gen_ai.input.messages_ref" ,
235+ "gen_ai.output.messages_ref" ,
236+ "gen_ai.system_instructions_ref" ,
237+ ]:
238+ self .assertIn (ref_key , attributes )
239+
240+ self .assert_fsspec_equal (
241+ span .attributes ["gen_ai.input.messages_ref" ],
242+ '[{"role":"user","parts":[{"content":"What is the capital of France?","type":"text"}]}]' ,
243+ )
244+ self .assert_fsspec_equal (
245+ span .attributes ["gen_ai.output.messages_ref" ],
246+ '[{"role":"assistant","parts":[{"content":"Paris","type":"text"}],"finish_reason":"stop"}]' ,
247+ )
248+ self .assert_fsspec_equal (
249+ span .attributes ["gen_ai.system_instructions_ref" ],
250+ '[{"content":"You are a helpful assistant.","type":"text"}]' ,
251+ )
252+
253+ @staticmethod
254+ def upload_with_log (log_record : LogRecord ):
255+ hook = FsspecUploadHook (
256+ base_path = BASE_PATH ,
257+ )
258+
214259 hook .upload (
215260 inputs = FAKE_INPUTS ,
216261 outputs = FAKE_OUTPUTS ,
217262 system_instruction = FAKE_SYSTEM_INSTRUCTION ,
263+ log_record = log_record ,
218264 )
219265 hook .shutdown ()
220266
221- fs = fsspec .open (BASE_PATH ).fs
222- self .assertEqual (len (fs .ls (BASE_PATH )), 3 )
223- # TODO: test stamped telemetry
267+ def test_stamps_empty_log (self ):
268+ log_record = LogRecord ()
269+ self .upload_with_log (log_record )
270+
271+ # stamp on both body and attributes
272+ self .assertIn ("gen_ai.input.messages_ref" , log_record .attributes )
273+ self .assertIn ("gen_ai.output.messages_ref" , log_record .attributes )
274+ self .assertIn ("gen_ai.system_instructions_ref" , log_record .attributes )
0 commit comments