From 13544d50c2729642e91b2032ff5c454dac94a17b Mon Sep 17 00:00:00 2001 From: Shankar Nair Date: Thu, 27 Jul 2023 00:41:19 -0700 Subject: [PATCH 1/2] Enforce minimum reporting interval --- scope.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index 1418348d..e311f6e8 100644 --- a/scope.go +++ b/scope.go @@ -39,7 +39,8 @@ const ( // OmitInternalMetrics turns off internal metrics submission. OmitInternalMetrics - _defaultInitialSliceSize = 16 + _defaultInitialSliceSize = 16 + _minimumAllowedReportingInterval = 2 * time.Second ) var ( @@ -120,6 +121,10 @@ type ScopeOptions struct { // a reporting interval. // Must provide either a StatsReporter or a CachedStatsReporter. func NewRootScope(opts ScopeOptions, interval time.Duration) (Scope, io.Closer) { + if interval > 0 && interval < _minimumAllowedReportingInterval { + interval = _minimumAllowedReportingInterval + } + s := newRootScope(opts, interval) return s, s } From ffb9e5acc9dede599ec16fe20d5e5ffe55c1833a Mon Sep 17 00:00:00 2001 From: Shankar Nair Date: Thu, 27 Jul 2023 11:10:10 -0700 Subject: [PATCH 2/2] Add new RootScope constructor with default interval --- scope.go | 14 ++++++++------ scope_test.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/scope.go b/scope.go index e311f6e8..0b301040 100644 --- a/scope.go +++ b/scope.go @@ -39,8 +39,8 @@ const ( // OmitInternalMetrics turns off internal metrics submission. OmitInternalMetrics - _defaultInitialSliceSize = 16 - _minimumAllowedReportingInterval = 2 * time.Second + _defaultInitialSliceSize = 16 + _defaultReportingInterval = 2 * time.Second ) var ( @@ -121,14 +121,16 @@ type ScopeOptions struct { // a reporting interval. // Must provide either a StatsReporter or a CachedStatsReporter. func NewRootScope(opts ScopeOptions, interval time.Duration) (Scope, io.Closer) { - if interval > 0 && interval < _minimumAllowedReportingInterval { - interval = _minimumAllowedReportingInterval - } - s := newRootScope(opts, interval) return s, s } +// NewRootScopeWithDefaultInterval invokes NewRootScope with the default +// reporting interval of 2s. +func NewRootScopeWithDefaultInterval(opts ScopeOptions) (Scope, io.Closer) { + return NewRootScope(opts, _defaultReportingInterval) +} + // NewTestScope creates a new Scope without a stats reporter with the // given prefix and adds the ability to take snapshots of metrics emitted // to it. diff --git a/scope_test.go b/scope_test.go index 70d91aaa..a439576d 100644 --- a/scope_test.go +++ b/scope_test.go @@ -395,6 +395,26 @@ func TestWriteReportLoop(t *testing.T) { r.WaitAll() } +func TestWriteReportLoopDefaultInterval(t *testing.T) { + r := newTestStatsReporter() + s, closer := NewRootScopeWithDefaultInterval( + ScopeOptions{Reporter: r, MetricsOption: OmitInternalMetrics}, + ) + defer closer.Close() + + r.cg.Add(1) + s.Counter("bar").Inc(1) + r.gg.Add(1) + s.Gauge("zed").Update(1) + r.tg.Add(1) + s.Timer("ticky").Record(time.Millisecond * 175) + r.hg.Add(1) + s.Histogram("baz", MustMakeLinearValueBuckets(0, 10, 10)). + RecordValue(42.42) + + r.WaitAll() +} + func TestCachedReportLoop(t *testing.T) { r := newTestStatsReporter() s, closer := NewRootScope(ScopeOptions{CachedReporter: r, MetricsOption: OmitInternalMetrics}, 10)