AlexStocks commented on code in PR #3805:
URL: https://github.com/apache/dubbo-go/pull/3805#discussion_r4548966425


##########
metrics/rpc/collector.go:
##########
@@ -49,28 +55,42 @@ func init() {
 
 // rpcCollector is a collector which will collect the rpc metrics
 type rpcCollector struct {
-       registry  metrics.MetricRegistry
-       metricSet *metricSet // metricSet is a struct which contains all 
metrics about rpc
+       registry      metrics.MetricRegistry
+       metricSet     *metricSet // metricSet is a struct which contains all 
metrics about rpc
+       series        sync.Map   // seriesKey -> *rpcSeries; owned by this 
registry
+       bindMu        sync.Mutex
+       fallbackMu    sync.Mutex // preserve serialized calls for legacy 
registries
+       reportFailure sync.Once
 }
 
-// start will subscribe the rpc.metricsEvent from channel rpcMetricsChan, and 
handle the event from the channel
-func (c *rpcCollector) start() {
-       metrics.Subscribe(constant.MetricsRpc, rpcMetricsChan)
-       for event := range rpcMetricsChan {
-               if rpcEvent, ok := event.(*metricsEvent); ok {
-                       switch rpcEvent.name {
-                       case BeforeInvoke:
-                               c.beforeInvokeHandler(rpcEvent)
-                       case AfterInvoke:
-                               c.afterInvokeHandler(rpcEvent)
-                       case ObservationExpired:
-                               c.observationExpiredHandler(rpcEvent)
-                       default:
-                       }
-               } else {
-                       logger.Error("[Metrics][RPC] bad metrics event found in 
RPC collector")
+// record is called by Publish, before best-effort observer delivery. It never
+// lets a broken custom registry replace a business result or suppress 
callbacks.
+func (c *rpcCollector) record(event metrics.MetricsEvent) {
+       defer func() {
+               if recovered := recover(); recovered != nil {
+                       c.reportFailure.Do(func() { 
logger.Errorf("[Metrics][RPC] synchronous recording failed: %v", recovered) })
                }
+       }()
+       e, ok := event.(*metricsEvent)
+       if !ok || e.collector != c || (e.role != constant.SideConsumer && 
e.role != constant.SideProvider) {
+               return
+       }
+       if _, ok := c.registry.(metrics.GaugeFuncRegistry); !ok {
+               // Retain record-time window gauges and serial registry access 
for custom
+               // registries that do not implement the optional 
collection-time extension.
+               c.fallbackMu.Lock()
+               defer c.fallbackMu.Unlock()
+               switch e.name {
+               case BeforeInvoke:
+                       c.beforeInvokeHandler(e)
+               case AfterInvoke:
+                       c.afterInvokeHandler(e)
+               case ObservationExpired:
+                       c.observationExpiredHandler(e)
+               }
+               return
        }
+       c.getSeries(e).record(e)

Review Comment:
   [P2] 新的同步记账入口没有承接 RT 直方图记录,与 #3791 合并后 Prometheus 部署的直方图会停止采样
   
   **触发条件**:#3791 在 `afterInvokeHandler` 末尾新增 `c.reportRTHistogram(role, 
labels, event.costTime.Seconds())`(该 PR head `221c96e2` 的 
`metrics/rpc/collector.go:127`),并在 `newRPCCollector` 注入 `histogramRegistry`。本 
PR 删除了 `rpcMetricsChan` 与 `go rc.start()`,`afterInvokeHandler` 只在「注册表未实现 
`GaugeFuncRegistry`」的兼容分支内被调用(`metrics/rpc/collector.go:81-91`,`metrics/api.go:343`
 是同一判据的另一处)。`promMetricRegistry` 实现了 
`GaugeFunc`(`metrics/prometheus/registry.go:96`),因此默认 Prometheus 部署的每个完成事件都走本行的 
`c.getSeries(e).record(e)`,`reportRTHistogram` 不再可达。
   
   **实际影响**:两个 PR 都合入后,`dubbo_provider_rt_milliseconds_histogram_seconds` / 
`dubbo_consumer_rt_milliseconds_histogram_seconds` 只会暴露空序列(`_bucket` 
各档位、`_sum`、`_count` 恒为 0),开启 `histogram.enabled=true` 的部署拿不到任何延迟分布数据;既有 RT、计数与 
`processing` 指标不受影响。若按相反顺序合并(本 PR 先入、#3791 后 rebase),结论相同,因为新入口是本 PR 引入的。
   
   **证据**:本 PR head `803bd2cd` 的 `record()` 
里,`BeforeInvoke`/`ObservationExpired`/`AfterInvoke` 三个分支均只更新 
`qps`、`processing`、`counter`、`rt`、`quantiles`,没有任何 
`HistogramWithOptions(...).Observe(...)` 调用;`histogram` 相关字段也未出现在本 PR 的 
`rpcCollector` 结构体中。
   
   **修复方向**:在 `rpcSeries.record` 的 `AfterInvoke` 分支中同样观察直方图(等价于把 
`reportRTHistogram` 提到 `record()` 的分支判断之上,用 
`e.costTime.Seconds()`),并补一条断言:完成事件记录后 `_bucket` 的累计值随观测数增长,而非恒为 0。合并顺序上建议先合 
#3791,本 PR rebase 时一并迁移该调用点。



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