2626 set_span_data_for_response ,
2727 _capture_exception ,
2828 prepare_generate_content_args ,
29+ prepare_embed_content_args ,
30+ set_span_data_for_embed_request ,
31+ set_span_data_for_embed_response ,
2932)
3033from .streaming import (
3134 set_span_data_for_streaming_response ,
@@ -49,6 +52,7 @@ def setup_once():
4952 Models .generate_content_stream = _wrap_generate_content_stream (
5053 Models .generate_content_stream
5154 )
55+ Models .embed_content = _wrap_embed_content (Models .embed_content )
5256
5357 # Patch async methods
5458 AsyncModels .generate_content = _wrap_async_generate_content (
@@ -57,6 +61,7 @@ def setup_once():
5761 AsyncModels .generate_content_stream = _wrap_async_generate_content_stream (
5862 AsyncModels .generate_content_stream
5963 )
64+ AsyncModels .embed_content = _wrap_async_embed_content (AsyncModels .embed_content )
6065
6166
6267def _wrap_generate_content_stream (f ):
@@ -299,3 +304,73 @@ async def new_async_generate_content(self, *args, **kwargs):
299304 return response
300305
301306 return new_async_generate_content
307+
308+
309+ def _wrap_embed_content (f ):
310+ # type: (Callable[..., Any]) -> Callable[..., Any]
311+ @wraps (f )
312+ def new_embed_content (self , * args , ** kwargs ):
313+ # type: (Any, Any, Any) -> Any
314+ integration = sentry_sdk .get_client ().get_integration (GoogleGenAIIntegration )
315+ if integration is None :
316+ return f (self , * args , ** kwargs )
317+
318+ model_name , contents = prepare_embed_content_args (args , kwargs )
319+
320+ with sentry_sdk .start_span (
321+ op = OP .GEN_AI_EMBEDDINGS ,
322+ name = f"embeddings { model_name } " ,
323+ origin = ORIGIN ,
324+ ) as span :
325+ span .set_data (SPANDATA .GEN_AI_OPERATION_NAME , "embeddings" )
326+ span .set_data (SPANDATA .GEN_AI_SYSTEM , GEN_AI_SYSTEM )
327+ span .set_data (SPANDATA .GEN_AI_REQUEST_MODEL , model_name )
328+ set_span_data_for_embed_request (span , integration , contents , kwargs )
329+
330+ try :
331+ response = f (self , * args , ** kwargs )
332+ except Exception as exc :
333+ _capture_exception (exc )
334+ span .set_status (SPANSTATUS .INTERNAL_ERROR )
335+ raise
336+
337+ set_span_data_for_embed_response (span , integration , response )
338+
339+ return response
340+
341+ return new_embed_content
342+
343+
344+ def _wrap_async_embed_content (f ):
345+ # type: (Callable[..., Any]) -> Callable[..., Any]
346+ @wraps (f )
347+ async def new_async_embed_content (self , * args , ** kwargs ):
348+ # type: (Any, Any, Any) -> Any
349+ integration = sentry_sdk .get_client ().get_integration (GoogleGenAIIntegration )
350+ if integration is None :
351+ return await f (self , * args , ** kwargs )
352+
353+ model_name , contents = prepare_embed_content_args (args , kwargs )
354+
355+ with sentry_sdk .start_span (
356+ op = OP .GEN_AI_EMBEDDINGS ,
357+ name = f"embeddings { model_name } " ,
358+ origin = ORIGIN ,
359+ ) as span :
360+ span .set_data (SPANDATA .GEN_AI_OPERATION_NAME , "embeddings" )
361+ span .set_data (SPANDATA .GEN_AI_SYSTEM , GEN_AI_SYSTEM )
362+ span .set_data (SPANDATA .GEN_AI_REQUEST_MODEL , model_name )
363+ set_span_data_for_embed_request (span , integration , contents , kwargs )
364+
365+ try :
366+ response = await f (self , * args , ** kwargs )
367+ except Exception as exc :
368+ _capture_exception (exc )
369+ span .set_status (SPANSTATUS .INTERNAL_ERROR )
370+ raise
371+
372+ set_span_data_for_embed_response (span , integration , response )
373+
374+ return response
375+
376+ return new_async_embed_content
0 commit comments