Skip to content

Commit 29c4ceb

Browse files
committed
fix: guard token histogram before recording metrics
1 parent c3a992e commit 29c4ceb

File tree

1 file changed

+5
-2
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents

1 file changed

+5
-2
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/span_processor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,10 @@ def _record_metrics(
542542
self, span: Span[Any], attributes: dict[str, AttributeValue]
543543
) -> None:
544544
"""Record metrics for the span."""
545-
if not self._metrics_enabled or not self._duration_histogram:
545+
if not self._metrics_enabled or (
546+
self._duration_histogram is None
547+
and self._token_usage_histogram is None
548+
):
546549
return
547550

548551
try:
@@ -580,7 +583,7 @@ def _record_metrics(
580583
}
581584

582585
# Record duration
583-
if duration is not None:
586+
if duration is not None and self._duration_histogram is not None:
584587
self._duration_histogram.record(duration, metric_attrs)
585588

586589
# Record token usage

0 commit comments

Comments
 (0)