Prompt
When implementing observability with OpenTelemetry, place attributes at the appropriate level in the telemetry hierarchy to ensure effective data collection and analysis:
- Use resource attributes for static, process-level information that applies globally:
# 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) - Use baggage only for appropriate correlation use cases:
# 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 - Avoid timestamps as metric attributes - timestamps are already handled by the telemetry system:
# Bad: Don't add timestamps to metrics new_attributes["processed_at"] = str(int(time.time())) # Avoid this! - Add instrument-specific attributes using the proper API methods:
# 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.