OpenTelemetry Resource Detector Not Populating Fix
In this tutorial, you'll learn about OpenTelemetry Resource Detector Not Populating Fix. We cover key concepts, practical examples, and best practices.
Your OpenTelemetry spans show service.name as unknown_service — the resource detector did not run, or custom resource attributes override detected values. Resources identify the service and environment that produced telemetry.
The Problem
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
# WRONG: no resource configured
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
All spans show service.name: unknown_service — the resource has no identifying information.
Step-by-Step Fix
1. Create a resource with service name
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
resource = Resource.create({
"service.name": "payment-service",
"service.version": "1.2.3",
"deployment.environment": "production",
})
provider = TracerProvider(resource=resource)
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
2. Use resource detectors
from opentelemetry.sdk.resources import (
Resource,
OTELResourceDetector,
ProcessResourceDetector,
)
from opentelemetry.sdk.trace import TracerProvider
# Auto-detect resource attributes from environment and process
detected = Resource.create(
OTELResourceDetector().detect()
).merge(
ProcessResourceDetector().detect()
)
# Merge with custom attributes (custom takes priority)
resource = detected.merge(Resource.create({
"service.name": "payment-service",
"service.namespace": "prod-us-east",
}))
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)
3. Configure resource via environment variables
# Set resource attributes as environment variables
export OTEL_RESOURCE_ATTRIBUTES=service.name=payment-service,service.version=1.2.3
export OTEL_SERVICE_NAME=payment-service # Shorthand for service.name
# The SDK automatically reads these on initialization
opentelemetry-instrument python app.py
4. Combine multiple resource detectors
from opentelemetry.sdk.resources import (
Resource,
OTELResourceDetector,
ProcessResourceDetector,
HostResourceDetector,
OsResourceDetector,
ContainerResourceDetector,
)
resource = Resource.create({}).merge(
OTELResourceDetector().detect()
).merge(
ProcessResourceDetector().detect()
).merge(
HostResourceDetector().detect()
).merge(
OsResourceDetector().detect()
).merge(
ContainerResourceDetector().detect()
).merge(
Resource.create({
"service.name": "payment-service",
"service.namespace": "production",
})
)
5. Verify exported resource
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
resource = Resource.create({
"service.name": "payment-service",
"service.instance.id": "pod-abc123",
"host.name": "worker-1",
})
provider = TracerProvider(resource=resource)
# Print resource attributes
for key, value in resource.attributes.items():
print(f"{key}: {value}")
Expected output:
service.name: payment-service
service.version: 1.2.3
service.instance.id: pod-abc123
deployment.environment: production
host.name: worker-1
process.pid: 12345
process.runtime.name: cpython
process.runtime.version: 3.11.4
Prevention Tips
- Always set
service.name— every other resource attribute is optional - Use environment variables (
OTEL_RESOURCE_ATTRIBUTES) for deployment-specific attributes - Merge custom attributes AFTER detected attributes to override defaults
- Use resource detectors for automatic host/process/container metadata
- Verify resource attributes in your tracing backend's service list
Common Mistakes with resource detector
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world OPENTELEMETRY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro