openlit 1.33.21__tar.gz → 1.33.23__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. {openlit-1.33.21 → openlit-1.33.23}/PKG-INFO +1 -1
  2. {openlit-1.33.21 → openlit-1.33.23}/pyproject.toml +1 -1
  3. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/google_ai_studio/__init__.py +21 -6
  4. openlit-1.33.23/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py +159 -0
  5. openlit-1.33.23/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py +159 -0
  6. openlit-1.33.23/src/openlit/instrumentation/google_ai_studio/utils.py +249 -0
  7. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/transformers/transformers.py +1 -2
  8. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/transformers/utils.py +3 -3
  9. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/otel/metrics.py +5 -0
  10. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/semcov/__init__.py +2 -0
  11. openlit-1.33.21/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py +0 -227
  12. openlit-1.33.21/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py +0 -227
  13. {openlit-1.33.21 → openlit-1.33.23}/LICENSE +0 -0
  14. {openlit-1.33.21 → openlit-1.33.23}/README.md +0 -0
  15. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/__helpers.py +0 -0
  16. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/__init__.py +0 -0
  17. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/evals/__init__.py +0 -0
  18. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/evals/all.py +0 -0
  19. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/evals/bias_detection.py +0 -0
  20. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/evals/hallucination.py +0 -0
  21. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/evals/toxicity.py +0 -0
  22. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/evals/utils.py +0 -0
  23. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/guard/__init__.py +0 -0
  24. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/guard/all.py +0 -0
  25. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/guard/prompt_injection.py +0 -0
  26. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/guard/restrict_topic.py +0 -0
  27. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/guard/sensitive_topic.py +0 -0
  28. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/guard/utils.py +0 -0
  29. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ag2/__init__.py +0 -0
  30. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ag2/ag2.py +0 -0
  31. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ai21/__init__.py +0 -0
  32. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ai21/ai21.py +0 -0
  33. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ai21/async_ai21.py +0 -0
  34. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ai21/utils.py +0 -0
  35. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/anthropic/__init__.py +0 -0
  36. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/anthropic/anthropic.py +0 -0
  37. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/anthropic/async_anthropic.py +0 -0
  38. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/anthropic/utils.py +0 -0
  39. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/assemblyai/__init__.py +0 -0
  40. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/assemblyai/assemblyai.py +0 -0
  41. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/astra/__init__.py +0 -0
  42. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/astra/astra.py +0 -0
  43. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/astra/async_astra.py +0 -0
  44. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/astra/utils.py +0 -0
  45. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/azure_ai_inference/__init__.py +0 -0
  46. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py +0 -0
  47. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py +0 -0
  48. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/azure_ai_inference/utils.py +0 -0
  49. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/bedrock/__init__.py +0 -0
  50. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/bedrock/bedrock.py +0 -0
  51. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/bedrock/utils.py +0 -0
  52. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/chroma/__init__.py +0 -0
  53. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/chroma/chroma.py +0 -0
  54. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/cohere/__init__.py +0 -0
  55. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/cohere/async_cohere.py +0 -0
  56. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/cohere/cohere.py +0 -0
  57. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/controlflow/__init__.py +0 -0
  58. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/controlflow/controlflow.py +0 -0
  59. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/crawl4ai/__init__.py +0 -0
  60. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/crawl4ai/async_crawl4ai.py +0 -0
  61. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/crawl4ai/crawl4ai.py +0 -0
  62. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/crewai/__init__.py +0 -0
  63. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/crewai/crewai.py +0 -0
  64. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/dynamiq/__init__.py +0 -0
  65. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/dynamiq/dynamiq.py +0 -0
  66. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/elevenlabs/__init__.py +0 -0
  67. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py +0 -0
  68. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/elevenlabs/elevenlabs.py +0 -0
  69. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/embedchain/__init__.py +0 -0
  70. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/embedchain/embedchain.py +0 -0
  71. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/firecrawl/__init__.py +0 -0
  72. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/firecrawl/firecrawl.py +0 -0
  73. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/gpt4all/__init__.py +0 -0
  74. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/gpt4all/gpt4all.py +0 -0
  75. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/gpu/__init__.py +0 -0
  76. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/groq/__init__.py +0 -0
  77. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/groq/async_groq.py +0 -0
  78. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/groq/groq.py +0 -0
  79. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/haystack/__init__.py +0 -0
  80. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/haystack/haystack.py +0 -0
  81. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/julep/__init__.py +0 -0
  82. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/julep/async_julep.py +0 -0
  83. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/julep/julep.py +0 -0
  84. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/langchain/__init__.py +0 -0
  85. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/langchain/async_langchain.py +0 -0
  86. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/langchain/langchain.py +0 -0
  87. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/letta/__init__.py +0 -0
  88. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/letta/letta.py +0 -0
  89. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/litellm/__init__.py +0 -0
  90. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/litellm/async_litellm.py +0 -0
  91. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/litellm/litellm.py +0 -0
  92. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/llamaindex/__init__.py +0 -0
  93. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/llamaindex/llamaindex.py +0 -0
  94. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/mem0/__init__.py +0 -0
  95. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/mem0/mem0.py +0 -0
  96. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/milvus/__init__.py +0 -0
  97. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/milvus/milvus.py +0 -0
  98. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/mistral/__init__.py +0 -0
  99. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/mistral/async_mistral.py +0 -0
  100. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/mistral/mistral.py +0 -0
  101. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/multion/__init__.py +0 -0
  102. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/multion/async_multion.py +0 -0
  103. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/multion/multion.py +0 -0
  104. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ollama/__init__.py +0 -0
  105. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ollama/async_ollama.py +0 -0
  106. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ollama/ollama.py +0 -0
  107. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/ollama/utils.py +0 -0
  108. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/openai/__init__.py +0 -0
  109. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/openai/async_openai.py +0 -0
  110. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/openai/openai.py +0 -0
  111. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/openai_agents/__init__.py +0 -0
  112. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/openai_agents/openai_agents.py +0 -0
  113. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/phidata/__init__.py +0 -0
  114. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/phidata/phidata.py +0 -0
  115. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/pinecone/__init__.py +0 -0
  116. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/pinecone/pinecone.py +0 -0
  117. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/premai/__init__.py +0 -0
  118. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/premai/premai.py +0 -0
  119. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/qdrant/__init__.py +0 -0
  120. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/qdrant/async_qdrant.py +0 -0
  121. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/qdrant/qdrant.py +0 -0
  122. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/reka/__init__.py +0 -0
  123. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/reka/async_reka.py +0 -0
  124. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/reka/reka.py +0 -0
  125. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/together/__init__.py +0 -0
  126. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/together/async_together.py +0 -0
  127. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/together/together.py +0 -0
  128. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/transformers/__init__.py +0 -0
  129. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/vertexai/__init__.py +0 -0
  130. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/vertexai/async_vertexai.py +0 -0
  131. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/vertexai/vertexai.py +0 -0
  132. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/vllm/__init__.py +0 -0
  133. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/instrumentation/vllm/vllm.py +0 -0
  134. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/otel/events.py +0 -0
  135. {openlit-1.33.21 → openlit-1.33.23}/src/openlit/otel/tracing.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: openlit
