Skip to content

Commit

Permalink
Add metrics registry package (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeavital authored and nmiyake committed Aug 16, 2018
1 parent 10b91c0 commit e950b14
Show file tree
Hide file tree
Showing 61 changed files with 7,499 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions metrics/context.go
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
}
29 changes: 29 additions & 0 deletions metrics/context_test.go
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)
}
26 changes: 26 additions & 0 deletions metrics/emit.go
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)
}
}
}
Loading

0 comments on commit e950b14

Please sign in to comment.