Alanxtl commented on code in PR #3838: URL: https://github.com/apache/dubbo-go/pull/3838#discussion_r4640499821
########## filter/otel/trace/cluster_interceptor.go: ########## @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package trace + +import ( + "context" + "errors" + "strings" + "sync" +) + +import ( + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" +) + +import ( + cluster "dubbo.apache.org/dubbo-go/v3/cluster/cluster" + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/protocol/base" + "dubbo.apache.org/dubbo-go/v3/protocol/result" +) + +func init() { + cluster.SetClusterInterceptor(constant.OTELClientTraceKey, func() cluster.Interceptor { + return &clusterInterceptor{TracerProvider: otel.GetTracerProvider()} + }) +} + +type clusterInterceptor struct{ TracerProvider trace.TracerProvider } + +func referenceURL(url *common.URL) *common.URL { + if url != nil && url.SubURL != nil { + return url.SubURL + } + return url +} + +func clientTracingEnabled(url *common.URL) bool { + if url == nil { + return false + } + enabled := false + for name := range strings.SplitSeq(url.GetParam(constant.ReferenceFilterKey, ""), ",") { + switch strings.TrimSpace(name) { + case "-" + constant.OTELClientTraceKey: + return false + case constant.OTELClientTraceKey: + enabled = true + } + } + return enabled +} + +func (i *clusterInterceptor) Invoke(ctx context.Context, invoker base.Invoker, invocation base.Invocation) result.Result { + url := referenceURL(invoker.GetURL()) + if !clientTracingEnabled(url) { + if ctx != nil { + ctx = cluster.WithDiagnostics(ctx, nil, nil) + } + return invoker.Invoke(ctx, invocation) + } + if ctx == nil { + ctx = context.Background() + } + ctx, span := i.TracerProvider.Tracer(constant.TraceScopeName, Review Comment: 多注册中心下,一次 RPC 会创建两个逻辑 span。 外层 zone-aware 和内层 failover 都会对同一个 invocation 开 span。本地验证一次实际重试后,两个 span 同名且都标成 failover,外层的 cluster.retry_count 是 0,内层才是 1。建议同一个 invocation 只在最外层创建逻辑 span,内层把重试事件记到同一个 span 上 ########## filter/otel/trace/error.go: ########## @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package trace + +import ( + "context" + "errors" +) + +import ( + "go.opentelemetry.io/otel/attribute" + + "go.opentelemetry.io/otel/codes" + + "go.opentelemetry.io/otel/trace" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/rpcerror" + tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" +) + +// errorType mirrors metrics/rpc's precedence without depending on metrics or +// guessing local failure origins from error text. Protocol statuses take +// precedence over wrapped context errors and confirmed local origins. +func errorType(err error) string { + code := tri.CodeOf(err) + switch code { + case tri.CodeDeadlineExceeded: + return "timeout" + case tri.CodeResourceExhausted: + return "limit" + case tri.CodeUnavailable, tri.CodePermissionDenied: + if code == tri.CodePermissionDenied || rpcerror.Of(err) == rpcerror.Unknown { + return "service_unavailable" + } + case tri.CodeBizError: + return "business_failed" + } + if errors.Is(err, context.DeadlineExceeded) { + return "timeout" + } + if errors.Is(err, context.Canceled) { + return "unknown" + } + switch rpcerror.Of(err) { + case rpcerror.Network: + return "network_failure" + case rpcerror.Codec: + return "codec" + default: + return "unknown" + } +} + +func recordSpanError(span trace.Span, err error, category string) { + span.RecordError(err) Review Comment: 集群失败时可能把注册中心密码导出到 Trace。 多注册中心下,`zone-aware` 强制匹配分区失败时会把完整注册中心 URL 拼进错误。新逻辑 span 会把这段错误原样写入 span status 和 exception.message。我用假凭据确认两个字段都包含密码。建议给集群错误提供可安全导出的摘要,地址只取 host 和 port。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
