|
| 1 | +// Package `metrics` provides some tools useful for gathering and |
| 2 | +// exposing system metrics for external monitoring tools. |
| 3 | +// |
| 4 | +// Currently, this package is intended to use with Prometheus but |
| 5 | +// can be easily extended if needed. Also, not all Prometheus metric |
| 6 | +// types are implemented. The main motivation of creating a custom |
| 7 | +// package was a need to avoid usage of external unaudited dependencies. |
| 8 | +// |
| 9 | +// Following specifications was used as reference: |
| 10 | +// - https://prometheus.io/docs/instrumenting/writing_clientlibs/ |
| 11 | +// - https://prometheus.io/docs/instrumenting/exposition_formats/ |
| 12 | +package metrics |
| 13 | + |
| 14 | +import ( |
| 15 | + "fmt" |
| 16 | + "io" |
| 17 | + "net/http" |
| 18 | + "strconv" |
| 19 | + "strings" |
| 20 | + "sync" |
| 21 | + |
| 22 | + "github.com/ipfs/go-log" |
| 23 | +) |
| 24 | + |
| 25 | +var logger = log.Logger("keep-metrics") |
| 26 | + |
| 27 | +type metric interface { |
| 28 | + expose() string |
| 29 | +} |
| 30 | + |
| 31 | +// Registry performs all management of metrics. Specifically, it allows |
| 32 | +// to registering new metrics and exposing them through the metrics server. |
| 33 | +type Registry struct { |
| 34 | + application string |
| 35 | + identifier string |
| 36 | + |
| 37 | + metrics map[string]metric |
| 38 | + metricsMutex sync.RWMutex |
| 39 | +} |
| 40 | + |
| 41 | +// NewRegistry creates a new metrics registry. |
| 42 | +func NewRegistry(application, identifier string) *Registry { |
| 43 | + return &Registry{ |
| 44 | + application: application, |
| 45 | + identifier: identifier, |
| 46 | + metrics: make(map[string]metric), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// EnableServer enables the metrics server on the given port. Data will |
| 51 | +// be exposed on `/metrics` path. |
| 52 | +func (r *Registry) EnableServer(port int) { |
| 53 | + server := &http.Server{Addr: ":" + strconv.Itoa(port)} |
| 54 | + |
| 55 | + http.HandleFunc("/metrics", func(response http.ResponseWriter, _ *http.Request) { |
| 56 | + if _, err := io.WriteString(response, r.exposeMetrics()); err != nil { |
| 57 | + logger.Errorf("could not write response: [%v]", err) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + go func() { |
| 62 | + if err := server.ListenAndServe(); err != http.ErrServerClosed { |
| 63 | + logger.Errorf("metrics server error: [%v]", err) |
| 64 | + } |
| 65 | + }() |
| 66 | +} |
| 67 | + |
| 68 | +// Exposes all registered metrics in their text format. |
| 69 | +func (r *Registry) exposeMetrics() string { |
| 70 | + r.metricsMutex.RLock() |
| 71 | + defer r.metricsMutex.RUnlock() |
| 72 | + |
| 73 | + metrics := make([]string, 0) |
| 74 | + for _, metric := range r.metrics { |
| 75 | + metrics = append(metrics, metric.expose()) |
| 76 | + } |
| 77 | + |
| 78 | + return strings.Join(metrics, "\n\n") |
| 79 | +} |
| 80 | + |
| 81 | +// NewGauge creates and registers a new gauge metric which will be exposed |
| 82 | +// through the metrics server. In case a metric already exists, an error |
| 83 | +// will be returned. |
| 84 | +func (r *Registry) NewGauge(name string) (*Gauge, error) { |
| 85 | + r.metricsMutex.Lock() |
| 86 | + defer r.metricsMutex.Unlock() |
| 87 | + |
| 88 | + if _, exists := r.metrics[name]; exists { |
| 89 | + return nil, fmt.Errorf("gauge [%v] already exists", name) |
| 90 | + } |
| 91 | + |
| 92 | + gauge := &Gauge{ |
| 93 | + name: name, |
| 94 | + labels: map[string]string{ |
| 95 | + "application": r.application, |
| 96 | + "identifier": r.identifier, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + r.metrics[name] = gauge |
| 101 | + return gauge, nil |
| 102 | +} |
0 commit comments