Skip to content

Commit

Permalink
Set default reporting interval (#226)
Browse files Browse the repository at this point in the history
* Enforce minimum reporting interval

* Add new RootScope constructor with default interval
  • Loading branch information
shaan420 authored Jul 28, 2023
1 parent ecb4ac9 commit 67241bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const (
// OmitInternalMetrics turns off internal metrics submission.
OmitInternalMetrics

_defaultInitialSliceSize = 16
_defaultInitialSliceSize = 16
_defaultReportingInterval = 2 * time.Second
)

var (
Expand Down Expand Up @@ -124,6 +125,12 @@ func NewRootScope(opts ScopeOptions, interval time.Duration) (Scope, io.Closer)
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.
Expand Down
20 changes: 20 additions & 0 deletions scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 67241bf

Please sign in to comment.