Skip to content

Commit e887859

Browse files
committed
chore: reuse semantic attribute symbols in metrics
1 parent 29c4ceb commit e887859

File tree

1 file changed

+33
-13
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents

1 file changed

+33
-13
lines changed

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@
5757
from opentelemetry.semconv._incubating.attributes import (
5858
gen_ai_attributes as GenAIAttributes,
5959
)
60+
61+
try:
62+
from opentelemetry.semconv._incubating.attributes import (
63+
server_attributes as ServerAttributes,
64+
)
65+
except ImportError: # pragma: no cover - fallback for older semconv versions
66+
67+
class ServerAttributes:
68+
SERVER_ADDRESS = "server.address"
69+
SERVER_PORT = "server.port"
70+
71+
6072
from opentelemetry.trace import Span as OtelSpan
6173
from opentelemetry.trace import (
6274
SpanKind,
@@ -353,9 +365,9 @@ def _infer_server_attributes(base_url: Optional[str]) -> dict[str, Any]:
353365
try:
354366
parsed = urlparse(base_url)
355367
if parsed.hostname:
356-
out["server.address"] = parsed.hostname
368+
out[ServerAttributes.SERVER_ADDRESS] = parsed.hostname
357369
if parsed.port:
358-
out["server.port"] = parsed.port
370+
out[ServerAttributes.SERVER_PORT] = parsed.port
359371
except Exception:
360372
return out
361373
return out
@@ -483,9 +495,13 @@ def __init__(
483495
) and effective_base_url:
484496
server_attrs = _infer_server_attributes(effective_base_url)
485497
if not self.server_address:
486-
self.server_address = server_attrs.get("server.address")
498+
self.server_address = server_attrs.get(
499+
ServerAttributes.SERVER_ADDRESS
500+
)
487501
if not self.server_port:
488-
self.server_port = server_attrs.get("server.port")
502+
self.server_port = server_attrs.get(
503+
ServerAttributes.SERVER_PORT
504+
)
489505

490506
# Content capture configuration
491507
self._capture_messages = (
@@ -513,9 +529,9 @@ def _get_server_attributes(self) -> dict[str, Any]:
513529
"""Get server attributes from configured values."""
514530
attrs = {}
515531
if self.server_address:
516-
attrs["server.address"] = self.server_address
532+
attrs[ServerAttributes.SERVER_ADDRESS] = self.server_address
517533
if self.server_port:
518-
attrs["server.port"] = self.server_port
534+
attrs[ServerAttributes.SERVER_PORT] = self.server_port
519535
return attrs
520536

521537
def _init_metrics(self):
@@ -561,14 +577,18 @@ def _record_metrics(
561577

562578
# Build metric attributes
563579
metric_attrs = {
564-
"gen_ai.provider.name": attributes.get(GEN_AI_PROVIDER_NAME),
565-
"gen_ai.operation.name": attributes.get(GEN_AI_OPERATION_NAME),
566-
"gen_ai.request.model": (
580+
GEN_AI_PROVIDER_NAME: attributes.get(GEN_AI_PROVIDER_NAME),
581+
GEN_AI_OPERATION_NAME: attributes.get(GEN_AI_OPERATION_NAME),
582+
GEN_AI_REQUEST_MODEL: (
567583
attributes.get(GEN_AI_REQUEST_MODEL)
568584
or attributes.get(GEN_AI_RESPONSE_MODEL)
569585
),
570-
"server.address": attributes.get("server.address"),
571-
"server.port": attributes.get("server.port"),
586+
ServerAttributes.SERVER_ADDRESS: attributes.get(
587+
ServerAttributes.SERVER_ADDRESS
588+
),
589+
ServerAttributes.SERVER_PORT: attributes.get(
590+
ServerAttributes.SERVER_PORT
591+
),
572592
}
573593

574594
# Add error type if present
@@ -591,15 +611,15 @@ def _record_metrics(
591611
input_tokens = attributes.get(GEN_AI_USAGE_INPUT_TOKENS)
592612
if isinstance(input_tokens, (int, float)):
593613
token_attrs = dict(metric_attrs)
594-
token_attrs["gen_ai.token.type"] = "input"
614+
token_attrs[GEN_AI_TOKEN_TYPE] = "input"
595615
self._token_usage_histogram.record(
596616
input_tokens, token_attrs
597617
)
598618

599619
output_tokens = attributes.get(GEN_AI_USAGE_OUTPUT_TOKENS)
600620
if isinstance(output_tokens, (int, float)):
601621
token_attrs = dict(metric_attrs)
602-
token_attrs["gen_ai.token.type"] = "output"
622+
token_attrs[GEN_AI_TOKEN_TYPE] = "output"
603623
self._token_usage_histogram.record(
604624
output_tokens, token_attrs
605625
)

0 commit comments

Comments
 (0)