-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #147: Support multiple metrics libraries
- Loading branch information
1 parent
bbc2ce8
commit 042f725
Showing
9 changed files
with
254 additions
and
78 deletions.
There are no files selected for viewing
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,91 @@ | ||
// Package gometrics provides an implementation of the | ||
// metricslib.Provider interface using the github.com/rcrowley/go-metrics | ||
// library. | ||
package gometrics | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net" | ||
"os" | ||
"sort" | ||
"time" | ||
|
||
"github.com/eBay/fabio/metrics/metricslib" | ||
|
||
graphite "github.com/cyberdelia/go-metrics-graphite" | ||
statsd "github.com/pubnub/go-metrics-statsd" | ||
gm "github.com/rcrowley/go-metrics" | ||
) | ||
|
||
// StdoutProvider returns a provider that reports to stdout. | ||
func StdoutProvider(interval time.Duration) (metricslib.Provider, error) { | ||
registry := gm.NewRegistry() | ||
logger := log.New(os.Stderr, "localhost: ", log.Lmicroseconds) | ||
go gm.Log(gm.DefaultRegistry, interval, logger) | ||
go gm.Log(registry, interval, logger) | ||
return &gmProvider{registry}, nil | ||
} | ||
|
||
// GraphiteProvider returns a provider that reports to a Graphite server. | ||
func GraphiteProvider(prefix, addr string, interval time.Duration) (metricslib.Provider, error) { | ||
if addr == "" { | ||
return nil, errors.New("metrics: graphite addr missing") | ||
} | ||
|
||
a, err := net.ResolveTCPAddr("tcp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("metrics: cannot connect to Graphite: %s", err) | ||
} | ||
|
||
registry := gm.NewRegistry() | ||
go graphite.Graphite(gm.DefaultRegistry, interval, prefix, a) | ||
go graphite.Graphite(registry, interval, prefix, a) | ||
return &gmProvider{registry}, nil | ||
} | ||
|
||
// StatsDProvider returns a provider that reports to a StatsD server. | ||
func StatsDProvider(prefix, addr string, interval time.Duration) (metricslib.Provider, error) { | ||
if addr == "" { | ||
return nil, errors.New("metrics: statsd addr missing") | ||
} | ||
|
||
a, err := net.ResolveUDPAddr("udp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("metrics: cannot connect to StatsD: %s", err) | ||
} | ||
|
||
registry := gm.NewRegistry() | ||
go statsd.StatsD(gm.DefaultRegistry, interval, prefix, a) | ||
go statsd.StatsD(registry, interval, prefix, a) | ||
return &gmProvider{registry}, nil | ||
} | ||
|
||
// gmProvider implements the metricslib.Provider interface | ||
// using the github.com/rcrowley/go-metrics library. | ||
type gmProvider struct { | ||
// registry keeps track of registered metrics values | ||
// to keep them separate from the default metrics. | ||
registry gm.Registry | ||
} | ||
|
||
func (p *gmProvider) Names() (names []string) { | ||
p.registry.Each(func(name string, _ interface{}) { | ||
names = append(names, name) | ||
}) | ||
sort.Strings(names) | ||
return names | ||
} | ||
|
||
func (p *gmProvider) Unregister(name string) { | ||
p.registry.Unregister(name) | ||
} | ||
|
||
func (p *gmProvider) UnregisterAll() { | ||
p.registry.UnregisterAll() | ||
} | ||
|
||
func (p *gmProvider) GetTimer(name string) metricslib.Timer { | ||
return gm.GetOrRegisterTimer(name, p.registry) | ||
} |
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
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,25 @@ | ||
package metricslib | ||
|
||
import "time" | ||
|
||
// NoopProvider is a stub implementation of the Provider interface. | ||
type NoopProvider struct{} | ||
|
||
func (p NoopProvider) Names() []string { return nil } | ||
|
||
func (p NoopProvider) Unregister(name string) {} | ||
|
||
func (p NoopProvider) UnregisterAll() {} | ||
|
||
func (p NoopProvider) GetTimer(name string) Timer { return noopTimer } | ||
|
||
var noopTimer = NoopTimer{} | ||
|
||
// NoopTimer is a stub implementation of the Timer interface. | ||
type NoopTimer struct{} | ||
|
||
func (t NoopTimer) UpdateSince(start time.Time) {} | ||
|
||
func (t NoopTimer) Rate1() float64 { return 0 } | ||
|
||
func (t NoopTimer) Percentile(nth float64) float64 { return 0 } |
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,41 @@ | ||
// Package metricslib defines the common interfaces for | ||
// different metrics libraries. | ||
package metricslib | ||
|
||
import "time" | ||
|
||
// Provider defines a common interface for metric libraries. | ||
// An implementation must be safe to be used by multiple | ||
// go routines. | ||
type Provider interface { | ||
// Names returns the list of registered metrics acquired | ||
// through the GetXXX() functions. It should return them | ||
// sorted in alphabetical order. | ||
Names() []string | ||
|
||
// Unregister removes the registered metric and stops | ||
// reporting it to an external backend. | ||
Unregister(name string) | ||
|
||
// UnregisterAll removes all registered metrics and stops | ||
// reporting them to an external backend. | ||
UnregisterAll() | ||
|
||
// GetTimer returns a timer metric for the given name. | ||
// If the metric does not exist yet it should be created | ||
// otherwise the existing metric should be returned. | ||
GetTimer(name string) Timer | ||
} | ||
|
||
// Timer defines a metric for counting and timing durations for events. | ||
type Timer interface { | ||
// Percentile returns the nth percentile of the duration. | ||
Percentile(nth float64) float64 | ||
|
||
// Rate1 returns the 1min rate. | ||
Rate1() float64 | ||
|
||
// UpdateSince counts an event and records the duration | ||
// as the delta between 'start' and the function is called. | ||
UpdateSince(start time.Time) | ||
} |
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
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
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
Oops, something went wrong.