Resources

You are viewing the English version of this page because it has not yet been fully translated. Interested in helping out? See Contributing.

资源以资源属性的形式表示产生遥测数据的实体。例如, an OTP Release 产生的遥测数据运行在 Kubernetes 的容器中,那么它会具有 an OTP Release 的名称、Pod 名称、命名空间,可能还有部署名称。这四个属性都可以包含在资源中。

在你的可观测性后端中,你可以使用资源信息来更好地调查异常行为。例如, 如果你的追踪或指标数据表明系统中存在延迟,你可以将问题定位到特定的容器、Pod 或 Kubernetes 部署上。

Using resource detectors

Resource detectors fetch resource attributes from various sources. The default detectors use the OS environment variable OTEL_RESOURCE_ATTRIBUTES and the opentelemetry OTP application environment variable resource.

The detectors to use is a list of module names and can be configured in the Application configuration:

%% sys.config
{opentelemetry, {resource_detectors, [otel_resource_env_var, otel_resource_app_env]}}
## runtime.exs
config :opentelemetry, resource_detectors: [:otel_resource_env_var, :otel_resource_app_env]

Or through the environment variable OTEL_RESOURCE_DETECTORS:

OTEL_RESOURCE_DETECTORS=otel_resource_env_var,otel_resource_app_env

All resources detectors are protected with a timeout, in milliseconds, after which they return an empty value. This allows for resource detectors to do things like hit the network without potentially hanging the entire program indefinitely. The default is 5000 milliseconds and can be set with environment variable OTEL_RESOURCE_DETECTOR_TIMEOUT or Application variable otel_resource_detector_timeout.

Adding resources with OS and OTP application environment variables

With the two default resource detectors enabled you can set resource attributes either with the OS environment variable OTEL_RESOURCE_ATTRIBUTES:

OTEL_RESOURCE_ATTRIBUTES="deployment.environment=development"

Alternatively, use the resource OTP application environment under the opentelemetry Application configuration of sys.config or runtime.exs:

%% sys.config
{opentelemetry, {resource, #{deployment => #{environment => <<"development">>}}}}
## runtime.exs
config :opentelemetry, resource: %{deployment: %{environment: "development" }}

Resource attributes in the resource OTP application environment variable are flattened and combined with ., so #{deployment => #{environment => <<"development">> } is the same as #{'deployment.environment' => <<"development">>}.

Custom resource detectors

Custom resource detectors can be created by implementing the otel_resource_detector behaviour which contains a single callback get_resource/1 that returns an otel_resource.

Note that there are semantic conventions defined for resource that should be followed if they apply when adding new resource attributes.