3
- Version: 1.33.21
3
+ Version: 1.33.23
4
4
  Summary: OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects
5
5
  License: Apache-2.0
6
6
  Keywords: OpenTelemetry,otel,otlp,llm,tracing,openai,anthropic,claude,cohere,llm monitoring,observability,monitoring,gpt,Generative AI,chatGPT,gpu
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "openlit"
3
- version = "1.33.21"
3
+ version = "1.33.23"
4
4
  description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
5
5
  authors = ["OpenLIT"]
6
6
  license = "Apache-2.0"
@@ -7,11 +7,11 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7
7
  from wrapt import wrap_function_wrapper
8
8
 
9
9
  from openlit.instrumentation.google_ai_studio.google_ai_studio import (
10
- generate
10
+ generate, generate_stream
11
11
  )
12
12
 
13
13
  from openlit.instrumentation.google_ai_studio.async_google_ai_studio import (
14
- async_generate
14
+ async_generate, async_generate_stream
15
15
  )
16
16
 
17
17
  _instruments = ("google-genai >= 1.3.0",)
@@ -39,16 +39,31 @@ class GoogleAIStudioInstrumentor(BaseInstrumentor):
39
39
  "google.genai.models",
40
40
  "Models.generate_content",
41
41
  generate(version, environment, application_name,
42
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
42
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
43
+ )
44
+
45
+ # sync stream generate
46
+ wrap_function_wrapper(
47
+ "google.genai.models",
48
+ "Models.generate_content_stream",
49
+ generate_stream(version, environment, application_name,
50
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
43
51
  )
44
52
 
45
53
  # async generate
46
54
  wrap_function_wrapper(
47
55
  "google.genai.models",
48
56
  "AsyncModels.generate_content",
49
- async_generate(version, environment,
50
- application_name, tracer, pricing_info, capture_message_content, metrics,
51
- disable_metrics),
57
+ async_generate(version, environment, application_name,
58
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
59
+ )
60
+
61
+ # async stream generate
62
+ wrap_function_wrapper(
63
+ "google.genai.models",
64
+ "AsyncModels.generate_content_stream",
65
+ async_generate_stream(version, environment, application_name,
66
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
52
67
  )
53
68
 
54
69
  def _uninstrument(self, **kwargs):
@@ -0,0 +1,159 @@
1
+ """
2
+ Module for monitoring Google AI Studio API calls.
3
+ """
4
+
5
+ import logging
6
+ import time
7
+ from opentelemetry.trace import SpanKind
8
+ from openlit.__helpers import (
9
+ handle_exception,
10
+ set_server_address_and_port
11
+ )
12
+ from openlit.instrumentation.google_ai_studio.utils import (
13
+ process_chat_response,
14
+ process_chunk,
15
+ process_streaming_chat_response
16
+ )
17
+ from openlit.semcov import SemanticConvention
18
+
19
+ # Initialize logger for logging potential issues and operations
20
+ logger = logging.getLogger(__name__)
21
+
22
+ def async_generate(version, environment, application_name,
23
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
24
+ """
25
+ Generates a telemetry wrapper for GenAI function call
26
+ """
27
+
28
+ async def wrapper(wrapped, instance, args, kwargs):
29
+ """
30
+ Wraps the GenAI function call.
31
+ """
32
+
33
+ server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
34
+ request_model = kwargs.get("model", "gemini-2.0-flash")
35
+
36
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
37
+
38
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
39
+ start_time = time.time()
40
+ response = await wrapped(*args, **kwargs)
41
+
42
+ response = process_chat_response(
43
+ instance = instance,
44
+ response=response,
45
+ request_model=request_model,
46
+ pricing_info=pricing_info,
47
+ server_port=server_port,
48
+ server_address=server_address,
49
+ environment=environment,
50
+ application_name=application_name,
51
+ metrics=metrics,
52
+ start_time=start_time,
53
+ span=span,
54
+ args=args,
55
+ kwargs=kwargs,
56
+ capture_message_content=capture_message_content,
57
+ disable_metrics=disable_metrics,
58
+ version=version,
59
+ )
60
+
61
+ return response
62
+
63
+ return wrapper
64
+
65
+ def async_generate_stream(version, environment, application_name,
66
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
67
+ """
68
+ Generates a telemetry wrapper for GenAI function call
69
+ """
70
+
71
+ class TracedAsyncStream:
72
+ """
73
+ Wrapper for streaming responses to collect telemetry.
74
+ """
75
+
76
+ def __init__(
77
+ self,
78
+ wrapped,
79
+ span,
80
+ span_name,
81
+ kwargs,
82
+ server_address,
83
+ server_port,
84
+ **args,
85
+ ):
86
+ self.__wrapped__ = wrapped
87
+ self._span = span
88
+ self._span_name = span_name
89
+ self._llmresponse = ''
90
+ self._finish_reason = ''
91
+ self._output_tokens = ''
92
+ self._input_tokens = ''
93
+ self._response_model = ''
94
+ self._tools = None
95
+
96
+ self._args = args
97
+ self._kwargs = kwargs
98
+ self._start_time = time.time()
99
+ self._end_time = None
100
+ self._timestamps = []
101
+ self._ttft = 0
102
+ self._tbt = 0
103
+ self._server_address = server_address
104
+ self._server_port = server_port
105
+
106
+ async def __aenter__(self):
107
+ await self.__wrapped__.__aenter__()
108
+ return self
109
+
110
+ async def __aexit__(self, exc_type, exc_value, traceback):
111
+ await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)
112
+
113
+ def __aiter__(self):
114
+ return self
115
+
116
+ async def __getattr__(self, name):
117
+ """Delegate attribute access to the wrapped object."""
118
+ return getattr(await self.__wrapped__, name)
119
+
120
+ async def __anext__(self):
121
+ try:
122
+ chunk = await self.__wrapped__.__anext__()
123
+ process_chunk(self, chunk)
124
+ return chunk
125
+ except StopAsyncIteration:
126
+ try:
127
+ with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
128
+ process_streaming_chat_response(
129
+ self,
130
+ pricing_info=pricing_info,
131
+ environment=environment,
132
+ application_name=application_name,
133
+ metrics=metrics,
134
+ capture_message_content=capture_message_content,
135
+ disable_metrics=disable_metrics,
136
+ version=version
137
+ )
138
+
139
+ except Exception as e:
140
+ handle_exception(self._span, e)
141
+ logger.error("Error in trace creation: %s", e)
142
+ raise
143
+
144
+ async def wrapper(wrapped, instance, args, kwargs):
145
+ """
146
+ Wraps the GenAI function call.
147
+ """
148
+
149
+ server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
150
+ request_model = kwargs.get("model", "gemini-2.0-flash")
151
+
152
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
153
+
154
+ awaited_wrapped = await wrapped(*args, **kwargs)
155
+ span = tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT)
156
+
157
+ return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
158
+
159
+ return wrapper
@@ -0,0 +1,159 @@
1
+ """
2
+ Module for monitoring Google AI Studio API calls.
3
+ """
4
+
5
+ import logging
6
+ import time
7
+ from opentelemetry.trace import SpanKind
8
+ from openlit.__helpers import (
9
+ handle_exception,
10
+ set_server_address_and_port
11
+ )
12
+ from openlit.instrumentation.google_ai_studio.utils import (
13
+ process_chat_response,
14
+ process_chunk,
15
+ process_streaming_chat_response
16
+ )
17
+ from openlit.semcov import SemanticConvention
18
+
19
+ # Initialize logger for logging potential issues and operations
20
+ logger = logging.getLogger(__name__)
21
+
22
+ def generate(version, environment, application_name,
23
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
24
+ """
25
+ Generates a telemetry wrapper for GenAI function call
26
+ """
27
+
28
+ def wrapper(wrapped, instance, args, kwargs):
29
+ """
30
+ Wraps the GenAI function call.
31
+ """
32
+
33
+ server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
34
+ request_model = kwargs.get("model", "gemini-2.0-flash")
35
+
36
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
37
+
38
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
39
+ start_time = time.time()
40
+ response = wrapped(*args, **kwargs)
41
+
42
+ response = process_chat_response(
43
+ instance = instance,
44
+ response=response,
45
+ request_model=request_model,
46
+ pricing_info=pricing_info,
47
+ server_port=server_port,
48
+ server_address=server_address,
49
+ environment=environment,
50
+ application_name=application_name,
51
+ metrics=metrics,
52
+ start_time=start_time,
53
+ span=span,
54
+ args=args,
55
+ kwargs=kwargs,
56
+ capture_message_content=capture_message_content,
57
+ disable_metrics=disable_metrics,
58
+ version=version,
59
+ )
60
+
61
+ return response
62
+
63
+ return wrapper
64
+
65
+ def generate_stream(version, environment, application_name,
66
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
67
+ """
68
+ Generates a telemetry wrapper for GenAI function call
69
+ """
70
+
71
+ class TracedSyncStream:
72
+ """
73
+ Wrapper for streaming responses to collect telemetry.
74
+ """
75
+
76
+ def __init__(
77
+ self,
78
+ wrapped,
79
+ span,
80
+ span_name,
81
+ kwargs,
82
+ server_address,
83
+ server_port,
84
+ **args,
85
+ ):
86
+ self.__wrapped__ = wrapped
87
+ self._span = span
88
+ self._span_name = span_name
89
+ self._llmresponse = ''
90
+ self._finish_reason = ''
91
+ self._output_tokens = ''
92
+ self._input_tokens = ''
93
+ self._response_model = ''
94
+ self._tools = None
95
+
96
+ self._args = args
97
+ self._kwargs = kwargs
98
+ self._start_time = time.time()
99
+ self._end_time = None
100
+ self._timestamps = []
101
+ self._ttft = 0
102
+ self._tbt = 0
103
+ self._server_address = server_address
104
+ self._server_port = server_port
105
+
106
+ def __enter__(self):
107
+ self.__wrapped__.__enter__()
108
+ return self
109
+
110
+ def __exit__(self, exc_type, exc_value, traceback):
111
+ self.__wrapped__.__exit__(exc_type, exc_value, traceback)
112
+
113
+ def __iter__(self):
114
+ return self
115
+
116
+ def __getattr__(self, name):
117
+ """Delegate attribute access to the wrapped object."""
118
+ return getattr(self.__wrapped__, name)
119
+
120
+ def __next__(self):
121
+ try:
122
+ chunk = self.__wrapped__.__next__()
123
+ process_chunk(self, chunk)
124
+ return chunk
125
+ except StopIteration:
126
+ try:
127
+ with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
128
+ process_streaming_chat_response(
129
+ self,
130
+ pricing_info=pricing_info,
131
+ environment=environment,
132
+ application_name=application_name,
133
+ metrics=metrics,
134
+ capture_message_content=capture_message_content,
135
+ disable_metrics=disable_metrics,
136
+ version=version
137
+ )
138
+
139
+ except Exception as e:
140
+ handle_exception(self._span, e)
141
+ logger.error("Error in trace creation: %s", e)
142
+ raise
143
+
144
+ def wrapper(wrapped, instance, args, kwargs):
145
+ """
146
+ Wraps the GenAI function call.
147
+ """
148
+
149
+ server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
150
+ request_model = kwargs.get("model", "gemini-2.0-flash")
151
+
152
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
153
+
154
+ awaited_wrapped = wrapped(*args, **kwargs)
155
+ span = tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT)
156
+
157
+ return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
158
+
159
+ return wrapper
@@ -0,0 +1,249 @@
1
+ """
2
+ Google AI Studio OpenTelemetry instrumentation utility functions
3
+ """
4
+ import time
5
+
6
+ from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
7
+ from opentelemetry.trace import Status, StatusCode
8
+
9
+ from openlit.__helpers import (
10
+ calculate_ttft,
11
+ response_as_dict,
12
+ calculate_tbt,
13
+ get_chat_model_cost,
14
+ create_metrics_attributes,
15
+ )
16
+ from openlit.semcov import SemanticConvention
17
+
18
+ def format_content(messages):
19
+ """
20
+ Process a list of messages to extract content, categorize them by role,
21
+ and concatenate all 'content' fields into a single string with role: content format.
22
+ """
23
+
24
+ formatted_messages = []
25
+ prompt = ""
26
+
27
+ if isinstance(messages, list):
28
+ try:
29
+ for content in messages:
30
+ role = content.role if content.role is not None else "user"
31
+ parts = content.parts
32
+ content_str = []
33
+
34
+ for part in parts:
35
+ # Collect relevant fields and handle each type of data that Part could contain
36
+ if part.text:
37
+ content_str.append(f"text: {part.text}")
38
+ if part.video_metadata:
39
+ content_str.append(f"video_metadata: {part.video_metadata}")
40
+ if part.thought:
41
+ content_str.append(f"thought: {part.thought}")
42
+ if part.code_execution_result:
43
+ content_str.append(f"code_execution_result: {part.code_execution_result}")
44
+ if part.executable_code:
45
+ content_str.append(f"executable_code: {part.executable_code}")
46
+ if part.file_data:
47
+ content_str.append(f"file_data: {part.file_data}")
48
+ if part.function_call:
49
+ content_str.append(f"function_call: {part.function_call}")
50
+ if part.function_response:
51
+ content_str.append(f"function_response: {part.function_response}")
52
+ if part.inline_data:
53
+ content_str.append(f"inline_data: {part.inline_data}")
54
+
55
+ formatted_messages.append(f"{role}: {', '.join(content_str)}")
56
+
57
+ prompt = "\n".join(formatted_messages)
58
+
59
+ except:
60
+ prompt = str(messages)
61
+
62
+ else:
63
+ prompt = messages
64
+
65
+ return prompt
66
+
67
+ def process_chunk(self, chunk):
68
+ """
69
+ Process a chunk of response data and update state.
70
+ """
71
+
72
+ end_time = time.time()
73
+ # Record the timestamp for the current chunk
74
+ self._timestamps.append(end_time)
75
+
76
+ if len(self._timestamps) == 1:
77
+ # Calculate time to first chunk
78
+ self._ttft = calculate_ttft(self._timestamps, self._start_time)
79
+
80
+ chunked = response_as_dict(chunk)
81
+
82
+
83
+ self._response_id = str(chunked.get('response_id'))
84
+ self._input_tokens = chunked.get('usage_metadata').get('prompt_token_count')
85
+ self._response_model = chunked.get('model_version')
86
+
87
+ if chunk.text:
88
+ self._llmresponse += str(chunk.text)
89
+
90
+ self._output_tokens = chunked.get('usage_metadata').get('candidates_token_count')
91
+ self._reasoning_tokens = chunked.get('usage_metadata').get('thoughts_token_count') or 0
92
+ self._finish_reason = str(chunked.get('candidates')[0].get('finish_reason'))
93
+
94
+ try:
95
+ self._tools = chunked.get('candidates', [])[0].get('content', {}).get('parts', [])[0].get('function_call', '')
96
+ except:
97
+ self._tools = None
98
+
99
+ def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
100
+ capture_message_content, disable_metrics, version, is_stream):
101
+ """
102
+ Process chat request and generate Telemetry
103
+ """
104
+
105
+ scope._end_time = time.time()
106
+ if len(scope._timestamps) > 1:
107
+ scope._tbt = calculate_tbt(scope._timestamps)
108
+
109
+ prompt = format_content(scope._kwargs.get('contents', ''))
110
+ request_model = scope._kwargs.get("model", "gemini-2.0-flash")
111
+
112
+ cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
113
+
114
+ # Set Span attributes (OTel Semconv)
115
+ scope._span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
116
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
117
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_GEMINI)
118
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
119
+ scope._span.set_attribute(SemanticConvention.SERVER_PORT, scope._server_port)
120
+
121
+ inference_config = scope._kwargs.get('config', {})
122
+
123
+ attributes = [
124
+ (SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, 'frequency_penalty'),
125
+ (SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, 'max_tokens'),
126
+ (SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, 'presence_penalty'),
127
+ (SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, 'stop_sequences'),
128
+ (SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, 'temperature'),
129
+ (SemanticConvention.GEN_AI_REQUEST_TOP_P, 'top_p'),
130
+ (SemanticConvention.GEN_AI_REQUEST_TOP_K, 'top_k'),
131
+ ]
132
+
133
+ # Set each attribute if the corresponding value exists and is not None
134
+ for attribute, key in attributes:
135
+ # Use getattr to get the attribute value from the object
136
+ value = getattr(inference_config, key, None)
137
+ if value is not None:
138
+ scope._span.set_attribute(attribute, value)
139
+
140
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
141
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
142
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
143
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
144
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, scope._reasoning_tokens)
145
+ scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, scope._server_address)
146
+
147
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
148
+ 'text' if isinstance(scope._llmresponse, str) else 'json')
149
+
150
+ scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
151
+ scope._span.set_attribute(SERVICE_NAME, application_name)
152
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
153
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
154
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, scope._tbt)
155
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
156
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
157
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
158
+ scope._input_tokens + scope._output_tokens + scope._reasoning_tokens)
159
+
160
+ if scope._tools:
161
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get('name',''))
162
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get('id','')))
163
+ scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get('args','')))
164
+
165
+ # To be removed one the change to span_attributes (from span events) is complete
166
+ if capture_message_content:
167
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
168
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
169
+ scope._span.add_event(
170
+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
171
+ attributes={
172
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: prompt,
173
+ },
174
+ )
175
+ scope._span.add_event(
176
+ name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
177
+ attributes={
178
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: scope._llmresponse,
179
+ },
180
+ )
181
+
182
+ scope._span.set_status(Status(StatusCode.OK))
183
+
184
+ if not disable_metrics:
185
+ metrics_attributes = create_metrics_attributes(
186
+ service_name=application_name,
187
+ deployment_environment=environment,
188
+ operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
189
+ system=SemanticConvention.GEN_AI_SYSTEM_GEMINI,
190
+ request_model=request_model,
191
+ server_address=scope._server_address,
192
+ server_port=scope._server_port,
193
+ response_model=scope._response_model,
194
+ )
195
+
196
+ metrics['genai_client_operation_duration'].record(scope._end_time - scope._start_time, metrics_attributes)
197
+ metrics['genai_server_tbt'].record(scope._tbt, metrics_attributes)
198
+ metrics['genai_server_ttft'].record(scope._ttft, metrics_attributes)
199
+ metrics['genai_requests'].add(1, metrics_attributes)
200
+ metrics['genai_completion_tokens'].add(scope._output_tokens, metrics_attributes)
201
+ metrics['genai_prompt_tokens'].add(scope._input_tokens, metrics_attributes)
202
+ metrics['genai_reasoning_tokens'].add(scope._reasoning_tokens, metrics_attributes)
203
+ metrics['genai_cost'].record(cost, metrics_attributes)
204
+ metrics['genai_client_usage_tokens'].record(
205
+ scope._input_tokens + scope._output_tokens + scope._reasoning_tokens, metrics_attributes)
206
+
207
+
208
+ def process_streaming_chat_response(self, pricing_info, environment, application_name, metrics,
209
+ capture_message_content=False, disable_metrics=False, version=''):
210
+ """
211
+ Process chat request and generate Telemetry
212
+ """
213
+
214
+ common_chat_logic(self, pricing_info, environment, application_name, metrics,
215
+ capture_message_content, disable_metrics, version, is_stream=True)
216
+
217
+ def process_chat_response(instance, response, request_model, pricing_info, server_port, server_address,
218
+ environment, application_name, metrics, start_time,
219
+ span, args, kwargs, capture_message_content=False, disable_metrics=False, version="1.0.0"):
220
+ """
221
+ Process chat request and generate Telemetry
222
+ """
223
+
224
+ self = type('GenericScope', (), {})()
225
+ response_dict = response_as_dict(response)
226
+
227
+ self._start_time = start_time
228
+ self._end_time = time.time()
229
+ self._span = span
230
+ self._llmresponse = str(response.text)
231
+ self._input_tokens = response_dict.get('usage_metadata').get('prompt_token_count')
232
+ self._output_tokens = response_dict.get('usage_metadata').get('candidates_token_count')
233
+ self._reasoning_tokens = response_dict.get('usage_metadata').get('thoughts_token_count') or 0
234
+ self._response_model = response_dict.get('model_version')
235
+ self._timestamps = []
236
+ self._ttft, self._tbt = self._end_time - self._start_time, 0
237
+ self._server_address, self._server_port = server_address, server_port
238
+ self._kwargs = kwargs
239
+ self._finish_reason = str(response_dict.get('candidates')[0].get('finish_reason'))
240
+
241
+ try:
242
+ self._tools = response_dict.get('candidates', [])[0].get('content', {}).get('parts', [])[0].get('function_call', '')
243
+ except:
244
+ self._tools = None
245
+
246
+ common_chat_logic(self, pricing_info, environment, application_name, metrics,
247
+ capture_message_content, disable_metrics, version, is_stream=False)
248
+
249
+ return response
@@ -8,7 +8,6 @@ from opentelemetry.trace import SpanKind
8
8
  from openlit.__helpers import (
9
9
  set_server_address_and_port
10
10
  )
