From 1be28d76b73b092bf7d0e0b449db6ddacb1a239c Mon Sep 17 00:00:00 2001 From: linhai zhu Date: Thu, 21 Dec 2023 13:03:10 -0800 Subject: [PATCH] update internal metrics impl --- m3/reporter_integration_test.go | 1 + scope_registry.go | 26 +++++++++---------- scope_registry_test.go | 46 ++++++++++++++++----------------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/m3/reporter_integration_test.go b/m3/reporter_integration_test.go index 4ccfe586..a0e5d8c1 100644 --- a/m3/reporter_integration_test.go +++ b/m3/reporter_integration_test.go @@ -55,6 +55,7 @@ func main() { scope, closer := tally.NewRootScope(tally.ScopeOptions{ CachedReporter: r, + MetricsOption: tally.OmitInternalMetrics, }, 5 * time.Second) defer closer.Close() diff --git a/scope_registry.go b/scope_registry.go index c52514db..77f56e58 100644 --- a/scope_registry.go +++ b/scope_registry.go @@ -34,9 +34,9 @@ var ( // Metrics related. internalTags = map[string]string{"version": Version} - counterCardinalityName = "tally_internal_counter_cardinality" - gaugeCardinalityName = "tally_internal_gauge_cardinality" - histogramCardinalityName = "tally_internal_histogram_cardinality" + counterCardinalityName = "tally.internal.counter_cardinality" + gaugeCardinalityName = "tally.internal.gauge_cardinality" + histogramCardinalityName = "tally.internal.histogram_cardinality" ) type scopeRegistry struct { @@ -237,7 +237,7 @@ func (r *scopeRegistry) removeWithRLock(subscopeBucket *scopeBucket, key string) // Records internal Metrics' cardinalities. func (r *scopeRegistry) reportInternalMetrics() { - if r.internalMetricsOption != SendInternalMetrics { + if r.internalMetricsOption == OmitInternalMetrics { return } @@ -263,17 +263,17 @@ func (r *scopeRegistry) reportInternalMetrics() { histograms.Add(rootHistograms.Load()) if r.root.reporter != nil { - r.root.reporter.ReportCounter(r.sanitizedCounterCardinalityName, internalTags, counters.Load()) - r.root.reporter.ReportCounter(r.sanitizedGaugeCardinalityName, internalTags, gauges.Load()) - r.root.reporter.ReportCounter(r.sanitizedHistogramCardinalityName, internalTags, histograms.Load()) + r.root.reporter.ReportGauge(r.sanitizedCounterCardinalityName, internalTags, float64(counters.Load())) + r.root.reporter.ReportGauge(r.sanitizedGaugeCardinalityName, internalTags, float64(gauges.Load())) + r.root.reporter.ReportGauge(r.sanitizedHistogramCardinalityName, internalTags, float64(histograms.Load())) } if r.root.cachedReporter != nil { - numCounters := r.root.cachedReporter.AllocateCounter(r.sanitizedCounterCardinalityName, internalTags) - numGauges := r.root.cachedReporter.AllocateCounter(r.sanitizedGaugeCardinalityName, internalTags) - numHistograms := r.root.cachedReporter.AllocateCounter(r.sanitizedHistogramCardinalityName, internalTags) - numCounters.ReportCount(counters.Load()) - numGauges.ReportCount(gauges.Load()) - numHistograms.ReportCount(histograms.Load()) + numCounters := r.root.cachedReporter.AllocateGauge(r.sanitizedCounterCardinalityName, internalTags) + numGauges := r.root.cachedReporter.AllocateGauge(r.sanitizedGaugeCardinalityName, internalTags) + numHistograms := r.root.cachedReporter.AllocateGauge(r.sanitizedHistogramCardinalityName, internalTags) + numCounters.ReportGauge(float64(counters.Load())) + numGauges.ReportGauge(float64(gauges.Load())) + numHistograms.ReportGauge(float64(histograms.Load())) } } diff --git a/scope_registry_test.go b/scope_registry_test.go index 63bea579..9e31b3ec 100644 --- a/scope_registry_test.go +++ b/scope_registry_test.go @@ -62,12 +62,12 @@ func TestNewTestStatsReporterOneScope(t *testing.T) { numFakeGauges := 5 numFakeHistograms := 11 - r.cg.Add(numFakeCounters + numInternalMetrics) + r.cg.Add(numFakeCounters) for c := 1; c <= numFakeCounters; c++ { s.Counter(fmt.Sprintf("counter-%d", c)).Inc(int64(c)) } - r.gg.Add(numFakeGauges) + r.gg.Add(numFakeGauges + numInternalMetrics) for g := 1; g <= numFakeGauges; g++ { s.Gauge(fmt.Sprintf("gauge_%d", g)).Update(float64(g)) } @@ -80,35 +80,35 @@ func TestNewTestStatsReporterOneScope(t *testing.T) { closer.Close() r.WaitAll() - assert.NotNil(t, r.counters[counterCardinalityName], "counter cardinality should not be nil") + assert.NotNil(t, r.gauges[counterCardinalityName], "counter cardinality should not be nil") assert.Equal( - t, int64(numFakeCounters), r.counters[counterCardinalityName].val, "expected %d counters, got %d counters", - numFakeCounters, r.counters[counterCardinalityName].val, + t, numFakeCounters, int(r.gauges[counterCardinalityName].val), "expected %d counters, got %d counters", + numFakeCounters, r.gauges[counterCardinalityName].val, ) - assert.NotNil(t, r.counters[gaugeCardinalityName], "gauge cardinality should not be nil") + assert.NotNil(t, r.gauges[gaugeCardinalityName], "gauge cardinality should not be nil") assert.Equal( - t, int64(numFakeGauges), r.counters[gaugeCardinalityName].val, "expected %d gauges, got %d gauges", - numFakeGauges, r.counters[gaugeCardinalityName].val, + t, numFakeGauges, int(r.gauges[gaugeCardinalityName].val), "expected %d gauges, got %d gauges", + numFakeGauges, r.gauges[gaugeCardinalityName].val, ) - assert.NotNil(t, r.counters[histogramCardinalityName], "histogram cardinality should not be nil") + assert.NotNil(t, r.gauges[histogramCardinalityName], "histogram cardinality should not be nil") assert.Equal( - t, int64(numFakeHistograms), r.counters[histogramCardinalityName].val, - "expected %d histograms, got %d histograms", numFakeHistograms, r.counters[histogramCardinalityName].val, + t, numFakeHistograms, int(r.gauges[histogramCardinalityName].val), + "expected %d histograms, got %d histograms", numFakeHistograms, r.gauges[histogramCardinalityName].val, ) } func TestNewTestStatsReporterManyScopes(t *testing.T) { r := newTestStatsReporter() root, closer := NewRootScope(ScopeOptions{Reporter: r, MetricsOption: SendInternalMetrics}, 0) - wantCounters, wantGauges, wantHistograms := int64(3), int64(2), int64(1) + wantCounters, wantGauges, wantHistograms := 3, 2, 1 s := root.(*scope) - r.cg.Add(2 + numInternalMetrics) + r.cg.Add(2) s.Counter("counter-foo").Inc(1) s.Counter("counter-bar").Inc(2) - r.gg.Add(1) + r.gg.Add(1 + numInternalMetrics) s.Gauge("gauge-foo").Update(3) r.hg.Add(1) s.Histogram("histogram-foo", MustMakeLinearValueBuckets(0, 1, 10)).RecordValue(4) @@ -122,22 +122,22 @@ func TestNewTestStatsReporterManyScopes(t *testing.T) { closer.Close() r.WaitAll() - assert.NotNil(t, r.counters[counterCardinalityName], "counter cardinality should not be nil") + assert.NotNil(t, r.gauges[counterCardinalityName], "counter cardinality should not be nil") assert.Equal( - t, wantCounters, r.counters[counterCardinalityName].val, "expected %d counters, got %d counters", wantCounters, - r.counters[counterCardinalityName].val, + t, wantCounters, int(r.gauges[counterCardinalityName].val), "expected %d counters, got %d counters", wantCounters, + r.gauges[counterCardinalityName].val, ) - assert.NotNil(t, r.counters[gaugeCardinalityName], "gauge cardinality should not be nil") + assert.NotNil(t, r.gauges[gaugeCardinalityName], "gauge cardinality should not be nil") assert.Equal( - t, wantGauges, r.counters[gaugeCardinalityName].val, "expected %d counters, got %d gauges", wantGauges, - r.counters[gaugeCardinalityName].val, + t, wantGauges, int(r.gauges[gaugeCardinalityName].val), "expected %d counters, got %d gauges", wantGauges, + r.gauges[gaugeCardinalityName].val, ) - assert.NotNil(t, r.counters[histogramCardinalityName], "histogram cardinality should not be nil") + assert.NotNil(t, r.gauges[histogramCardinalityName], "histogram cardinality should not be nil") assert.Equal( - t, wantHistograms, r.counters[histogramCardinalityName].val, "expected %d counters, got %d histograms", - wantHistograms, r.counters[histogramCardinalityName].val, + t, wantHistograms, int(r.gauges[histogramCardinalityName].val), "expected %d counters, got %d histograms", + wantHistograms, r.gauges[histogramCardinalityName].val, ) }