Free — no signup required

The Semantic Conventions Package: Using Constants in Code

1 min read

Avoiding Magic Strings

Writing "service.name" as a raw string in your code is fragile. A typo like "service_name" or "servicename" will silently produce non-compliant telemetry — no error, no warning, just missing data in your dashboards.

Every major OTel SDK ships a semantic conventions package that provides typed constants for all standard attribute names. Use these constants instead of raw strings.

In Python, the opentelemetry-semantic-conventions package exposes ResourceAttributes and SpanAttributes constants, used the same way you'd use any typed constant in place of a raw dictionary or attribute key. In Go, the versioned go.opentelemetry.io/otel/semconv package exposes constants like semconv.DBSystemPostgreSQL and key-builders like semconv.DBOperationKey.String(...). In Java, io.opentelemetry:opentelemetry-semconv exposes a SemanticAttributes class with constants like DB_SYSTEM and HTTP_RESPONSE_STATUS_CODE. All three achieve the same goal through their language's idiomatic constant mechanism.

Notice that Go's semconv package is versioned (v1.21.0). This is intentional — when the conventions release a new version, you explicitly opt in by updating the import. This prevents silent breaking changes when attribute names are renamed between convention versions.

Key principle: If your IDE can't autocomplete the attribute name, you're probably using a raw string. Switch to the constants package.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free