演示使用 OpenTelemetry Operator 自动注入 Boot 3.2 探针

解读

国内云原生面试高频考点集中在“零代码改动”可观测性落地能力。OpenTelemetry Operator 是 CNCF 孵化项目,在阿里 ACK、腾讯 TKE、华为 CCE 等商业 Kubernetes 发行版中均已内置。
Boot 3.2 原生支持 OTel Java Agent 1.32+,Operator 通过 Admission Webhook 在 Pod 创建阶段自动注入 agent 并补全 OTLP endpoint、服务名、资源属性等配置,实现业务 jar 无侵入的链路、指标、日志“三位一体”采集。
面试官真正想听的是:

  1. 如何在国产 K8s 环境里一键拉起观测体系;
  2. 注入后如何验证探针生效;
  3. 与 Spring Boot Actuator 的指标互补关系;
  4. 出现版本漂移或注入失败时的定位思路。

知识点

  1. OpenTelemetry Operator 架构:Admission Webhook + Instrumentation CRD + 镜像仓库加速(国内常用 registry.cn-hangzhou.aliyuncs.com/acs/opentelemetry-operator)。
  2. Boot 3.2 探针规范:io.opentelemetry.javaagent:opentelemetry-javaagent:1.32.0 支持 Spring6 与 JakartaEE,自动识别 management.otlp.tracing.endpoint。
  3. Instrumentation CR 关键字段:
    • exporter.endpoint 指向阿里云 SLS OTLP 网关或自建 VictoriaMetrics。
    • propagators=b3,w3c 兼容国内存量 SkyWalking 链路。
    • resource.addAttributes=deployment.environment=prod,service.namespace=order 满足金融租户隔离需求。
  4. Webhook 注入条件:命名空间标签 otel-injection=enabled + Pod 注解 sidecar.opentelemetry.io/inject=true。
  5. 验证手段:
    • 容器内 ps -ef | grep javaagent 确认 JVM 参数;
    • /actuator/health 返回 observabilityState=UP;
    • 日志出现 BatchSpanProcessor{exporter=OtlpGrpcSpanExporter, endpoint=http://otlp-collector:4317}。
  6. 排障三板斧:
    • Webhook 事件 kubectl describe pod 查看 FailedMount;
    • Operator 日志 kubectl logs -n opentelemetry-operator-system 搜 mutate pod error;
    • 镜像拉取失败可替换为国内加速器并配置 imagePullSecrets。

答案

步骤一:在阿里云 ACK 集群安装 Operator(版本 ≥ 0.85)

helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
helm upgrade --install otel-k operator/open-telemetry-operator \
  --namespace observability --create-namespace \
  --set manager.image.repository=registry.cn-hangzhou.aliyuncs.com/acs/opentelemetry-operator \
  --set admissionWebhooks.certManager.enabled=false \
  --set admissionWebhooks.autoGenerateCert=true

步骤二:创建全局 Instrumentation 自定义资源,适配国内 VPC 内网 OTLP 接收端

apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: java-instrumentation
  namespace: observability
spec:
  exporter:
    endpoint: http://sls-otlp.cn-shanghai.aliyuncs.com:4317
  propagators:
    - b3
    - w3c
  sampler:
    type: parentbased_traceidratio
    argument: "0.1"
  env:
    - name: OTEL_RESOURCE_ATTRIBUTES
      value: service.name=order-service,service.version=1.0.0,deployment.environment=prod

步骤三:给业务命名空间打标签,开启自动注入

kubectl label namespace order-prod otel-injection=enabled

步骤四:在 Boot 3.2 应用的 Deployment 模板里只加一行注解,不改镜像、不改代码

metadata:
  annotations:
    sidecar.opentelemetry.io/inject: "true"
spec:
  template:
    metadata:
      annotations:
        instrumentation.opentelemetry.io/inject-java: "true"

步骤五:应用发布后,验证探针生效

kubectl exec -it order-prod-xxxxx -- jinfo $(pgrep java) | grep javaagent
# 输出中包含 -javaagent:/otel-auto-instrumentation/javaagent.jar

访问 /actuator/metrics 可看到 otel_trace_span_created_total 指标,证明链路已上报。

拓展思考

  1. 多环境隔离:利用 Kustomize 组件补丁,在测试环境把 sampler.argument 调到 1.0,压测时全量采样;生产保持 0.1,节省 90% 流量费用。
  2. 与 Sentinel 流控联动:在 Instrumentation 的 env 里追加 OTEL_SPAN_ATTRIBUTE_ADD=sentinel.rule.id=${SENTINEL_RULE_ID},把限流事件作为属性写入同一 Trace,实现可观测性驱动的高可用闭环。
  3. 版本漂移防控:在 CI 阶段使用 operator-opamp-bridge 下发探针版本策略,拒绝启动携带过期 JavaAgent 的 Pod,满足金融级合规。
  4. 性能开销评估:官方基准 CPU +3%、内存 +30 M;国内某头部支付公司实测 QPS 下降 <1%,可通过 G1HeapRegionSize 与 InitialRAMPercentage 调优进一步降低。