资源

A resource represents the entity producing telemetry as resource attributes. For example, 一个 OTP Release producing telemetry that is running in a container on Kubernetes has 一个 OTP Release 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.

使用资源探测器

资源探测器从各种来源获取资源属性。默认的探测器使用操作系统环境变量OTEL_RESOURCE_ATTRIBUTESopentelemetry OTP 应用程序的环境变量resource

要使用的探测器是一个模块名称列表,并且可以在应用程序配置中进行配置:

%% 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]

或通过环境变量OTEL_RESOURCE_DETECTORS进行配置:

OTEL_RESOURCE_DETECTORS=otel_resource_env_var,otel_resource_app_env

所有资源探测器都受到超时保护,超过超时时间(以毫秒计),探测器将返回空值。这允许资源探测器执行需要耗时较长的操作,如网络请求,而不会导致整个程序无限期地挂起。默认超时时间为 5000 毫秒,可以通过环境变量OTEL_RESOURCE_DETECTOR_TIMEOUT或应用程序变量otel_resource_detector_timeout进行设置。

通过操作系统和应用程序环境变量添加资源

使用默认的两个资源探测器,可以使用操作系统环境变量OTEL_RESOURCE_ATTRIBUTES设置资源属性:

OTEL_RESOURCE_ATTRIBUTES="deployment.environment=development"

或者,在sys.configruntime.exsopentelemetry应用程序配置下使用resource应用程序环境:

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

resource应用程序环境变量中的资源属性会被扁平化并使用.进行连接,因此#{deployment => #{environment => <<"development">> }相当于#{'deployment.environment' => <<"development">>}

自定义资源探测器

可以通过实现otel_resource_detector behavior来创建自定义资源探测器,其中包含一个回调函数get_resource/1,它返回一个otel_resource

请注意,如果适用,应遵循资源的语义约定来添加新的资源属性。

最后修改 December 10, 2023: translate (a4350d6e)