use dedicated span per conversation_post for token usage tracking
Some checks failed
ai_core_api_client Python package / build (3.10) (push) Failing after 6s
ai_core_api_client Python package / build (3.11) (push) Failing after 4s
ai_core_api_client Python package / build (3.12) (push) Failing after 4s
ai_core_api_client Python package / build (3.13) (push) Failing after 4s
ai_core_api_client Python package / build (3.9) (push) Failing after 4s

This commit is contained in:
NikitolProject 2026-03-26 13:31:32 +03:00
parent 395bbdbaf8
commit 0099bc4bbd

View File

@ -27,15 +27,14 @@ from ai_core_api_client.rest import RESTResponseType
def _record_usage_to_span(response) -> None: def _record_usage_to_span(response) -> None:
"""Record token usage from ai-core-api response as OTel span attributes.""" """Record token usage from ai-core-api response as a dedicated OTel span."""
try: try:
usage = getattr(response, 'usage', None) usage = getattr(response, 'usage', None)
if usage is None: if usage is None:
return return
from opentelemetry import trace from opentelemetry import trace
span = trace.get_current_span() tracer = trace.get_tracer("ai-core-api-client")
if not span.is_recording(): with tracer.start_as_current_span("ai_core_api_usage") as span:
return
model = getattr(usage, 'model', None) model = getattr(usage, 'model', None)
if model: if model:
span.set_attribute("gen_ai.usage.model", model) span.set_attribute("gen_ai.usage.model", model)