Skip to content

Commit

Permalink
update internal metrics impl
Browse files Browse the repository at this point in the history
  • Loading branch information
brawndou committed Dec 21, 2023
1 parent 8686c7d commit 39ebbd2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
1 change: 1 addition & 0 deletions m3/reporter_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func main() {
scope, closer := tally.NewRootScope(tally.ScopeOptions{
CachedReporter: r,
MetricsOption: tally.OmitInternalMetrics,
}, 5 * time.Second)
defer closer.Close()
Expand Down
26 changes: 13 additions & 13 deletions scope_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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()))
}
}
46 changes: 23 additions & 23 deletions scope_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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)
Expand All @@ -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,
)
}

Expand Down

0 comments on commit 39ebbd2

Please sign in to comment.