11
-
12
11
  from openlit.instrumentation.transformers.utils import (
13
12
  process_chat_response,
14
13
  )
@@ -18,7 +17,7 @@ from openlit.semcov import SemanticConvention
18
17
  logger = logging.getLogger(__name__)
19
18
 
20
19
  def pipeline_wrapper(version, environment, application_name,
21
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
20
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
22
21
  """
23
22
  Generates a telemetry wrapper for GenAI function call
24
23
  """
@@ -72,7 +72,7 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
72
72
  # To be removed one the change to span_attributes (from span events) is complete
73
73
  if capture_message_content:
74
74
  scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, scope._prompt)
75
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse,)
75
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
76
76
 
77
77
  scope._span.add_event(
78
78
  name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
@@ -111,8 +111,8 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
111
111
  metrics["genai_cost"].record(cost, metrics_attributes)
112
112
 
113
113
  def process_chat_response(instance, response, request_model, pricing_info, server_port, server_address,
114
- environment, application_name, metrics, start_time,
115
- span, args, kwargs, capture_message_content=False, disable_metrics=False, version="1.0.0"):
114
+ environment, application_name, metrics, start_time,
115
+ span, args, kwargs, capture_message_content=False, disable_metrics=False, version="1.0.0"):
116
116
  """
117
117
  Process chat request and generate Telemetry
118
118
  """
@@ -202,6 +202,11 @@ def setup_meter(application_name, environment, meter, otlp_endpoint, otlp_header
202
202
  description="Number of completion tokens processed.",
203
203
  unit="1",
204
204
  ),
205
+ "genai_reasoning_tokens": meter.create_counter(
206
+ name=SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS,
207
+ description="Number of reasoning thought tokens processed.",
208
+ unit="1",
209
+ ),
205
210
  "genai_cost": meter.create_histogram(
206
211
  name=SemanticConvention.GEN_AI_USAGE_COST,
207
212
  description="The distribution of GenAI request costs.",