@@ -353,57 +353,66 @@ async def _create_gemini_cache(
353353 Returns:
354354 Cache metadata with precise creation timestamp
355355 """
356- # Prepare cache contents (first N contents + system instruction + tools)
357- cache_contents = llm_request .contents [:cache_contents_count ]
358-
359- cache_config = types .CreateCachedContentConfig (
360- contents = cache_contents ,
361- ttl = llm_request .cache_config .ttl_string ,
362- display_name = (
363- f"adk-cache-{ int (time .time ())} -{ cache_contents_count } contents"
364- ),
365- )
366-
367- # Add system instruction if present
368- if llm_request .config and llm_request .config .system_instruction :
369- cache_config .system_instruction = llm_request .config .system_instruction
370- logger .debug (
371- "Added system instruction to cache config (length=%d)" ,
372- len (llm_request .config .system_instruction ),
356+ from ..telemetry .tracing import tracer
357+
358+ with tracer .start_as_current_span ("create_cache" ) as span :
359+ # Prepare cache contents (first N contents + system instruction + tools)
360+ cache_contents = llm_request .contents [:cache_contents_count ]
361+
362+ cache_config = types .CreateCachedContentConfig (
363+ contents = cache_contents ,
364+ ttl = llm_request .cache_config .ttl_string ,
365+ display_name = (
366+ f"adk-cache-{ int (time .time ())} -{ cache_contents_count } contents"
367+ ),
373368 )
374369
375- # Add tools if present
376- if llm_request .config and llm_request .config .tools :
377- cache_config .tools = llm_request .config .tools
370+ # Add system instruction if present
371+ if llm_request .config and llm_request .config .system_instruction :
372+ cache_config .system_instruction = llm_request .config .system_instruction
373+ logger .debug (
374+ "Added system instruction to cache config (length=%d)" ,
375+ len (llm_request .config .system_instruction ),
376+ )
378377
379- # Add tool config if present
380- if llm_request .config and llm_request .config .tool_config :
381- cache_config .tool_config = llm_request .config .tool_config
378+ # Add tools if present
379+ if llm_request .config and llm_request .config .tools :
380+ cache_config .tools = llm_request .config .tools
382381
383- logger .debug (
384- "Creating cache with model %s and config: %s" ,
385- llm_request .model ,
386- cache_config ,
387- )
388- cached_content = await self .genai_client .aio .caches .create (
389- model = llm_request .model ,
390- config = cache_config ,
391- )
392- # Set precise creation timestamp right after cache creation
393- created_at = time .time ()
394- logger .info ("Cache created successfully: %s" , cached_content .name )
382+ # Add tool config if present
383+ if llm_request .config and llm_request .config .tool_config :
384+ cache_config .tool_config = llm_request .config .tool_config
395385
396- # Return complete cache metadata with precise timing
397- return CacheMetadata (
398- cache_name = cached_content .name ,
399- expire_time = created_at + llm_request .cache_config .ttl_seconds ,
400- fingerprint = self ._generate_cache_fingerprint (
401- llm_request , cache_contents_count
402- ),
403- invocations_used = 1 ,
404- contents_count = cache_contents_count ,
405- created_at = created_at ,
406- )
386+ span .set_attribute ("cache_contents_count" , cache_contents_count )
387+ span .set_attribute ("model" , llm_request .model )
388+ span .set_attribute ("ttl_seconds" , llm_request .cache_config .ttl_seconds )
389+
390+ logger .debug (
391+ "Creating cache with model %s and config: %s" ,
392+ llm_request .model ,
393+ cache_config ,
394+ )
395+ cached_content = await self .genai_client .aio .caches .create (
396+ model = llm_request .model ,
397+ config = cache_config ,
398+ )
399+ # Set precise creation timestamp right after cache creation
400+ created_at = time .time ()
401+ logger .info ("Cache created successfully: %s" , cached_content .name )
402+
403+ span .set_attribute ("cache_name" , cached_content .name )
404+
405+ # Return complete cache metadata with precise timing
406+ return CacheMetadata (
407+ cache_name = cached_content .name ,
408+ expire_time = created_at + llm_request .cache_config .ttl_seconds ,
409+ fingerprint = self ._generate_cache_fingerprint (
410+ llm_request , cache_contents_count
411+ ),
412+ invocations_used = 1 ,
413+ contents_count = cache_contents_count ,
414+ created_at = created_at ,
415+ )
407416
408417 async def cleanup_cache (self , cache_name : str ) -> None :
409418 """Clean up cache by deleting it.
0 commit comments