When implementing observability with OpenTelemetry, place attributes at the appropriate level in the telemetry hierarchy to ensure effective data collection and analysis:
When implementing observability with OpenTelemetry, place attributes at the appropriate level in the telemetry hierarchy to ensure effective data collection and analysis:
# Good: Static information as resource attributes
resource = Resource.create({
"environment": "production",
"service.name": "api-server",
"service.version": "1.0.0"
})
meter_provider = MeterProvider(resource=resource)
# Good: Valid baggage usage
processor = BaggageMeasurementProcessor(baggage_keys=["user.id", "synthetic_request"])
# Bad: Don't use trace.id in baggage; use exemplars instead for trace correlation
# Bad: Don't add timestamps to metrics
new_attributes["processed_at"] = str(int(time.time())) # Avoid this!
# Good: Use proper attribute fields in API calls
meter = meter_provider.get_meter("name", "version", schema_url="url", attributes={"component": "billing"})
This approach reduces cardinality issues, improves query performance, and follows OpenTelemetry best practices for attribute placement.
Enter the URL of a public GitHub repository