genai-otel-instrument 0.1.2.dev0__py3-none-any.whl → 0.1.7.dev0__py3-none-any.whl
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.
Potentially problematic release.
This version of genai-otel-instrument might be problematic. Click here for more details.
- genai_otel/__version__.py +2 -2
- genai_otel/auto_instrument.py +18 -1
- genai_otel/config.py +22 -1
- genai_otel/cost_calculator.py +204 -13
- genai_otel/cost_enrichment_processor.py +175 -0
- genai_otel/gpu_metrics.py +50 -0
- genai_otel/instrumentors/base.py +300 -44
- genai_otel/instrumentors/cohere_instrumentor.py +140 -76
- genai_otel/instrumentors/huggingface_instrumentor.py +142 -13
- genai_otel/instrumentors/langchain_instrumentor.py +75 -75
- genai_otel/instrumentors/mistralai_instrumentor.py +234 -38
- genai_otel/instrumentors/ollama_instrumentor.py +104 -35
- genai_otel/instrumentors/replicate_instrumentor.py +59 -14
- genai_otel/instrumentors/togetherai_instrumentor.py +120 -16
- genai_otel/instrumentors/vertexai_instrumentor.py +79 -15
- genai_otel/llm_pricing.json +869 -589
- genai_otel/logging_config.py +45 -45
- genai_otel/py.typed +2 -2
- {genai_otel_instrument-0.1.2.dev0.dist-info → genai_otel_instrument-0.1.7.dev0.dist-info}/METADATA +294 -33
- {genai_otel_instrument-0.1.2.dev0.dist-info → genai_otel_instrument-0.1.7.dev0.dist-info}/RECORD +24 -23
- {genai_otel_instrument-0.1.2.dev0.dist-info → genai_otel_instrument-0.1.7.dev0.dist-info}/WHEEL +0 -0
- {genai_otel_instrument-0.1.2.dev0.dist-info → genai_otel_instrument-0.1.7.dev0.dist-info}/entry_points.txt +0 -0
- {genai_otel_instrument-0.1.2.dev0.dist-info → genai_otel_instrument-0.1.7.dev0.dist-info}/licenses/LICENSE +0 -0
- {genai_otel_instrument-0.1.2.dev0.dist-info → genai_otel_instrument-0.1.7.dev0.dist-info}/top_level.txt +0 -0
|
@@ -1,42 +1,146 @@
|
|
|
1
1
|
"""OpenTelemetry instrumentor for the Together AI SDK.
|
|
2
2
|
|
|
3
3
|
This instrumentor automatically traces completion calls to Together AI models,
|
|
4
|
-
capturing relevant attributes such as the model name.
|
|
4
|
+
capturing relevant attributes such as the model name and token usage.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import logging
|
|
8
|
+
from typing import Any, Dict, Optional
|
|
8
9
|
|
|
9
10
|
from ..config import OTelConfig
|
|
10
11
|
from .base import BaseInstrumentor
|
|
11
12
|
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
12
15
|
|
|
13
16
|
class TogetherAIInstrumentor(BaseInstrumentor):
|
|
14
17
|
"""Instrumentor for Together AI"""
|
|
15
18
|
|
|
16
19
|
def instrument(self, config: OTelConfig):
|
|
20
|
+
"""Instrument Together AI SDK if available."""
|
|
17
21
|
self.config = config
|
|
18
22
|
try:
|
|
19
23
|
import together
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
model = kwargs.get("model", "unknown")
|
|
25
|
+
# Instrument chat completions (newer API)
|
|
26
|
+
if hasattr(together, "Together"):
|
|
27
|
+
# This is the newer Together SDK with client-based API
|
|
28
|
+
original_init = together.Together.__init__
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
def wrapped_init(instance, *args, **kwargs):
|
|
31
|
+
original_init(instance, *args, **kwargs)
|
|
32
|
+
self._instrument_client(instance)
|
|
29
33
|
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
together.Together.__init__ = wrapped_init
|
|
35
|
+
self._instrumented = True
|
|
36
|
+
logger.info("Together AI instrumentation enabled (client-based API)")
|
|
37
|
+
# Fallback to older Complete API if available
|
|
38
|
+
elif hasattr(together, "Complete"):
|
|
39
|
+
original_complete = together.Complete.create
|
|
32
40
|
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
wrapped_complete = self.create_span_wrapper(
|
|
42
|
+
span_name="together.complete",
|
|
43
|
+
extract_attributes=self._extract_complete_attributes,
|
|
44
|
+
)(original_complete)
|
|
35
45
|
|
|
36
|
-
|
|
46
|
+
together.Complete.create = wrapped_complete
|
|
47
|
+
self._instrumented = True
|
|
48
|
+
logger.info("Together AI instrumentation enabled (Complete API)")
|
|
37
49
|
|
|
38
50
|
except ImportError:
|
|
39
|
-
|
|
51
|
+
logger.debug("Together AI library not installed, instrumentation will be skipped")
|
|
52
|
+
except Exception as e:
|
|
53
|
+
logger.error("Failed to instrument Together AI: %s", e, exc_info=True)
|
|
54
|
+
if config.fail_on_error:
|
|
55
|
+
raise
|
|
56
|
+
|
|
57
|
+
def _instrument_client(self, client):
|
|
58
|
+
"""Instrument Together AI client methods."""
|
|
59
|
+
if hasattr(client, "chat") and hasattr(client.chat, "completions"):
|
|
60
|
+
original_create = client.chat.completions.create
|
|
61
|
+
|
|
62
|
+
wrapped_create = self.create_span_wrapper(
|
|
63
|
+
span_name="together.chat.completion",
|
|
64
|
+
extract_attributes=self._extract_chat_attributes,
|
|
65
|
+
)(original_create)
|
|
66
|
+
|
|
67
|
+
client.chat.completions.create = wrapped_create
|
|
68
|
+
|
|
69
|
+
def _extract_chat_attributes(self, instance: Any, args: Any, kwargs: Any) -> Dict[str, Any]:
|
|
70
|
+
"""Extract attributes from Together AI chat completion call.
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
instance: The client instance.
|
|
74
|
+
args: Positional arguments.
|
|
75
|
+
kwargs: Keyword arguments.
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
Dict[str, Any]: Dictionary of attributes to set on the span.
|
|
79
|
+
"""
|
|
80
|
+
attrs = {}
|
|
81
|
+
model = kwargs.get("model", "unknown")
|
|
82
|
+
messages = kwargs.get("messages", [])
|
|
83
|
+
|
|
84
|
+
attrs["gen_ai.system"] = "together"
|
|
85
|
+
attrs["gen_ai.request.model"] = model
|
|
86
|
+
attrs["gen_ai.operation.name"] = "chat"
|
|
87
|
+
attrs["gen_ai.request.message_count"] = len(messages)
|
|
88
|
+
|
|
89
|
+
# Optional parameters
|
|
90
|
+
if "temperature" in kwargs:
|
|
91
|
+
attrs["gen_ai.request.temperature"] = kwargs["temperature"]
|
|
92
|
+
if "top_p" in kwargs:
|
|
93
|
+
attrs["gen_ai.request.top_p"] = kwargs["top_p"]
|
|
94
|
+
if "max_tokens" in kwargs:
|
|
95
|
+
attrs["gen_ai.request.max_tokens"] = kwargs["max_tokens"]
|
|
96
|
+
|
|
97
|
+
return attrs
|
|
98
|
+
|
|
99
|
+
def _extract_complete_attributes(self, instance: Any, args: Any, kwargs: Any) -> Dict[str, Any]:
|
|
100
|
+
"""Extract attributes from Together AI complete call.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
instance: The instance (None for class methods).
|
|
104
|
+
args: Positional arguments.
|
|
105
|
+
kwargs: Keyword arguments.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
Dict[str, Any]: Dictionary of attributes to set on the span.
|
|
109
|
+
"""
|
|
110
|
+
attrs = {}
|
|
111
|
+
model = kwargs.get("model", "unknown")
|
|
112
|
+
|
|
113
|
+
attrs["gen_ai.system"] = "together"
|
|
114
|
+
attrs["gen_ai.request.model"] = model
|
|
115
|
+
attrs["gen_ai.operation.name"] = "complete"
|
|
116
|
+
|
|
117
|
+
return attrs
|
|
40
118
|
|
|
41
119
|
def _extract_usage(self, result) -> Optional[Dict[str, int]]:
|
|
42
|
-
|
|
120
|
+
"""Extract token usage from Together AI response.
|
|
121
|
+
|
|
122
|
+
Together AI uses OpenAI-compatible format with usage field containing:
|
|
123
|
+
- prompt_tokens: Input tokens
|
|
124
|
+
- completion_tokens: Output tokens
|
|
125
|
+
- total_tokens: Total tokens
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
result: The API response object.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
Optional[Dict[str, int]]: Dictionary with token counts or None.
|
|
132
|
+
"""
|
|
133
|
+
try:
|
|
134
|
+
# Handle OpenAI-compatible response format
|
|
135
|
+
if hasattr(result, "usage") and result.usage:
|
|
136
|
+
usage = result.usage
|
|
137
|
+
return {
|
|
138
|
+
"prompt_tokens": getattr(usage, "prompt_tokens", 0),
|
|
139
|
+
"completion_tokens": getattr(usage, "completion_tokens", 0),
|
|
140
|
+
"total_tokens": getattr(usage, "total_tokens", 0),
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return None
|
|
144
|
+
except Exception as e:
|
|
145
|
+
logger.debug("Failed to extract usage from Together AI response: %s", e)
|
|
146
|
+
return None
|
|
@@ -1,42 +1,106 @@
|
|
|
1
1
|
"""OpenTelemetry instrumentor for Google Vertex AI SDK.
|
|
2
2
|
|
|
3
3
|
This instrumentor automatically traces content generation calls to Vertex AI models,
|
|
4
|
-
capturing relevant attributes such as the model name.
|
|
4
|
+
capturing relevant attributes such as the model name and token usage.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import logging
|
|
8
|
+
from typing import Any, Dict, Optional
|
|
8
9
|
|
|
9
10
|
from ..config import OTelConfig
|
|
10
11
|
from .base import BaseInstrumentor
|
|
11
12
|
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
12
15
|
|
|
13
16
|
class VertexAIInstrumentor(BaseInstrumentor):
|
|
14
17
|
"""Instrumentor for Google Vertex AI"""
|
|
15
18
|
|
|
16
19
|
def instrument(self, config: OTelConfig):
|
|
20
|
+
"""Instrument Vertex AI SDK if available."""
|
|
17
21
|
self.config = config
|
|
18
22
|
try:
|
|
19
23
|
from vertexai.preview.generative_models import GenerativeModel
|
|
20
24
|
|
|
21
25
|
original_generate = GenerativeModel.generate_content
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
# Wrap using create_span_wrapper
|
|
28
|
+
wrapped_generate = self.create_span_wrapper(
|
|
29
|
+
span_name="vertexai.generate_content",
|
|
30
|
+
extract_attributes=self._extract_generate_attributes,
|
|
31
|
+
)(original_generate)
|
|
26
32
|
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
GenerativeModel.generate_content = wrapped_generate
|
|
34
|
+
self._instrumented = True
|
|
35
|
+
logger.info("Vertex AI instrumentation enabled")
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
except ImportError:
|
|
38
|
+
logger.debug("Vertex AI library not installed, instrumentation will be skipped")
|
|
39
|
+
except Exception as e:
|
|
40
|
+
logger.error("Failed to instrument Vertex AI: %s", e, exc_info=True)
|
|
41
|
+
if config.fail_on_error:
|
|
42
|
+
raise
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
def _extract_generate_attributes(self, instance: Any, args: Any, kwargs: Any) -> Dict[str, Any]:
|
|
45
|
+
"""Extract attributes from Vertex AI generate_content call.
|
|
35
46
|
|
|
36
|
-
|
|
47
|
+
Args:
|
|
48
|
+
instance: The GenerativeModel instance.
|
|
49
|
+
args: Positional arguments.
|
|
50
|
+
kwargs: Keyword arguments.
|
|
37
51
|
|
|
38
|
-
|
|
39
|
-
|
|
52
|
+
Returns:
|
|
53
|
+
Dict[str, Any]: Dictionary of attributes to set on the span.
|
|
54
|
+
"""
|
|
55
|
+
attrs = {}
|
|
56
|
+
model_name = getattr(instance, "_model_name", "unknown")
|
|
57
|
+
|
|
58
|
+
attrs["gen_ai.system"] = "vertexai"
|
|
59
|
+
attrs["gen_ai.request.model"] = model_name
|
|
60
|
+
attrs["gen_ai.operation.name"] = "generate_content"
|
|
61
|
+
|
|
62
|
+
return attrs
|
|
40
63
|
|
|
41
64
|
def _extract_usage(self, result) -> Optional[Dict[str, int]]:
|
|
42
|
-
|
|
65
|
+
"""Extract token usage from Vertex AI response.
|
|
66
|
+
|
|
67
|
+
Vertex AI responses include usage_metadata with:
|
|
68
|
+
- prompt_token_count: Input tokens
|
|
69
|
+
- candidates_token_count: Output tokens
|
|
70
|
+
- total_token_count: Total tokens
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
result: The API response object.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
Optional[Dict[str, int]]: Dictionary with token counts or None.
|
|
77
|
+
"""
|
|
78
|
+
try:
|
|
79
|
+
# Handle response with usage_metadata
|
|
80
|
+
if hasattr(result, "usage_metadata") and result.usage_metadata:
|
|
81
|
+
usage_metadata = result.usage_metadata
|
|
82
|
+
|
|
83
|
+
# Try snake_case first (Python SDK style)
|
|
84
|
+
prompt_tokens = getattr(usage_metadata, "prompt_token_count", None)
|
|
85
|
+
candidates_tokens = getattr(usage_metadata, "candidates_token_count", None)
|
|
86
|
+
total_tokens = getattr(usage_metadata, "total_token_count", None)
|
|
87
|
+
|
|
88
|
+
# Fallback to camelCase (REST API style)
|
|
89
|
+
if prompt_tokens is None:
|
|
90
|
+
prompt_tokens = getattr(usage_metadata, "promptTokenCount", 0)
|
|
91
|
+
if candidates_tokens is None:
|
|
92
|
+
candidates_tokens = getattr(usage_metadata, "candidatesTokenCount", 0)
|
|
93
|
+
if total_tokens is None:
|
|
94
|
+
total_tokens = getattr(usage_metadata, "totalTokenCount", 0)
|
|
95
|
+
|
|
96
|
+
if prompt_tokens or candidates_tokens:
|
|
97
|
+
return {
|
|
98
|
+
"prompt_tokens": int(prompt_tokens or 0),
|
|
99
|
+
"completion_tokens": int(candidates_tokens or 0),
|
|
100
|
+
"total_tokens": int(total_tokens or 0),
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return None
|
|
104
|
+
except Exception as e:
|
|
105
|
+
logger.debug("Failed to extract usage from Vertex AI response: %s", e)
|
|
106
|
+
return None
|