-
Notifications
You must be signed in to change notification settings - Fork 0
.: Add a multi incarnation scope wrapper #7
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package stats | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "sync" | ||
|
|
||
| "github.com/upfluence/stats/internal/hash" | ||
| ) | ||
|
|
||
| var globalIncarnationRegistry = &incarnationRegistry{key: "incarnation", counters: make(map[counterKey]uint)} | ||
|
|
||
| type incarnationRegistry struct { | ||
| countersMu sync.Mutex | ||
| counters map[counterKey]uint | ||
| key string | ||
| } | ||
|
|
||
| func (ir *incarnationRegistry) next(ck counterKey) uint { | ||
| ir.countersMu.Lock() | ||
| defer ir.countersMu.Unlock() | ||
|
|
||
| current, ok := ir.counters[ck] | ||
|
|
||
| next := current + 1 | ||
|
|
||
| if !ok { | ||
| next-- | ||
| } | ||
|
|
||
| ir.counters[ck] = next | ||
|
|
||
| return next | ||
| } | ||
|
|
||
| type counterKey struct { | ||
| keySum uint64 | ||
| tagsSum uint64 | ||
| } | ||
|
|
||
| func newCounterKey() counterKey { | ||
| return counterKey{keySum: hash.New()} | ||
| } | ||
|
|
||
| func (ck counterKey) add(key string, tags map[string]string) counterKey { | ||
| if key != "" { | ||
| if ck.keySum != hash.New() { | ||
| key = "_" + key | ||
| } | ||
|
|
||
| ck.keySum = hash.AddNoScramble(ck.keySum, key) | ||
| } | ||
|
|
||
| for k, v := range tags { | ||
| ck.tagsSum |= hash.Add(hash.Add(hash.New(), k), v) | ||
| } | ||
|
|
||
| return ck | ||
| } | ||
|
|
||
| func (ck counterKey) appendTag(k, v string) counterKey { | ||
| ck.tagsSum |= hash.Add(hash.Add(hash.New(), k), v) | ||
|
|
||
| return ck | ||
| } | ||
|
|
||
| type multiIncarnationScope struct { | ||
| scope Scope | ||
|
|
||
| currentKey counterKey | ||
| registry *incarnationRegistry | ||
| } | ||
|
|
||
| func newMultiIncarnationScope(sc Scope, r *incarnationRegistry) *multiIncarnationScope { | ||
| return &multiIncarnationScope{ | ||
| scope: sc, | ||
| currentKey: newCounterKey().add(sc.namespace(), sc.tags()), | ||
| registry: r, | ||
| } | ||
| } | ||
|
|
||
| func GlobalIncarnationScope(sc Scope) Scope { | ||
| return newMultiIncarnationScope(sc, globalIncarnationRegistry) | ||
| } | ||
|
|
||
| func LocalIncarnationScope(sc Scope, k string) Scope { | ||
| return newMultiIncarnationScope( | ||
| sc, | ||
| &incarnationRegistry{key: k, counters: make(map[counterKey]uint)}, | ||
| ) | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) namespace() string { return mis.scope.namespace() } | ||
| func (mis *multiIncarnationScope) tags() map[string]string { return mis.scope.tags() } | ||
| func (mis *multiIncarnationScope) rootScope() *rootScope { return mis.scope.rootScope() } | ||
|
|
||
| func (mis *multiIncarnationScope) Scope(k string, vs map[string]string) Scope { | ||
| if _, ok := vs[mis.registry.key]; ok { | ||
| panic(fmt.Sprintf("scope can not include the incarnation key %q", mis.registry.key)) | ||
| } | ||
|
|
||
| return &multiIncarnationScope{ | ||
| scope: mis.scope.Scope(k, vs), | ||
| currentKey: mis.currentKey.add(k, vs), | ||
| registry: mis.registry, | ||
| } | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) RootScope() Scope { | ||
| return &multiIncarnationScope{ | ||
| scope: mis.scope.RootScope(), | ||
| registry: mis.registry, | ||
| } | ||
| } | ||
|
|
||
| type abstractVector[T any] interface { | ||
| WithLabels(...string) T | ||
| } | ||
|
|
||
| type multiIncarnationVector[T any] struct { | ||
| cv abstractVector[T] | ||
| ls []string | ||
|
|
||
| currentKey counterKey | ||
| registry *incarnationRegistry | ||
| } | ||
|
|
||
| func (micv *multiIncarnationVector[T]) WithLabels(vs ...string) T { | ||
| if len(vs) != len(micv.ls) { | ||
| panic("wrong number of label values") | ||
| } | ||
|
|
||
| ck := micv.currentKey | ||
|
|
||
| for i, l := range micv.ls { | ||
| ck = ck.appendTag(l, vs[i]) | ||
| } | ||
|
|
||
| return micv.cv.WithLabels( | ||
| append( | ||
| vs, | ||
| strconv.Itoa(int(micv.registry.next(ck))), | ||
| )..., | ||
| ) | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) Counter(k string) Counter { | ||
| return mis.CounterVector(k, nil).WithLabels() | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) CounterVector(k string, ls []string) CounterVector { | ||
| return &multiIncarnationVector[Counter]{ | ||
| cv: mis.scope.CounterVector(k, append(ls, mis.registry.key)), | ||
| ls: ls, | ||
| currentKey: mis.currentKey.add(k, nil), | ||
| registry: mis.registry, | ||
| } | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) Gauge(k string) Gauge { | ||
| return mis.GaugeVector(k, nil).WithLabels() | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) GaugeVector(k string, ls []string) GaugeVector { | ||
| return &multiIncarnationVector[Gauge]{ | ||
| cv: mis.scope.GaugeVector(k, append(ls, mis.registry.key)), | ||
| ls: ls, | ||
| currentKey: mis.currentKey.add(k, nil), | ||
| registry: mis.registry, | ||
| } | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) Histogram(k string, opts ...HistogramOption) Histogram { | ||
| return mis.HistogramVector(k, nil, opts...).WithLabels() | ||
| } | ||
|
|
||
| func (mis *multiIncarnationScope) HistogramVector(k string, ls []string, opts ...HistogramOption) HistogramVector { | ||
| return &multiIncarnationVector[Histogram]{ | ||
| cv: mis.scope.HistogramVector(k, append(ls, mis.registry.key), opts...), | ||
| ls: ls, | ||
| currentKey: mis.currentKey.add(k, nil), | ||
| registry: mis.registry, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package stats | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestMultiIncarnationScope(t *testing.T) { | ||
| c := NewStaticCollector() | ||
|
|
||
| sc := LocalIncarnationScope(RootScope(c), "incarnation") | ||
|
|
||
| g1 := sc.Scope("foo", map[string]string{"bar": "buz"}).Gauge("bar") | ||
|
|
||
| g1.Update(1) | ||
|
|
||
| sc.GaugeVector("foo_bar", []string{"bar"}).WithLabels("buz").Update(2) | ||
|
|
||
| g1.Update(3) | ||
|
|
||
| sc.Scope("foo", map[string]string{"bar": "buz"}).Gauge("bar").Update(4) | ||
|
|
||
| sc.Counter("foo_bar_1").Inc() | ||
|
|
||
| assert.Equal( | ||
| t, | ||
| []Int64Snapshot{ | ||
| {Name: "foo_bar", Labels: map[string]string{"bar": "buz", "incarnation": "0"}, Value: 3}, | ||
| {Name: "foo_bar", Labels: map[string]string{"bar": "buz", "incarnation": "1"}, Value: 2}, | ||
| {Name: "foo_bar", Labels: map[string]string{"bar": "buz", "incarnation": "2"}, Value: 4}, | ||
| }, | ||
| c.Get().Gauges, | ||
| ) | ||
|
|
||
| assert.Equal( | ||
| t, | ||
| []Int64Snapshot{ | ||
| {Name: "foo_bar_1", Labels: map[string]string{"incarnation": "0"}, Value: 1}, | ||
| }, | ||
| c.Get().Counters, | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [golangci] reported by reviewdog 🐶
G115: integer overflow conversion uint -> int (gosec)