forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
52 lines (43 loc) · 1.45 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package toxiproxy
import (
"net/http"
"github.com/Shopify/toxiproxy/v2/collectors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// NewMetricsContainer initializes a container for storing all prometheus metrics.
func NewMetricsContainer(registry *prometheus.Registry) *metricsContainer {
if registry == nil {
registry = prometheus.NewRegistry()
}
return &metricsContainer{
registry: registry,
}
}
type metricsContainer struct {
RuntimeMetrics *collectors.RuntimeMetricCollectors
ProxyMetrics *collectors.ProxyMetricCollectors
registry *prometheus.Registry
}
func (m *metricsContainer) runtimeMetricsEnabled() bool {
return m.RuntimeMetrics != nil
}
func (m *metricsContainer) proxyMetricsEnabled() bool {
return m.ProxyMetrics != nil
}
// anyMetricsEnabled determines whether we have any prometheus metrics registered for exporting.
func (m *metricsContainer) anyMetricsEnabled() bool {
return m.runtimeMetricsEnabled() || m.proxyMetricsEnabled()
}
// handler returns an HTTP handler with the necessary collectors registered
// via a global prometheus registry.
func (m *metricsContainer) handler() http.Handler {
if m.runtimeMetricsEnabled() {
m.registry.MustRegister(m.RuntimeMetrics.Collectors()...)
}
if m.proxyMetricsEnabled() {
m.registry.MustRegister(m.ProxyMetrics.Collectors()...)
}
return promhttp.HandlerFor(
m.registry, promhttp.HandlerOpts{Registry: m.registry})
}