-
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
34d6959
commit 8a34961
Showing
10 changed files
with
228 additions
and
85 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
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,81 @@ | ||
package metrics | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net" | ||
"os" | ||
"sort" | ||
"time" | ||
|
||
graphite "github.com/cyberdelia/go-metrics-graphite" | ||
statsd "github.com/pubnub/go-metrics-statsd" | ||
gm "github.com/rcrowley/go-metrics" | ||
) | ||
|
||
// gmStdoutRegistry returns a go-metrics registry that reports to stdout. | ||
func gmStdoutRegistry(interval time.Duration) (Registry, error) { | ||
logger := log.New(os.Stderr, "localhost: ", log.Lmicroseconds) | ||
r := gm.NewRegistry() | ||
go gm.Log(r, interval, logger) | ||
return &gmRegistry{r}, nil | ||
} | ||
|
||
// gmGraphiteRegistry returns a go-metrics registry that reports to a Graphite server. | ||
func gmGraphiteRegistry(prefix, addr string, interval time.Duration) (Registry, error) { | ||
if addr == "" { | ||
return nil, errors.New(" graphite addr missing") | ||
} | ||
|
||
a, err := net.ResolveTCPAddr("tcp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf(" cannot connect to Graphite: %s", err) | ||
} | ||
|
||
r := gm.NewRegistry() | ||
go graphite.Graphite(r, interval, prefix, a) | ||
return &gmRegistry{r}, nil | ||
} | ||
|
||
// gmStatsDRegistry returns a go-metrics registry that reports to a StatsD server. | ||
func gmStatsDRegistry(prefix, addr string, interval time.Duration) (Registry, error) { | ||
if addr == "" { | ||
return nil, errors.New(" statsd addr missing") | ||
} | ||
|
||
a, err := net.ResolveUDPAddr("udp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf(" cannot connect to StatsD: %s", err) | ||
} | ||
|
||
r := gm.NewRegistry() | ||
go statsd.StatsD(r, interval, prefix, a) | ||
return &gmRegistry{r}, nil | ||
} | ||
|
||
// gmRegistry implements the Registry interface | ||
// using the github.com/rcrowley/go-metrics library. | ||
type gmRegistry struct { | ||
r gm.Registry | ||
} | ||
|
||
func (p *gmRegistry) Names() (names []string) { | ||
p.r.Each(func(name string, _ interface{}) { | ||
names = append(names, name) | ||
}) | ||
sort.Strings(names) | ||
return names | ||
} | ||
|
||
func (p *gmRegistry) Unregister(name string) { | ||
p.r.Unregister(name) | ||
} | ||
|
||
func (p *gmRegistry) UnregisterAll() { | ||
p.r.UnregisterAll() | ||
} | ||
|
||
func (p *gmRegistry) GetTimer(name string) Timer { | ||
return gm.GetOrRegisterTimer(name, p.r) | ||
} |
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 metrics | ||
|
||
import "time" | ||
|
||
// NoopRegistry is a stub implementation of the Registry interface. | ||
type NoopRegistry struct{} | ||
|
||
func (p NoopRegistry) Names() []string { return nil } | ||
|
||
func (p NoopRegistry) Unregister(name string) {} | ||
|
||
func (p NoopRegistry) UnregisterAll() {} | ||
|
||
func (p NoopRegistry) 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,40 @@ | ||
package metrics | ||
|
||
import "time" | ||
|
||
// Registry defines an interface for metrics values which | ||
// can be implemented by different metrics libraries. | ||
// An implementation must be safe to be used by multiple | ||
// go routines. | ||
type Registry 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.