Resource attributes provide contextual information about the source of your logs, such as service name, environment, hostname, and deployment details. These attributes enable better filtering, grouping, and correlation of logs in SigNoz. You can set resource attributes at different levels in your observability stack.
Setting Resource Attributes at the Collector Level
The OpenTelemetry Collector provides several processors to add or modify resource attributes for incoming log data.
Using Resource Processor
The resource processor allows you to add, modify, or delete resource attributes. This configuration adds static resource attributes to all logs processed by the collector:
processors:
resource:
attributes:
- key: service.name
value: "my-log-service"
action: upsert
- key: deployment.environment
value: "production"
action: insert
- key: host.name
from_attribute: "hostname"
action: upsert
service:
pipelines:
logs:
receivers: [otlp]
processors: [resource, batch]
exporters: [otlp]
This configuration adds a service name and environment to all logs, and copies the hostname from an existing attribute. The action
parameter determines how the attribute is handled (insert, update, or upsert).
Using Resource Detection Processor
The resource detection processor automatically detects resource information from the host environment. This processor can detect attributes like hostname, OS information, cloud provider details, and Kubernetes metadata:
processors:
resourcedetection:
detectors: [env, host, system, docker, k8snode]
system:
hostname_sources: ["os"]
timeout: 5s
service:
pipelines:
logs:
receivers: [otlp]
processors: [resourcedetection, batch]
exporters: [otlp]
This processor automatically adds resource attributes by detecting information from the environment, host system, Docker containers, and Kubernetes nodes. The timeout
parameter controls how long the processor waits for detection to complete.
Setting Resource Attributes at the SDK Level
You can configure resource attributes directly in your application code using OpenTelemetry SDKs. This approach gives you fine-grained control over the attributes for each service.
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
// Create a resource with custom attributes
const resource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'my-log-service',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: 'production',
'custom.team': 'backend-team',
'custom.region': 'us-west-2'
});
// Initialize the SDK with the resource
const sdk = new NodeSDK({
resource: resource,
logRecordProcessor: new BatchLogRecordProcessor(
new OTLPLogExporter({
url: 'http://localhost:4318/v1/logs',
})
),
});
sdk.start();
This configuration creates a resource with both standard semantic conventions (service name, version, environment) and custom attributes (team, region). These attributes will be attached to all logs generated by this service.
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter
# Create a resource with custom attributes
resource = Resource.create({
ResourceAttributes.SERVICE_NAME: "my-log-service",
ResourceAttributes.SERVICE_VERSION: "1.0.0",
ResourceAttributes.DEPLOYMENT_ENVIRONMENT: "production",
"custom.team": "backend-team",
"custom.region": "us-west-2"
})
# Initialize logger provider with the resource
logger_provider = LoggerProvider(resource=resource)
set_logger_provider(logger_provider)
# Add log exporter
logger_provider.add_log_record_processor(
BatchLogRecordProcessor(
OTLPLogExporter(endpoint="http://localhost:4317")
)
)
This Python configuration creates resource attributes using the OpenTelemetry semantic conventions and custom attributes. The resource is then attached to the logger provider, ensuring all logs include these attributes.
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
// Create a resource with custom attributes
Resource resource = Resource.getDefault()
.merge(Resource.builder()
.put(ResourceAttributes.SERVICE_NAME, "my-log-service")
.put(ResourceAttributes.SERVICE_VERSION, "1.0.0")
.put(ResourceAttributes.DEPLOYMENT_ENVIRONMENT, "production")
.put("custom.team", "backend-team")
.put("custom.region", "us-west-2")
.build());
// Initialize logger provider with the resource
SdkLoggerProvider loggerProvider = SdkLoggerProvider.builder()
.setResource(resource)
.addLogRecordProcessor(BatchLogRecordProcessor.builder(
OtlpGrpcLogRecordExporter.builder()
.setEndpoint("http://localhost:4317")
.build())
.build())
.build();
// Build the OpenTelemetry SDK
OpenTelemetrySdk sdk = OpenTelemetrySdk.builder()
.setLoggerProvider(loggerProvider)
.build();
This Java configuration uses the builder pattern to create resource attributes and configure the OpenTelemetry SDK. The resource attributes are merged with default attributes and attached to all logs from this application.
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Logs;
// Create a resource with custom attributes
var resource = ResourceBuilder.CreateDefault()
.AddService("my-log-service", "1.0.0")
.AddAttributes(new Dictionary<string, object>
{
["deployment.environment"] = "production",
["custom.team"] = "backend-team",
["custom.region"] = "us-west-2"
})
.Build();
// Configure logging with OpenTelemetry
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService("my-log-service", "1.0.0")
.AddAttributes(new Dictionary<string, object>
{
["deployment.environment"] = "production",
["custom.team"] = "backend-team",
["custom.region"] = "us-west-2"
}));
options.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("http://localhost:4318/v1/logs");
});
});
});
This .NET configuration uses the ResourceBuilder to create custom resource attributes. The AddService method automatically adds service name and version, while AddAttributes allows you to specify custom resource attributes that will be included with all logs.
Using Environment Variables
You can set resource attributes using environment variables, which is particularly useful for containerized deployments and CI/CD pipelines:
Multiple attributes in single line
# Set multiple resource attributes in a single environment variable
export OTEL_RESOURCE_ATTRIBUTES="service.name=my-log-service,service.version=1.0.0,deployment.environment=production,custom.team=backend"
Single attribute
# Set service name separately
export OTEL_SERVICE_NAME="my-log-service"
These environment variables are automatically picked up by OpenTelemetry SDKs and applied to all telemetry data, including logs. This approach is ideal for deployment scenarios where you want to configure resource attributes without modifying code.
Using Kubernetes
The K8s infra chart enables a lot of these resource attributes by default. You can find the complete list of attributes here.
When deploying applications in Kubernetes, you can use the OpenTelemetry Operator to automatically inject resource attributes:
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
name: my-instrumentation
namespace: default
spec:
resource:
addK8sUIDAttributes: true
resourceAttributes:
deployment.environment: "production"
custom.team: "backend-team"
env:
- name: OTEL_RESOURCE_ATTRIBUTES
value: "service.version=1.0.0"
This configuration automatically adds Kubernetes-specific resource attributes (like pod name, namespace, deployment name) and custom attributes to all instrumented applications in the cluster. The addK8sUIDAttributes
option includes unique identifiers that help correlate logs with Kubernetes resources.
To apply this instrumentation to a deployment, add the following annotation:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-application
spec:
template:
metadata:
annotations:
instrumentation.opentelemetry.io/inject-sdk: "my-instrumentation"
spec:
containers:
- name: app
image: my-app:latest
This approach ensures that all logs from your Kubernetes applications include consistent resource attributes for filtering and correlation in SigNoz.