

新闻资讯
技术学院Go服务接入OpenTelemetry需显式透传context:服务端用otelhttp.NewHandler,客户端用otelhttp.Client.Do(req.WithContext(ctx)),并设置全局propagator;gRPC需用otelgrpc拦截器及metadata.AppendToOutgoingContext;go-micro需改用k8s registry;Istio熔断需对齐http.Transport配置。
Go 应用在云原生环境中若不主动注入上下文传播逻辑,otelhttp 中间件捕获的 span 将无法跨服务串联,表现为「单跳 trace」或 trace_id 断裂。关键不在是否引入 SDK,而在 HTTP 客户端是否使用了带 context 透传的封装。
otelhttp.NewHandler() 包裹 http.ServeMux,且 handler 内部所有下游调用需从 r.Context() 提取 span 并显式传递http.DefaultClient.Do(req),要改用 otelhttp.Client.Do(req.WithContext(ctx)),其中 ctx 来自 propagators.Extract(r.Context(), r.Header)
otel.SetTextMapPropagator(otelpropagation.NewCompositeTextMapPropagator(otelpropagation.TraceContext{}, otelpropagation.Baggage{})),否则 Kubernetes Service Mesh(如 Istio)注入的 b3 或 w3c header 会被忽略import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) func initTracer() { exporter, _ := otlptracehttp.NewClient( otlptracehttp.WithEndpoint("otel-collector:4318"), otlptracehttp.WithInsecure(), ) tp := trace.NewTracerProvider( trace.WithBatcher(exporter), ) otel.SetTracerProvider(tp) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( propagation.TraceContext{}, propagation.Baggage{}, )) }
gRPC 的 metadata.MD 默认不自动携带 OpenTelemetry 的 trace context,即使两端都启用了 otelgrpc,若未显式将 context.Context 注入 client stub,span 仍会断开。这不是配置问题,是 Go 的 context 机制与 gRPC 拦截器协作方式决定的。
otelgrpc.UnaryServerInterceptor(),且 handler 函数签名保持为 func(ctx context.Context, req interface{}) (interface{}, error) —— 丢掉 ctx 参数就等于丢掉 tracemd := metadata.Pairs("traceparent", "00-...") 不行;正确做法是 ctx = metadata.AppendToOutgoingContext(ctx, "key", "val") 配合 propagator 自动序列化b3 和 w3c header;gRPC 的 binary metadata 不被识别,必须启用 envoy.filters.http.grpc_http1_reverse_bridge 或改用 text 编码模式go-micro/v4 默认使用 mdns 或 etcd 插件做服务发现,在 K8s 里直接部署会导致节点间无法互相发现 —— 因为 Pod IP 是动态的、Service DNS 名称未被解析进 registry,且 micro.Service 的 Address 字段若填 localhost:8080,其他服务根本连不上。
micro.Registry(nil),改用 kubernetes.NewRegistry()(来自 github.com/micro/go-micro/v4/registry/kubernetes)os.Getenv("MY_POD_IP"),并设为 micro.Address(fmt.Sprintf("%s:%d", ip, port))
endpoints 和 services 的 list/watch 权限,否则 registry 初始化失败但日志无提示Istio 的 DestinationRule 熔断策略(如 connectionPool.maxConnections)对 Go 应用无效,不是 Istio bug,而是 Go net/http 默认复用连接池,且未设置 http.Transport.MaxIdleConnsPerHost 与 Istio sidecar 的连接限制对齐,导致请求绕过熔断检测。
http.Transport:将 MaxIdleConnsPerHost 设为略小于 Istio 的 maxConnections(例如 Istio 设 100,则 Go 客户端设 95)ForceAttemptHTTP2: false,因 Istio 对 h2 的 connection pool 统计不一致istioctl analyze 是否报 IST0118(未启用 mTLS),mTLS 关闭时部分熔断指标不上报服务治理不是堆 SDK,而是理解 Go 运行时行为、K8s 网络模型和 mesh 数据面之间的耦合点。最容易被忽略的是:所有 context 透传都依赖开发者在每一层手动提取和注入,没有“自动魔法”。