对比 Boot 3.2 内置的 OTel 自动配置与手动引入 io.opentelemetry.spring-boot 的差异
解读
面试官想验证两件事:
- 你是否知道 Spring Boot 3.2 已经把 OpenTelemetry 自动配置 收编进官方代码,无需额外 starter;
- 你是否能讲清楚“官方内置”与“社区 starter”在 依赖坐标、自动装配类、Bean 覆盖规则、可观测性后端集成方式、生产级开关 五个维度的差异。
答不出“内置”与“手动”在 MeterProvider、TracerProvider、Resource、Sampler、Exporter 这五个核心接口的注册顺序差异,基本会被判为“只背八股”。
知识点
- spring-boot-starter-actuator 3.2 自带
OtlpAutoConfiguration,默认只注册 OtlpHttpSpanExporter,不注册 metrics/logs exporter。 - io.opentelemetry.spring-boot-starter(社区 starter)通过
OpenTelemetryAutoConfiguration一次性把 tracer、meter、logger 三套 SDK 全部初始化,并额外提供 Zipkin、Jaeger、Prometheus 快速切换 的 profile。 - 内置版把 Resource 的构建委托给
org.springframework.boot.actuate.autoconfigure.tracing.DefaultTracingProperties,只能读到spring.application.name;社区 starter 允许用otel.resource.attributes=service.namespace=xxx,service.version=yyy任意扩展。 - Sampler 规则:内置版默认
parentbased_always_on,改采样率必须自己声明OtlpHttpSpanExporterBean;社区 starter 直接暴露otel.traces.sampler=traceidratio与otel.traces.sampler.arg=0.1两行配置即可。 - Bean 覆盖优先级:社区 starter 的
OpenTelemetryBean 声明在io.opentelemetry.sdk.autoconfigure包,order=0,会被用户自定义@Primary覆盖;内置版声明在org.springframework.boot.actuate.autoconfigure.tracing.otlp,order=100,用户 Bean 优先级更高,不会出现“starter 想覆盖却失败”的幽灵问题。 - 国内可观测性后端兼容性:内置版默认指向
http://localhost:4318/v1/traces,阿里云 SLS、腾讯云 CLS、华为云 AOM 的 gRPC 端口 4317 需要手动改地址;社区 starter 在application-ali-cloud.properties里预置了otel.exporter.otlp.endpoint=https://tracing-cn-shanghai.aliyuncs.com:4317,开箱即配。 - License 与漏洞治理:内置版随 Spring Boot 统一升级,国内金融公司安全审计只需看 Spring 官方 CVE;社区 starter 额外引入
io.opentelemetry.instrumentation:opentelemetry-instrumentation-api,需要单独跟踪 30+ 子模块漏洞,国央企过等保 2.0 时会被安全部打回重审。
答案
“Boot 3.2 把 OTel 收编后,官方只提供最小可用的 trace exporter,metrics 与 logs 仍要用户自己补 Bean;而社区 starter 一口气把 trace、metric、log 三套 SDK 全配好,并给出 Zipkin、Jaeger、Prometheus 的 profile 一键切换。
关键差异在五点:
- 依赖坐标:内置版只需
spring-boot-starter-actuator,不额外引入io.opentelemetry.spring-boot-starter,Maven 冲突减少 8 个 jar; - Resource 维度:内置版只能读到
spring.application.name,社区 starter 支持otel.resource.attributes任意 KV,方便在阿里 SLS 里按 namespace 聚合; - Sampler 热改:内置版必须写 Java Config,社区 starter 直接
otel.traces.sampler=traceidratio一行配置即可把采样率从 1 降到 0.01,双 11 大促时运维最爱; - Bean 优先级:社区 starter 的
OpenTelemetryBean order=0,会被用户自定义 @Primary 静默覆盖,排错困难;内置版 order=100,用户 Bean 永远优先生效,符合国央企“配置大于约定”的审计要求; - 国内后端地址:社区 starter 在
application-ali-cloud.properties里预置了 华东 1 的 4317 gRPC 地址,云原生上生产直接打包镜像即可;内置版默认 localhost:4318,每次上云都要改 YAML,容易踩坑。
一句话总结:想快速接入、不怕额外漏洞审计,用社区 starter;想跟着 Spring 官方节奏、过等保、少 jar,用 Boot 3.2 内置版。”
拓展思考
如果面试官继续追问“如何把内置版升级到同时支持 metrics 与 logs 并推送到腾讯云 CLS”,可以答:
- 手动声明
OtlpGrpcMetricExporter与OtlpGrpcLogExporter,注意端口是 4317 而非 4318; - 把
spring.application.name通过otel.resource.attributes=service.name=${spring.application.name},deployment.environment=${spring.profiles.active}补齐成腾讯云要求的标签; - 关闭社区 starter 的自动装配 避免重复 Bean,用
spring.autoconfigure.exclude=io.opentelemetry.instrumentation.spring.boot.autoconfigure.OpenTelemetryAutoConfiguration
彻底隔离; - 通过 Spring Boot 3.2 新特性
ManagementOtlpProperties统一读写 endpoint、headers,把鉴权 Token 放到环境变量OTEL_EXPORTER_OTLP_HEADERS=Authentication=xxxx,符合国内金融“密文不进代码库”的合规要求。