From 35c554be736b7ff3c1ac65ba8782a1c7ec12d796 Mon Sep 17 00:00:00 2001 From: Kevin Zhao Date: Thu, 18 May 2023 11:27:55 -0700 Subject: [PATCH] Avoid making a subscope if possible --- common/metrics/tally_metrics_handler.go | 26 ++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/common/metrics/tally_metrics_handler.go b/common/metrics/tally_metrics_handler.go index 9f37c483875..19017cebb22 100644 --- a/common/metrics/tally_metrics_handler.go +++ b/common/metrics/tally_metrics_handler.go @@ -78,28 +78,44 @@ func (tmp *tallyMetricsHandler) WithTags(tags ...Tag) Handler { // Counter obtains a counter for the given name and MetricOptions. func (tmp *tallyMetricsHandler) Counter(counter string) CounterIface { return CounterFunc(func(i int64, t ...Tag) { - tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)).Counter(counter).Inc(i) + scope := tmp.scope + if len(t) > 0 { + scope = tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)) + } + scope.Counter(counter).Inc(i) }) } // Gauge obtains a gauge for the given name and MetricOptions. func (tmp *tallyMetricsHandler) Gauge(gauge string) GaugeIface { return GaugeFunc(func(f float64, t ...Tag) { - tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)).Gauge(gauge).Update(f) + scope := tmp.scope + if len(t) > 0 { + scope = tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)) + } + scope.Gauge(gauge).Update(f) }) } // Timer obtains a timer for the given name and MetricOptions. func (tmp *tallyMetricsHandler) Timer(timer string) TimerIface { - return TimerFunc(func(d time.Duration, tag ...Tag) { - tmp.scope.Tagged(tagsToMap(tag, tmp.excludeTags)).Timer(timer).Record(d) + return TimerFunc(func(d time.Duration, t ...Tag) { + scope := tmp.scope + if len(t) > 0 { + scope = tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)) + } + scope.Timer(timer).Record(d) }) } // Histogram obtains a histogram for the given name and MetricOptions. func (tmp *tallyMetricsHandler) Histogram(histogram string, unit MetricUnit) HistogramIface { return HistogramFunc(func(i int64, t ...Tag) { - tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)).Histogram(histogram, tmp.perUnitBuckets[unit]).RecordValue(float64(i)) + scope := tmp.scope + if len(t) > 0 { + scope = tmp.scope.Tagged(tagsToMap(t, tmp.excludeTags)) + } + scope.Histogram(histogram, tmp.perUnitBuckets[unit]).RecordValue(float64(i)) }) }