SDK 配置

此 Spring Starter 支持 配置元数据,这意味着您可以在 IDE 中查看并自动完成所有可用属性。

通用配置

OpenTelemetry Starter 支持所有 SDK 自动配置(自 2.2.0 起)。

您可以通过 application.propertiesapplication.yaml 文件中的属性,或通过环境变量来更新配置。

application.properties 示例

otel.propagators=tracecontext,b3
otel.resource.attributes.deployment.environment=dev
otel.resource.attributes.service.name=cart
otel.resource.attributes.service.namespace=shop

application.yaml 示例

otel:
  propagators:
    - tracecontext
    - b3
  resource:
    attributes:
      deployment.environment: dev
      service:
        name: cart
        namespace: shop

环境变量示例

export OTEL_PROPAGATORS="tracecontext,b3"
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=dev,service.name=cart,service.namespace=shop"

覆盖资源属性

与往常一样在 Spring Boot 中,您可以使用环境变量覆盖 application.propertiesapplication.yaml 文件中的属性。

例如,您可以通过设置标准的 OTEL_RESOURCE_ATTRIBUTES 环境变量来设置或覆盖 deployment.environment 资源属性(不更改 service.nameservice.namespace)。

export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=prod"

或者,您可以使用 OTEL_RESOURCE_ATTRIBUTES_DEPLOYMENT_ENVIRONMENT 环境变量来设置或覆盖单个资源属性。

export OTEL_RESOURCE_ATTRIBUTES_DEPLOYMENT_ENVIRONMENT="prod"

第二个选项支持 SpEL 表达式。

请注意,DEPLOYMENT_ENVIRONMENT 会通过 Spring Boot 的 Relaxed Binding 转换为 deployment.environment

禁用 OpenTelemetry Starter

系统属性otel.sdk.disabled环境变量OTEL_SDK_DISABLED

描述:将值设置为 true 以禁用 Starter,例如用于测试目的。

以编程方式配置

您可以使用 AutoConfigurationCustomizerProvider 进行以编程方式配置。以编程方式配置推荐用于高级用例,而这些用例无法通过属性进行配置。

从跟踪中排除 actuator 端点

例如,您可以自定义采样器以从跟踪中排除健康检查端点。

<dependencies>
  <dependency>
    <groupId>io.opentelemetry.contrib</groupId>
    <artifactId>opentelemetry-samplers</artifactId>
    <version>1.33.0-alpha</version>
  </dependency>
</dependencies>
dependencies {
  implementation("io.opentelemetry.contrib:opentelemetry-samplers:1.33.0-alpha")
}
package otel;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.semconv.UrlAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterPaths {

  @Bean
  public AutoConfigurationCustomizerProvider otelCustomizer() {
    return p ->
        p.addSamplerCustomizer(
            (fallback, config) ->
                RuleBasedRoutingSampler.builder(SpanKind.SERVER, fallback)
                    .drop(UrlAttributes.URL_PATH, "^/actuator")
                    .build());
  }
}

以编程方式配置导出器

您也可以以编程方式配置 OTLP 导出器。此配置将替换默认的 OTLP 导出器,并在请求中添加自定义标头。

package otel;

import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import java.util.Collections;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomAuth {
  @Bean
  public AutoConfigurationCustomizerProvider otelCustomizer() {
    return p ->
        p.addSpanExporterCustomizer(
            (exporter, config) -> {
              if (exporter instanceof OtlpHttpSpanExporter) {
                return ((OtlpHttpSpanExporter) exporter)
                    .toBuilder().setHeaders(this::headers).build();
              }
              return exporter;
            });
  }

  private Map<String, String> headers() {
    return Collections.singletonMap("Authorization", "Bearer " + refreshToken());
  }

  private String refreshToken() {
    // e.g. read the token from a kubernetes secret
    return "token";
  }
}

资源提供者

OpenTelemetry Starter 包含与 Java Agent 相同的资源提供者。

此外,OpenTelemetry Starter 还包含以下特定于 Spring Boot 的资源提供者:

分发资源提供者

FQN:io.opentelemetry.instrumentation.spring.autoconfigure.resources.DistroVersionResourceProvider

属性
telemetry.distro.nameopentelemetry-spring-boot-starter
telemetry.distro.versionstarter 的版本

Spring 资源提供者

FQN:io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringResourceProvider

属性
service.namespring.application.namebuild.name(来自 build-info.properties,请参阅 服务名称
service.versionbuild.version(来自 build-info.properties

服务名称

使用这些资源提供者,服务名称将根据以下优先规则确定,符合 OpenTelemetry 规范

  1. otel.service.name Spring 属性或 OTEL_SERVICE_NAME 环境变量(最高优先级)
  2. otel.resource.attributes 系统/Spring 属性或 OTEL_RESOURCE_ATTRIBUTES 环境变量中的 service.name
  3. spring.application.name Spring 属性
  4. build-info.properties
  5. META-INF/MANIFEST.MF 中的 Implementation-Title
  6. 默认值为 unknown_service:java(最低优先级)。

在您的 pom.xml 文件中使用以下代码段生成 build-info.properties 文件:

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
springBoot {
  buildInfo {
  }
}