dodjdnh commented on code in PR #3843:
URL: https://github.com/apache/dubbo-go/pull/3843#discussion_r4661561756


##########
metrics/prometheus/registry.go:
##########
@@ -175,11 +200,17 @@ func (p *promMetricRegistry) Export() {
        }
 }
 
+func (p *promMetricRegistry) httpHandler() http.Handler {

Review Comment:
   **[P2] `histogram.enabled=false` 且 `histogram.exemplars=true` 时,`/metrics` 
仍开启了 `EnableOpenMetrics`**
   
   `HistogramWithOptions` 与 `global.MetricsConfig.RPCExemplarsEnabled()` 都同时校验了 
`HistogramEnabledKey` 和 `HistogramExemplarsKey`,但这里的 `p.exemplarsEnabled()` 只读了 
`HistogramExemplarsKey`。
   
   如果运维临时把 `metrics.histogram.enabled` 设为 `false` 回滚直方图、但保留了 `exemplars: 
true`,Prometheus 2.5+ 抓取器仍会协商 `application/openmetrics-text`(导致整数 `le` 标签补 
`.0`、末尾输出 `# EOF` 等格式变化)。建议让 `exemplarsEnabled()` 统一同时校验 `HistogramEnabledKey` 
和 `HistogramExemplarsKey`,保持契约一致。



##########
common/config/utils.go:
##########
@@ -75,6 +75,34 @@ func Verify(s any) error {
        return nil
 }
 
+// AppendObservabilityFilters adds the automatic filters in execution order.
+// When exemplars are enabled, honor explicitly positioned metrics and tracing 
filters.
+func AppendObservabilityFilters(filters, explicit, traceFilter string, metrics 
*global.MetricsConfig, tracing *global.OtelTraceConfig) string {

Review Comment:
   **[P1] 仅显式配置单个可观测 Filter 时,自动注入的另一个 Filter 会排到错误一侧,导致 Exemplar 静默失效**
   
   当 `metrics.RPCExemplarsEnabled()` 为 `true`,且用户只显式写了 `metrics` 或 `otel*Trace` 
中的一个(另一个走自动注入)时,两端会出现对称的顺序反转:
   
   - **Provider 侧(`server/action.go`)**:调用 `AppendObservabilityFilters(filters, 
filters, constant.OTELServerTraceKey, metrics, tracing)` 时把同一个 `filters` 同时传给了 
`filters` 和 `explicit`。当 `svcConf.Filter`(或 `extensionFilterNames`)显式包含 
`metrics`(例如 `custom,metrics`)时,`metricsEnabled` 变为 `false`,自动注入的 
`otelServerTrace` 会被追加到末尾 → `custom,metrics,otelServerTrace`,导致 `metricsFilter` 
在 `otelServerFilter` 之前执行,`rpcexemplar.ForInvocation` 恒返回 `nil`。
   - **Consumer 侧(`client/action.go`)**:当 `ref.Filter` 显式配置了 
`otelClientTrace`(且未写 `default`)时,自动注入的 `metrics` 会被拼到 
`defaultReferenceFilter`(`cshutdown,metrics`),随后 `MergeValue` 将默认段前置展开 → 
`cshutdown,metrics,otelClientTrace`,同样把 `metrics` 放到了 `otelClientTrace` 之前。
   
   **建议**:在 `RPCExemplarsEnabled()` 为 `true` 且仅其中一个 Filter 自动注入时,Provider 
侧将自动注入的 `otelServerTrace` 插入到已显式存在的 `metrics` 之前,Consumer 侧将自动注入的 `metrics` 
放到显式 `otelClientTrace` 之后;同时建议在 `server/action_test.go`、`client/action_test.go` 
和 `common/config/utils_test.go` 补齐这类单侧显式配置的装配单测。



##########
internal/rpcexemplar/span.go:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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 rpcexemplar associates local RPC spans with invocations without
+// registering tracing or metrics extensions.
+package rpcexemplar
+
+import (
+       "context"
+       "reflect"
+       "sync/atomic"
+)
+
+import (
+       "go.opentelemetry.io/otel/trace"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/protocol/base"
+)
+
+var enabled atomic.Bool
+
+// Enable activates span capture for filters built after RPC metrics 
initialize.
+func Enable() { enabled.Store(true) }
+
+// Enabled is read when constructing filters, outside the invocation path.
+func Enabled() bool { return enabled.Load() }
+
+type exemplarSpanKey struct{}
+
+// IDs is the trace identity retained by an RPC latency observation.
+type IDs struct {
+       TraceID trace.TraceID
+       SpanID  trace.SpanID
+}
+
+type exemplarSpan struct {

Review Comment:
   **[P2] `exemplarSpan` 在透传至业务层的 `ctx` 中强引用 `base.Invocation`,可能阻碍参数 GC**
   
   `exemplarSpan` 直接在 `ctx` 中持有了 `invocation base.Invocation` 强引用(泛化重写时 
`Rebind` 还会再包一层 `context.WithValue`,父子节点同时持有重写前后两份参数对象)。如果业务 Handler 用 
`context.WithoutCancel(ctx)` 起后台异步任务保留 Trace Context,或在长流场景中持有 `ctx`,整份 
`*invocation.RPCInvocation`(参数、attachments、reply)在后台任务结束前都无法被 GC。
   
   对照同仓 `common/rpcerror/observation.go`(认领后主动将 `o.invocation = nil` 释放引用),建议在 
`otel*Filter.Invoke` 退出时通过 `defer` 清零内部 invocation 引用,或避免在 `ctx` value 中直接强持有 
`base.Invocation`。



-- 
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]

Reply via email to