Skip to content

Commit a810ce1

Browse files
authored
refactor: remove internal record_aicore_metric function (#19)
1 parent 171d984 commit a810ce1

5 files changed

Lines changed: 2 additions & 302 deletions

File tree

src/sap_cloud_sdk/core/telemetry/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from sap_cloud_sdk.core.telemetry.telemetry import (
99
record_request_metric,
1010
record_error_metric,
11-
record_aicore_metric,
1211
set_tenant_id,
1312
get_tenant_id,
1413
)
@@ -33,7 +32,6 @@
3332
"record_metrics",
3433
"record_request_metric",
3534
"record_error_metric",
36-
"record_aicore_metric",
3735
"set_tenant_id",
3836
"get_tenant_id",
3937
"auto_instrument",

src/sap_cloud_sdk/core/telemetry/telemetry.py

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
from sap_cloud_sdk.core.telemetry.constants import (
1414
REQUEST_COUNTER_NAME,
1515
ERROR_COUNTER_NAME,
16-
LLM_TOKEN_HISTOGRAM_NAME,
1716
ATTR_SAP_TENANT_ID,
1817
ATTR_CAPABILITY,
1918
ATTR_FUNCTIONALITY,
2019
ATTR_SOURCE,
2120
ATTR_DEPRECATED,
22-
ATTR_GENAI_REQUEST_MODEL,
23-
ATTR_GENAI_OPERATION_NAME,
24-
ATTR_GENAI_TOKEN_TYPE,
25-
ATTR_GENAI_PROVIDER,
2621
)
2722
from sap_cloud_sdk.core.telemetry.module import Module
2823

@@ -32,7 +27,6 @@
3227
# Global metric instruments
3328
_request_counter: Optional[metrics.Counter] = None
3429
_error_counter: Optional[metrics.Counter] = None
35-
_aicore_token_histogram: Optional[metrics.Histogram] = None
3630

3731
# Context variable for per-request tenant ID
3832
_tenant_id_var: ContextVar[str] = ContextVar("tenant_id", default="")
@@ -136,141 +130,6 @@ def record_error_metric(
136130
logger.debug(f"Failed to record error metric: {e}")
137131

138132

139-
def record_aicore_metric(
140-
model_name: str,
141-
provider: str,
142-
operation_name: str,
143-
input_tokens: int,
144-
output_tokens: int,
145-
custom_attributes: Optional[Dict[str, Any]] = None,
146-
) -> None:
147-
"""Record token usage metrics for GenAI model API calls.
148-
149-
This function records token consumption for Generative AI model API calls following
150-
OpenTelemetry GenAI semantic conventions. It creates two separate histogram observations:
151-
one for input tokens and one for output tokens, each differentiated by the gen_ai.token.type
152-
attribute. The metrics include all default SDK attributes (service instance, SDK version,
153-
deployment metadata, etc.) along with the model name.
154-
155-
Args:
156-
model_name: The name of the GenAI model (e.g., "gpt-4", "claude-3-opus")
157-
provider: The name of the GenAI provider (e.g., "openai", "anthropic", "sap-aicore")
158-
operation_name: The type of GenAI operation. Well-known values include:
159-
- "chat": Chat completion operation (e.g., OpenAI Chat API)
160-
- "text_completion": Text completion operation
161-
- "embeddings": Embeddings operation (e.g., OpenAI Create embeddings API)
162-
- "generate_content": Multimodal content generation (e.g., Gemini Generate Content)
163-
- "create_agent": Create GenAI agent
164-
- "invoke_agent": Invoke GenAI agent
165-
- "execute_tool": Execute a tool
166-
Custom values are also allowed.
167-
input_tokens: Number of input/prompt tokens consumed
168-
output_tokens: Number of output/completion tokens generated
169-
custom_attributes: Optional dictionary of additional custom attributes to include
170-
in the metric. These will be merged with the standard attributes.
171-
172-
Example:
173-
```python
174-
from sap_cloud_sdk.core.telemetry import record_aicore_metric
175-
176-
# Chat completion
177-
record_aicore_metric(
178-
model_name="gpt-4",
179-
provider="openai",
180-
operation_name="chat",
181-
input_tokens=150,
182-
output_tokens=75
183-
)
184-
185-
# With additional custom attributes
186-
record_aicore_metric(
187-
model_name="gpt-4",
188-
provider="openai",
189-
operation_name="chat",
190-
input_tokens=150,
191-
output_tokens=75,
192-
custom_attributes={
193-
"user_id": "user123",
194-
"session_id": "session456"
195-
}
196-
)
197-
```
198-
"""
199-
global _aicore_token_histogram
200-
201-
# Lazy initialization of metrics
202-
if _aicore_token_histogram is None:
203-
_initialize_aicore_metrics()
204-
if _aicore_token_histogram is None:
205-
return
206-
207-
try:
208-
base_attributes = _genai_base_attributes(model_name, provider, operation_name)
209-
210-
# Merge in any additional user-provided attributes
211-
if custom_attributes:
212-
base_attributes.update(custom_attributes)
213-
214-
# Record input tokens as separate histogram observation
215-
input_attributes = base_attributes.copy()
216-
input_attributes[ATTR_GENAI_TOKEN_TYPE] = "input"
217-
_aicore_token_histogram.record(input_tokens, input_attributes)
218-
219-
# Record output tokens as separate histogram observation
220-
output_attributes = base_attributes.copy()
221-
output_attributes[ATTR_GENAI_TOKEN_TYPE] = "output"
222-
_aicore_token_histogram.record(output_tokens, output_attributes)
223-
224-
except Exception as e:
225-
logger.debug(f"Failed to record GenAI metric: {e}")
226-
227-
228-
def _genai_base_attributes(
229-
model_name: str, provider: str, operation_name: str
230-
) -> Dict[str, Any]:
231-
"""Get base attributes for GenAI metrics.
232-
233-
Args:
234-
model_name: The name of the LLM model
235-
provider: The name of the GenAI provider
236-
operation_name: The type of GenAI operation
237-
238-
Returns:
239-
Dictionary of attributes with default SDK attributes plus GenAI-specific ones.
240-
"""
241-
# Start with default SDK attributes for AI Core module
242-
attributes = default_attributes(
243-
module=Module.AICORE, source=None, operation="model_call", deprecated=False
244-
)
245-
246-
# Add GenAI-specific attributes
247-
attributes[ATTR_GENAI_REQUEST_MODEL] = model_name
248-
attributes[ATTR_GENAI_PROVIDER] = provider
249-
attributes[ATTR_GENAI_OPERATION_NAME] = operation_name
250-
251-
return attributes
252-
253-
254-
def _initialize_aicore_metrics() -> None:
255-
"""Initialize GenAI-specific metric instruments."""
256-
global _aicore_token_histogram
257-
258-
try:
259-
meter = get_meter()
260-
261-
# GenAI token usage histogram
262-
_aicore_token_histogram = meter.create_histogram(
263-
name=LLM_TOKEN_HISTOGRAM_NAME,
264-
description="Token usage for GenAI model requests",
265-
unit="{tokens}",
266-
)
267-
268-
logger.debug("GenAI telemetry metrics initialized successfully")
269-
270-
except Exception as e:
271-
logger.error(f"Failed to initialize GenAI telemetry metrics: {e}")
272-
273-
274133
def default_attributes(
275134
module: Module, source: Optional[Module], operation: str, deprecated: bool = False
276135
) -> Dict[str, Any]:

src/sap_cloud_sdk/core/telemetry/user-guide.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,38 +104,6 @@ with invoke_agent_span(provider="openai", agent_name="SupportBot", agent_id="id"
104104
response = client.beta.threads.runs.create(...)
105105
```
106106

107-
### Record token metrics
108-
109-
```python
110-
from sap_cloud_sdk.core.telemetry import record_aicore_metric
111-
112-
record_aicore_metric(
113-
model_name="gpt-4",
114-
provider="openai",
115-
operation_name="chat",
116-
input_tokens=150,
117-
output_tokens=75
118-
)
119-
```
120-
121-
With custom attributes:
122-
123-
```python
124-
record_aicore_metric(
125-
model_name="gpt-4",
126-
provider="openai",
127-
operation_name="chat",
128-
input_tokens=150,
129-
output_tokens=75,
130-
custom_attributes={
131-
"user_id": "user123",
132-
"feature": "document_summarization"
133-
}
134-
)
135-
```
136-
137-
Operation names: `"chat"`, `"text_completion"`, `"embeddings"`, `"generate_content"`, `"create_agent"`, `"invoke_agent"`, `"execute_tool"`
138-
139107
### Track tenant ID
140108

141109
Set at request entry point:

tests/core/unit/telemetry/test_init.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def test_module_exports(self):
1313
# Core functions
1414
assert hasattr(telemetry, 'record_request_metric')
1515
assert hasattr(telemetry, 'record_error_metric')
16-
assert hasattr(telemetry, 'record_aicore_metric')
17-
16+
1817
# Types
1918
assert hasattr(telemetry, 'Module')
2019
assert hasattr(telemetry, 'Operation')
@@ -42,7 +41,6 @@ def test_all_exports_in_all(self):
4241
'record_metrics',
4342
'record_request_metric',
4443
'record_error_metric',
45-
'record_aicore_metric',
4644
'auto_instrument',
4745
'context_overlay',
4846
'get_current_span',
@@ -58,7 +56,6 @@ def test_imports_work(self):
5856
from sap_cloud_sdk.core.telemetry import (
5957
record_request_metric,
6058
record_error_metric,
61-
record_aicore_metric,
6259
Module,
6360
Operation,
6461
GenAIOperation,
@@ -72,7 +69,6 @@ def test_imports_work(self):
7269
# All should be callable or types
7370
assert callable(record_request_metric)
7471
assert callable(record_error_metric)
75-
assert callable(record_aicore_metric)
7672
assert callable(record_metrics)
7773
assert callable(auto_instrument)
7874
assert callable(context_overlay)
@@ -102,7 +98,6 @@ def test_star_import(self):
10298
assert 'record_metrics' in test_module.__dict__
10399
assert 'record_request_metric' in test_module.__dict__
104100
assert 'record_error_metric' in test_module.__dict__
105-
assert 'record_aicore_metric' in test_module.__dict__
106101
assert 'auto_instrument' in test_module.__dict__
107102
assert 'context_overlay' in test_module.__dict__
108103
assert 'get_current_span' in test_module.__dict__

0 commit comments

Comments
 (0)