Resources
A resource represents the entity producing telemetry as resource attributes. For example, a process producing telemetry that is running in a container on Kubernetes has a process name, a pod name, a namespace, and possibly a deployment name. All four of these attributes can be included in the resource.
In your observability backend, you can use resource information to better investigate interesting behavior. For example, if your trace or metrics data indicate latency in your system, you can narrow it down to a specific container, pod, or Kubernetes deployment.
If you use the Java agent for automatic instrumentation you can learn how to setup resource detection following the Agent Configuration Guide.
For manual instrumentation, you will find some introductions on how to set up resource detection below.
Detecting resources from common environments
You can use ResourceProvider
s for filling in attributes related to common
environments, like Container,
Host or
Operating System. These can be used with or
without
autoconfiguration.
To use those providers, add the following dependency:
dependencies {
implementation("io.opentelemetry.instrumentation:opentelemetry-resources:1.36.0-alpha");
}
<project>
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-resources</artifactId>
</dependency>
</dependencies>
</project>
Next you can use them like the following in your code:
import io.opentelemetry.instrumentation.resources.ContainerResource;
import io.opentelemetry.instrumentation.resources.HostResource;
import io.opentelemetry.instrumentation.resources.OsResource;
import io.opentelemetry.instrumentation.resources.ProcessResource;
import io.opentelemetry.instrumentation.resources.ProcessRuntimeResource;
...
Resource resource = Resource.getDefault()
.merge(ContainerResource.get())
.merge(HostResource.get())
.merge(OsResource.get())
.merge(ProcessResource.get())
.merge(ProcessRuntimeResource.get())
.merge(Resource.create(Attributes.builder()
.put(ResourceAttributes.SERVICE_NAME, "dice-service")
...
.build()));
...
Adding resources in code
Custom resources can be configured in your code like the following:
Resource resource = Resource.getDefault()
.merge(Resource.create(Attributes.builder()
.put(ResourceAttributes.SERVICE_NAME, "dice-service")
.put(ResourceAttributes.SERVICE_VERSION, "0.1.0")
.put(ResourceAttributes.SERVICE_INSTANCE_ID, "dice-service-1")
.put(ResourceAttributes.HOST_NAME, System.getenv("HOSTNAME"))
.put(ResourceAttributes.PROCESS_PID, ProcessHandle.current().pid())
.build()));
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setResource(resource)
...
.build();
SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
.setResource(resource)
...
.build();
SdkLoggerProvider sdkLoggerProvider = SdkLoggerProvider.builder()
.setResource(resource)
...
.build();