Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestScope: don't prune from registry when closed #222

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type scope struct {
done chan struct{}
wg sync.WaitGroup
root bool
testScope bool
}

// ScopeOptions is a set of options to construct a scope.
Expand All @@ -114,6 +115,7 @@ type ScopeOptions struct {
SanitizeOptions *SanitizeOptions
MetricsOption InternalMetricOption

testScope bool
registryShardCount uint
}

Expand All @@ -132,7 +134,11 @@ func NewTestScope(
prefix string,
tags map[string]string,
) TestScope {
return newRootScope(ScopeOptions{Prefix: prefix, Tags: tags}, 0)
return newRootScope(ScopeOptions{
Prefix: prefix,
Tags: tags,
testScope: true,
}, 0)
}

func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
Expand Down Expand Up @@ -177,6 +183,7 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
separator: sanitizer.Name(opts.Separator),
timers: make(map[string]*timer),
root: true,
testScope: opts.testScope,
}

// NB(r): Take a copy of the tags on creation
Expand Down
9 changes: 5 additions & 4 deletions scope_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ func (r *scopeRegistry) Subscope(parent *scope, prefix string, tags map[string]s

s, ok := r.lockedLookup(subscopeBucket, unsanitizedKey)
if ok {
// If this subscope isn't closed, return it. Otherwise, report it
// immediately and delete it so that a new (functional) scope can be
// returned instead.
if !s.closed.Load() {
// If this subscope isn't closed or is a test scope, return it.
// Otherwise, report it immediately and delete it so that a new
// (functional) scope can be returned instead.
if !s.closed.Load() || s.testScope {
subscopeBucket.mu.RUnlock()
return s
}
Expand Down Expand Up @@ -229,6 +229,7 @@ func (r *scopeRegistry) Subscope(parent *scope, prefix string, tags map[string]s
timers: make(map[string]*timer),
bucketCache: parent.bucketCache,
done: make(chan struct{}),
testScope: parent.testScope,
}
subscopeBucket.s[sanitizedKey] = subscope
if _, ok := r.lockedLookup(subscopeBucket, unsanitizedKey); !ok {
Expand Down
29 changes: 29 additions & 0 deletions scope_registry_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ import (
"github.com/uber-go/tally/tallymock"
)

func TestTestScopesNotPruned(t *testing.T) {
var (
root = tally.NewTestScope("", nil)
subscope = root.SubScope("foo")
counter = subscope.Counter("bar")
)

counter.Inc(123)

closer, ok := subscope.(io.Closer)
require.True(t, ok)
require.NoError(t, closer.Close())

subscope = root.SubScope("foo")
counter = subscope.Counter("bar")
counter.Inc(123)

snapshot := root.Snapshot()
require.Len(t, snapshot.Counters(), 1)
require.Len(t, snapshot.Gauges(), 0)
require.Len(t, snapshot.Timers(), 0)
require.Len(t, snapshot.Histograms(), 0)

val, ok := snapshot.Counters()["foo.bar+"]
require.True(t, ok)
require.Equal(t, "foo.bar", val.Name())
require.EqualValues(t, 246, val.Value())
}

func TestNoDefunctSubscopes(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down