-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
7,499 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,57 @@ | ||
// Copyright (c) 2018 Palantir Technologies. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type mContextKey string | ||
|
||
const ( | ||
registryKey = mContextKey("metrics-registry") | ||
tagsKey = mContextKey("metrics-tags") | ||
) | ||
|
||
var DefaultMetricsRegistry = NewRootMetricsRegistry() | ||
|
||
func WithRegistry(ctx context.Context, registry Registry) context.Context { | ||
return context.WithValue(ctx, registryKey, registry) | ||
} | ||
|
||
func FromContext(ctx context.Context) Registry { | ||
if registry, ok := ctx.Value(registryKey).(Registry); ok { | ||
return registry | ||
} | ||
return DefaultMetricsRegistry | ||
} | ||
|
||
// AddTags adds the provided tags to the provided context. If the provided context already has tag storage, the new tags | ||
// are appended to the storage in the existing context (which mutates the content of the context) and returns the same | ||
// context. If the provided context does not store any tags, this call creates and returns a new context that has tag | ||
// storage and stores the provided tags. If the provided context has any tags already set on it, the provided tags are | ||
// appended to them. This function does not perform any de-duplication (that is, if a tag in the provided tags has the | ||
// same key as an existing one, it will still be appended). Note that tags are shared | ||
func AddTags(ctx context.Context, tags ...Tag) context.Context { | ||
if tagsContainer, ok := ctx.Value(tagsKey).(*tagsContainer); ok && tagsContainer != nil { | ||
tagsContainer.Tags = append(tagsContainer.Tags, tags...) | ||
return ctx | ||
} | ||
return context.WithValue(ctx, tagsKey, &tagsContainer{ | ||
Tags: tags, | ||
}) | ||
} | ||
|
||
// TagsFromContext returns the tags stored on the provided context. May be nil if no tags have been set on the context. | ||
func TagsFromContext(ctx context.Context) Tags { | ||
if tagsContainer, ok := ctx.Value(tagsKey).(*tagsContainer); ok { | ||
return tagsContainer.Tags | ||
} | ||
return nil | ||
} | ||
|
||
type tagsContainer struct { | ||
Tags Tags | ||
} |
This file contains 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,29 @@ | ||
// Copyright (c) 2018 Palantir Technologies. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/rcrowley/go-metrics" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFromContext(t *testing.T) { | ||
ctx := context.Background() | ||
reg := &rootRegistry{ | ||
registry: metrics.NewPrefixedRegistry("foo"), | ||
} | ||
|
||
ctx = WithRegistry(ctx, reg) | ||
|
||
assert.Equal(t, FromContext(ctx), reg) | ||
} | ||
|
||
func TestDefaultFromContext(t *testing.T) { | ||
ctx := context.Background() | ||
assert.Equal(t, FromContext(ctx), DefaultMetricsRegistry) | ||
} |
This file contains 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,26 @@ | ||
// Copyright (c) 2018 Palantir Technologies. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// RunEmittingRegistry periodically calls registry.Each with a provided visit function. See the | ||
// documentation for Registry for the arguments of the visit function. RunEmittingRegistry blocks forever | ||
// (or until ctx is cancelled) and should be started in its own goroutine. | ||
func RunEmittingRegistry(ctx context.Context, registry Registry, emitFrequency time.Duration, visitor MetricVisitor) { | ||
t := time.NewTicker(emitFrequency) | ||
defer t.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-t.C: | ||
registry.Each(visitor) | ||
} | ||
} | ||
} |
Oops, something went wrong.