Skip to content

Commit

Permalink
Add ability to use a non-default registry (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
robskillington authored May 15, 2019
1 parent f2b9562 commit 1018d35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
31 changes: 22 additions & 9 deletions prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"net/http"
"os"
"strings"

prom "github.com/m3db/prometheus_client_golang/prometheus"
)

// Configuration is a configuration for a Prometheus reporter.
Expand Down Expand Up @@ -68,37 +70,48 @@ type SummaryObjective struct {
AllowedError float64 `yaml:"allowedError"`
}

// ConfigurationOptions allows some error callbacks to be registered.
// ConfigurationOptions allows some programatic options, such as using a
// specific registry and what error callback to register.
type ConfigurationOptions struct {
// Registry if not nil will specify the specific registry to use
// for registering metrics.
Registry *prom.Registry
// OnError allows for customization of what to do when a metric
// registration error fails, the default is to panic.
OnError func(e error)
}

// NewReporter creates a new M3 reporter from this configuration.
func (c Configuration) NewReporter(
configOpts ConfigurationOptions,
) (Reporter, error) {
if configOpts.OnError == nil {
var opts Options

if configOpts.Registry != nil {
opts.Registerer = configOpts.Registry
}

if configOpts.OnError != nil {
opts.OnRegisterError = configOpts.OnError
} else {
switch c.OnError {
case "stderr":
configOpts.OnError = func(err error) {
opts.OnRegisterError = func(err error) {
fmt.Fprintf(os.Stderr, "tally prometheus reporter error: %v\n", err)
}
case "log":
configOpts.OnError = func(err error) {
opts.OnRegisterError = func(err error) {
log.Printf("tally prometheus reporter error: %v\n", err)
}
case "none":
configOpts.OnError = func(err error) {}
opts.OnRegisterError = func(err error) {}
default:
configOpts.OnError = func(err error) {
opts.OnRegisterError = func(err error) {
panic(err)
}
}
}

var opts Options
opts.OnRegisterError = configOpts.OnError

switch c.TimerType {
case "summary":
opts.DefaultTimerType = SummaryTimerType
Expand Down
6 changes: 6 additions & 0 deletions prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ type Options struct {
func NewReporter(opts Options) Reporter {
if opts.Registerer == nil {
opts.Registerer = prom.DefaultRegisterer
} else {
// A specific registerer was set, check if it's a registry and if
// no gatherer was set, then use that as the gatherer
if reg, ok := opts.Registerer.(*prom.Registry); ok && opts.Gatherer == nil {
opts.Gatherer = reg
}
}
if opts.Gatherer == nil {
opts.Gatherer = prom.DefaultGatherer
Expand Down

0 comments on commit 1018d35

Please sign in to comment.