mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 11:28:59 +00:00
65aaa607ab
Change made by: - running "gvt fetch" on each of the packages mentioned in Godeps/Godeps.json - `rm -rf Godeps` - tweaking the build scripts to not mention Godeps - tweaking the build scripts to test `./lib/...`, `./cmd/...` explicitly (to avoid testing vendor) - tweaking the build scripts to not juggle GOPATH for Godeps and instead set GO15VENDOREXPERIMENT. This also results in some updated packages at the same time I bet. Building with Go 1.3 and 1.4 still *works* but won't use our vendored dependencies - the user needs to have the actual packages in their GOPATH then, which they'll get with a normal "go get". Building with Go 1.6+ will get our vendored dependencies by default even when not using our build script, which is nice. By doing this we gain some freedom in that we can pick and choose manually what to include in vendor, as it's not based on just dependency analysis of our own code. This is also a risk as we might pick up dependencies we are unaware of, as the build may work locally with those packages present in GOPATH. On the other hand the build server will detect this as it has no packages in it's GOPATH beyond what is included in the repo. Recommended tool to manage dependencies is github.com/FiloSottile/gvt.
248 lines
6.7 KiB
Go
248 lines
6.7 KiB
Go
package metrics
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"sync"
|
|
)
|
|
|
|
// DuplicateMetric is the error returned by Registry.Register when a metric
|
|
// already exists. If you mean to Register that metric you must first
|
|
// Unregister the existing metric.
|
|
type DuplicateMetric string
|
|
|
|
func (err DuplicateMetric) Error() string {
|
|
return fmt.Sprintf("duplicate metric: %s", string(err))
|
|
}
|
|
|
|
// A Registry holds references to a set of metrics by name and can iterate
|
|
// over them, calling callback functions provided by the user.
|
|
//
|
|
// This is an interface so as to encourage other structs to implement
|
|
// the Registry API as appropriate.
|
|
type Registry interface {
|
|
|
|
// Call the given function for each registered metric.
|
|
Each(func(string, interface{}))
|
|
|
|
// Get the metric by the given name or nil if none is registered.
|
|
Get(string) interface{}
|
|
|
|
// Gets an existing metric or registers the given one.
|
|
// The interface can be the metric to register if not found in registry,
|
|
// or a function returning the metric for lazy instantiation.
|
|
GetOrRegister(string, interface{}) interface{}
|
|
|
|
// Register the given metric under the given name.
|
|
Register(string, interface{}) error
|
|
|
|
// Run all registered healthchecks.
|
|
RunHealthchecks()
|
|
|
|
// Unregister the metric with the given name.
|
|
Unregister(string)
|
|
|
|
// Unregister all metrics. (Mostly for testing.)
|
|
UnregisterAll()
|
|
}
|
|
|
|
// The standard implementation of a Registry is a mutex-protected map
|
|
// of names to metrics.
|
|
type StandardRegistry struct {
|
|
metrics map[string]interface{}
|
|
mutex sync.Mutex
|
|
}
|
|
|
|
// Create a new registry.
|
|
func NewRegistry() Registry {
|
|
return &StandardRegistry{metrics: make(map[string]interface{})}
|
|
}
|
|
|
|
// Call the given function for each registered metric.
|
|
func (r *StandardRegistry) Each(f func(string, interface{})) {
|
|
for name, i := range r.registered() {
|
|
f(name, i)
|
|
}
|
|
}
|
|
|
|
// Get the metric by the given name or nil if none is registered.
|
|
func (r *StandardRegistry) Get(name string) interface{} {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
return r.metrics[name]
|
|
}
|
|
|
|
// Gets an existing metric or creates and registers a new one. Threadsafe
|
|
// alternative to calling Get and Register on failure.
|
|
// The interface can be the metric to register if not found in registry,
|
|
// or a function returning the metric for lazy instantiation.
|
|
func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
if metric, ok := r.metrics[name]; ok {
|
|
return metric
|
|
}
|
|
if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
|
|
i = v.Call(nil)[0].Interface()
|
|
}
|
|
r.register(name, i)
|
|
return i
|
|
}
|
|
|
|
// Register the given metric under the given name. Returns a DuplicateMetric
|
|
// if a metric by the given name is already registered.
|
|
func (r *StandardRegistry) Register(name string, i interface{}) error {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
return r.register(name, i)
|
|
}
|
|
|
|
// Run all registered healthchecks.
|
|
func (r *StandardRegistry) RunHealthchecks() {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
for _, i := range r.metrics {
|
|
if h, ok := i.(Healthcheck); ok {
|
|
h.Check()
|
|
}
|
|
}
|
|
}
|
|
|
|
// Unregister the metric with the given name.
|
|
func (r *StandardRegistry) Unregister(name string) {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
delete(r.metrics, name)
|
|
}
|
|
|
|
// Unregister all metrics. (Mostly for testing.)
|
|
func (r *StandardRegistry) UnregisterAll() {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
for name, _ := range r.metrics {
|
|
delete(r.metrics, name)
|
|
}
|
|
}
|
|
|
|
func (r *StandardRegistry) register(name string, i interface{}) error {
|
|
if _, ok := r.metrics[name]; ok {
|
|
return DuplicateMetric(name)
|
|
}
|
|
switch i.(type) {
|
|
case Counter, Gauge, GaugeFloat64, Healthcheck, Histogram, Meter, Timer:
|
|
r.metrics[name] = i
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *StandardRegistry) registered() map[string]interface{} {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
metrics := make(map[string]interface{}, len(r.metrics))
|
|
for name, i := range r.metrics {
|
|
metrics[name] = i
|
|
}
|
|
return metrics
|
|
}
|
|
|
|
type PrefixedRegistry struct {
|
|
underlying Registry
|
|
prefix string
|
|
}
|
|
|
|
func NewPrefixedRegistry(prefix string) Registry {
|
|
return &PrefixedRegistry{
|
|
underlying: NewRegistry(),
|
|
prefix: prefix,
|
|
}
|
|
}
|
|
|
|
func NewPrefixedChildRegistry(parent Registry, prefix string) Registry {
|
|
return &PrefixedRegistry{
|
|
underlying: parent,
|
|
prefix: prefix,
|
|
}
|
|
}
|
|
|
|
// Call the given function for each registered metric.
|
|
func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
|
|
r.underlying.Each(fn)
|
|
}
|
|
|
|
// Get the metric by the given name or nil if none is registered.
|
|
func (r *PrefixedRegistry) Get(name string) interface{} {
|
|
return r.underlying.Get(name)
|
|
}
|
|
|
|
// Gets an existing metric or registers the given one.
|
|
// The interface can be the metric to register if not found in registry,
|
|
// or a function returning the metric for lazy instantiation.
|
|
func (r *PrefixedRegistry) GetOrRegister(name string, metric interface{}) interface{} {
|
|
realName := r.prefix + name
|
|
return r.underlying.GetOrRegister(realName, metric)
|
|
}
|
|
|
|
// Register the given metric under the given name. The name will be prefixed.
|
|
func (r *PrefixedRegistry) Register(name string, metric interface{}) error {
|
|
realName := r.prefix + name
|
|
return r.underlying.Register(realName, metric)
|
|
}
|
|
|
|
// Run all registered healthchecks.
|
|
func (r *PrefixedRegistry) RunHealthchecks() {
|
|
r.underlying.RunHealthchecks()
|
|
}
|
|
|
|
// Unregister the metric with the given name. The name will be prefixed.
|
|
func (r *PrefixedRegistry) Unregister(name string) {
|
|
realName := r.prefix + name
|
|
r.underlying.Unregister(realName)
|
|
}
|
|
|
|
// Unregister all metrics. (Mostly for testing.)
|
|
func (r *PrefixedRegistry) UnregisterAll() {
|
|
r.underlying.UnregisterAll()
|
|
}
|
|
|
|
var DefaultRegistry Registry = NewRegistry()
|
|
|
|
// Call the given function for each registered metric.
|
|
func Each(f func(string, interface{})) {
|
|
DefaultRegistry.Each(f)
|
|
}
|
|
|
|
// Get the metric by the given name or nil if none is registered.
|
|
func Get(name string) interface{} {
|
|
return DefaultRegistry.Get(name)
|
|
}
|
|
|
|
// Gets an existing metric or creates and registers a new one. Threadsafe
|
|
// alternative to calling Get and Register on failure.
|
|
func GetOrRegister(name string, i interface{}) interface{} {
|
|
return DefaultRegistry.GetOrRegister(name, i)
|
|
}
|
|
|
|
// Register the given metric under the given name. Returns a DuplicateMetric
|
|
// if a metric by the given name is already registered.
|
|
func Register(name string, i interface{}) error {
|
|
return DefaultRegistry.Register(name, i)
|
|
}
|
|
|
|
// Register the given metric under the given name. Panics if a metric by the
|
|
// given name is already registered.
|
|
func MustRegister(name string, i interface{}) {
|
|
if err := Register(name, i); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Run all registered healthchecks.
|
|
func RunHealthchecks() {
|
|
DefaultRegistry.RunHealthchecks()
|
|
}
|
|
|
|
// Unregister the metric with the given name.
|
|
func Unregister(name string) {
|
|
DefaultRegistry.Unregister(name)
|
|
}
|