binding.go 2.73 KB
Newer Older
1 2 3 4 5 6 7
package metricsprometheus

import (
	"strings"

	logging "github.com/ipfs/go-log"
	metrics "github.com/ipfs/go-metrics-interface"
Steven Allen's avatar
Steven Allen committed
8
	pro "github.com/prometheus/client_golang/prometheus"
9 10 11 12
)

var log logging.EventLogger = logging.Logger("metrics-prometheus")

13
func Inject() error {
14
	return metrics.InjectImpl(newCreator)
15 16 17 18
}

func newCreator(name, helptext string) metrics.Creator {
	return &creator{
Jakub Sztandera's avatar
Jakub Sztandera committed
19
		name:     strings.Replace(name, ".", "_", -1),
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
		helptext: helptext,
	}
}

var _ metrics.Creator = &creator{}

type creator struct {
	name     string
	helptext string
}

func (c *creator) Counter() metrics.Counter {
	res := pro.NewCounter(pro.CounterOpts{
		Name: c.name,
		Help: c.helptext,
	})
36 37 38 39 40 41 42 43 44 45
	err := pro.Register(res)
	if err != nil {
		if registered, ok := err.(pro.AlreadyRegisteredError); ok {
			if existing, ok := registered.ExistingCollector.(pro.Counter); ok {
				log.Warningf("using existing prometheus collector: %s\n", c.name)
				return existing
			}
		}
		log.Errorf("Registering prometheus collector, name: %s, error: %s\n", c.name, err.Error())
	}
46 47 48 49 50 51 52
	return res
}
func (c *creator) Gauge() metrics.Gauge {
	res := pro.NewGauge(pro.GaugeOpts{
		Name: c.name,
		Help: c.helptext,
	})
53 54 55 56 57 58 59 60 61 62
	err := pro.Register(res)
	if err != nil {
		if registered, ok := err.(pro.AlreadyRegisteredError); ok {
			if existing, ok := registered.ExistingCollector.(pro.Gauge); ok {
				log.Warningf("using existing prometheus collector: %s\n", c.name)
				return existing
			}
		}
		log.Errorf("Registering prometheus collector, name: %s, error: %s\n", c.name, err.Error())
	}
63 64 65 66 67 68 69 70
	return res
}
func (c *creator) Histogram(buckets []float64) metrics.Histogram {
	res := pro.NewHistogram(pro.HistogramOpts{
		Name:    c.name,
		Help:    c.helptext,
		Buckets: buckets,
	})
71 72 73 74 75 76 77 78 79 80
	err := pro.Register(res)
	if err != nil {
		if registered, ok := err.(pro.AlreadyRegisteredError); ok {
			if existing, ok := registered.ExistingCollector.(pro.Histogram); ok {
				log.Warningf("using existing prometheus collector: %s\n", c.name)
				return existing
			}
		}
		log.Errorf("Registering prometheus collector, name: %s, error: %s\n", c.name, err.Error())
	}
81 82 83 84 85 86 87 88 89 90 91 92 93
	return res
}

func (c *creator) Summary(opts metrics.SummaryOpts) metrics.Summary {
	res := pro.NewSummary(pro.SummaryOpts{
		Name: c.name,
		Help: c.helptext,

		Objectives: opts.Objectives,
		MaxAge:     opts.MaxAge,
		AgeBuckets: opts.AgeBuckets,
		BufCap:     opts.BufCap,
	})
94
	err := pro.Register(res)
95
	if err != nil {
96 97 98 99 100 101
		if registered, ok := err.(pro.AlreadyRegisteredError); ok {
			if existing, ok := registered.ExistingCollector.(pro.Summary); ok {
				log.Warningf("using existing prometheus collector: %s\n", c.name)
				return existing
			}
		}
102 103
		log.Errorf("Registering prometheus collector, name: %s, error: %s\n", c.name, err.Error())
	}
104
	return res
105
}