使用 OpenTelemetry 采集 N1QL 追踪的完整配置?
解读
面试官抛出这道题,核心想验证三件事:
- 你是否真的在生产环境把 Couchbase 的 N1QL 语句当成“黑盒”治理过;
- 对 OpenTelemetry 的 Collector 管线设计、SDK 注入、上下文传播 是否熟悉;
- 能否把“追踪”与“国内可观测性落地”打通——包括合规脱敏、性能损耗、国产化芯片/操作系统适配等现实痛点。
回答时切忌只贴 YAML,要体现“完整”二字:从 SDK → Collector → 后端 → 国内合规闭环,每一步都要给出可落地的参数与踩坑提示。
知识点
- Couchbase Server 6.5+ 内置 Query Service 级追踪开关(query_trace_level)与 OpenTelemetry exporter(CB 7.1+ 官方提供)。
- OpenTelemetry Java SDK 1.29+ 支持 自动织入 N1QL 驱动(couchbase-java-client 3.4+ 已原生实现 io.opentelemetry.instrumentation)。
- W3C TraceContext 与 baggage 在 N1QL 参数中的透传规则(traceparent 作为客户端上下文变量,须开启 query_context_propagation)。
- Collector 国内镜像源:可使用 阿里云 ACR、腾讯云 TCR 同步 otel/opentelemetry-collector-contrib:0.88,避免 DockerHub 拉取超时。
- 脱敏合规:N1QL 语句含手机号、身份证时,须在 Collector 中使用 transform processor + 正则脱敏,否则等保 2.0 测评直接挂。
- 性能:开启追踪后 Query Service CPU 增加 3%–7%,需设置 sample_ratio=0.1 并配合 adaptive sampling;ARM 鲲鹏 920 场景下要打开 -XX:+UseG1GC 降低停顿。
答案
下面给出一条从“应用代码”到“国内合规后端”的最小可运行闭环,可直接写进面试白板,既显深度又易落地。
-
开启 Couchbase Server 端追踪
在 7.1+ 集群的 Settings → Query Settings 里勾选
“OpenTelemetry integration”
或 curl 方式:curl -X POST -u admin:pwd http://<cb-host>:8091/settings/querySettings \ -d 'queryTraceLevel=traces' \ -d 'queryOtlpEndpoint=http://otel-collector:4317' \ -d 'queryOtlpHeaders=authorization=Bearer%20abc'注意:若集群版本 ≤7.0,需手动在 query.json 加
"query_trace_level": "traces" 并重启 Query Service。 -
应用侧自动注入(Spring Boot 为例)
pom.xml 引入<dependency> <groupId>com.couchbase.client</groupId> <artifactId>couchbase-java-client</artifactId> <version>3.4.11</version> </dependency> <dependency> <groupId>io.opentelemetry.instrumentation</groupId> <artifactId>opentelemetry-couchbase-3.4</artifactId> <version>1.29.0-alpha</version> </dependency>在 application.yml 打开
couchbase: tracing: enabled: true open-telemetry: service-name: payment-service include-statement: true # 生产建议 false,脱敏后再开启动参数加
-Dotel.traces.exporter=otlp -Dotel.exporter.otlp.endpoint=http://otel-collector:4317 -
Collector 配置(otel-collector-config.yaml,国内镜像)
receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 processors: batch: timeout: 1s send_batch_size: 512 transform: statements: - replace_pattern(attributes["db.statement"], "(\\d{15}|\\d{18})", "***ID***") exporters: otlp/elastic: endpoint: https://es-cn-xxxx.kibana.aliyuncs.com:9200 headers: Authorization: "Bearer ${ES_TOKEN}" service: pipelines: traces: receivers: [otlp] processors: [transform, batch] exporters: [otlp/elastic]部署命令(鲲鹏 ARM)
docker run -d --name otel-collector \ -v $(pwd)/otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml \ registry.cn-hangzhou.aliyuncs.com/acs/opentelemetry-collector-contrib:0.88-arm64 -
验证
在 Kibana 的 APM → Traces 里搜索
service.name : "payment-service" AND db.operation : "SELECT"
能看到耗时、N1QL 语句(已脱敏)、Couchbase 节点 IP、索引命中情况;
同时 trace.timeline 里包含
dispatch→parse→plan→indexScan→fetch→serialize 全链路 Span,证明追踪生效。 -
国内合规 checklist
- 语句脱敏已用 transform processor,满足 《个人信息保护法》 最小可用原则;
- 采样率 10%,存储周期 7 天,符合 等保 2.0 三级 审计要求;
- Collector 与 Couchbase 通信走 内网 DNS,4317 端口未暴露公网,通过 云防火墙 仅放行业务 VPC。
拓展思考
- 如果公司用的是 Go 微服务,couchbase-golang-sdk 2.6+ 同样支持 otel,但需在 ClusterOptions.TracerConfig 里手动注入 otlpgrpc.NewExporter,并注意 goroutine 泄漏 问题(官方 issue #266)。
- 当集群规模 >100 节点时,建议把 Collector 拆成 Agent(DaemonSet)+ Gateway(Deployment) 两层,避免单点;Gateway 层加 kafkaexporter 做缓冲,可扛住 双 11 峰值 60k spans/s。
- 国内银行去 IOE 场景下,Couchbase 跑在 麒麟 V10 + 鲲鹏 920,需把 Collector 编译成 ARM64 可执行文件 并打开 G1GC + PMU 硬件计数器,才能把 CPU 损耗压到 3% 以内。