资源
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.
如果您使用Java代理进行自动检测,您可以学习如何设置资源检测,按照代理配置指南。
对于手动检测,您可以在下面找到一些设置资源检测的介绍。
从常见环境中检测资源
您可以使用ResourceProvider
来填充与常见环境相关的属性,如容器、主机或操作系统。这些可以与或不使用自动配置。
要使用这些提供者,请添加以下依赖项:
dependencies {
implementation("io.opentelemetry.instrumentation:opentelemetry-resources:1.31.0-alpha");
}
<project>
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-resources</artifactId>
</dependency>
</dependencies>
</project>
接下来,您可以在代码中像下面这样使用它们:
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()));
...
在代码中添加资源
可以在代码中配置自定义资源,如下所示:
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();