Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/internal/controller/metrics.go
4095 views
1
package controller
2
3
import "github.com/prometheus/client_golang/prometheus"
4
5
// controllerMetrics contains the metrics for components controller
6
type controllerMetrics struct {
7
r prometheus.Registerer
8
9
controllerEvaluation prometheus.Gauge
10
componentEvaluationTime prometheus.Histogram
11
}
12
13
// newControllerMetrics inits the metrics for the components controller
14
func newControllerMetrics(r prometheus.Registerer) *controllerMetrics {
15
cm := controllerMetrics{r: r}
16
17
cm.controllerEvaluation = prometheus.NewGauge(prometheus.GaugeOpts{
18
Name: "agent_component_controller_evaluating",
19
Help: "Tracks if the controller is currently in the middle of a graph evaluation",
20
})
21
22
cm.componentEvaluationTime = prometheus.NewHistogram(
23
prometheus.HistogramOpts{
24
Name: "agent_component_evaluation_seconds",
25
Help: "Time spent performing component evaluation",
26
},
27
)
28
29
if r != nil {
30
r.MustRegister(
31
cm.controllerEvaluation,
32
cm.componentEvaluationTime,
33
)
34
}
35
return &cm
36
}
37
38
type controllerCollector struct {
39
l *Loader
40
runningComponentsTotal *prometheus.Desc
41
}
42
43
func newControllerCollector(l *Loader) prometheus.Collector {
44
return &controllerCollector{
45
l: l,
46
runningComponentsTotal: prometheus.NewDesc(
47
"agent_component_controller_running_components",
48
"Total number of running components.",
49
[]string{"health_type"},
50
nil,
51
),
52
}
53
}
54
55
func (cc *controllerCollector) Collect(ch chan<- prometheus.Metric) {
56
componentsByHealth := make(map[string]int)
57
58
for _, component := range cc.l.Components() {
59
health := component.CurrentHealth().Health.String()
60
componentsByHealth[health]++
61
component.register.Collect(ch)
62
}
63
64
for health, count := range componentsByHealth {
65
ch <- prometheus.MustNewConstMetric(cc.runningComponentsTotal, prometheus.GaugeValue, float64(count), health)
66
}
67
}
68
69
func (cc *controllerCollector) Describe(ch chan<- *prometheus.Desc) {
70
ch <- cc.runningComponentsTotal
71
}
72
73