Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/collector_integration.go
5363 views
1
package integrations
2
3
import (
4
"context"
5
"fmt"
6
"net/http"
7
8
"github.com/grafana/agent/pkg/build"
9
"github.com/grafana/agent/pkg/integrations/config"
10
"github.com/prometheus/client_golang/prometheus"
11
"github.com/prometheus/client_golang/prometheus/promhttp"
12
)
13
14
// CollectorIntegration is an integration exposing metrics from one or more Prometheus collectors.
15
type CollectorIntegration struct {
16
name string
17
cs []prometheus.Collector
18
includeExporterMetrics bool
19
runner func(context.Context) error
20
}
21
22
// NewCollectorIntegration creates a basic integration that exposes metrics from multiple prometheus.Collector.
23
func NewCollectorIntegration(name string, configs ...CollectorIntegrationConfig) *CollectorIntegration {
24
i := &CollectorIntegration{
25
name: name,
26
runner: func(ctx context.Context) error {
27
// We don't need to do anything by default, so we can just wait for the context to finish.
28
<-ctx.Done()
29
return ctx.Err()
30
},
31
}
32
for _, configure := range configs {
33
configure(i)
34
}
35
return i
36
}
37
38
// CollectorIntegrationConfig defines constructor configuration for NewCollectorIntegration
39
type CollectorIntegrationConfig func(integration *CollectorIntegration)
40
41
// WithCollectors adds more collectors to the CollectorIntegration being created.
42
func WithCollectors(cs ...prometheus.Collector) CollectorIntegrationConfig {
43
return func(i *CollectorIntegration) {
44
i.cs = append(i.cs, cs...)
45
}
46
}
47
48
// WithRunner replaces the runner of the CollectorIntegration.
49
// The runner function should run while the context provided is not done.
50
func WithRunner(runner func(context.Context) error) CollectorIntegrationConfig {
51
return func(i *CollectorIntegration) {
52
i.runner = runner
53
}
54
}
55
56
// WithExporterMetricsIncluded can enable the exporter metrics if the flag provided is enabled.
57
func WithExporterMetricsIncluded(included bool) CollectorIntegrationConfig {
58
return func(i *CollectorIntegration) {
59
i.includeExporterMetrics = included
60
}
61
}
62
63
// MetricsHandler returns the HTTP handler for the integration.
64
func (i *CollectorIntegration) MetricsHandler() (http.Handler, error) {
65
r := prometheus.NewRegistry()
66
for _, c := range i.cs {
67
if err := r.Register(c); err != nil {
68
return nil, fmt.Errorf("couldn't register %s: %w", i.name, err)
69
}
70
}
71
72
// Register <integration name>_build_info metrics, generally useful for
73
// dashboards that depend on them for discovering targets.
74
if err := r.Register(build.NewCollector(i.name)); err != nil {
75
return nil, fmt.Errorf("couldn't register %s: %w", i.name, err)
76
}
77
78
handler := promhttp.HandlerFor(
79
r,
80
promhttp.HandlerOpts{
81
ErrorHandling: promhttp.ContinueOnError,
82
},
83
)
84
85
if i.includeExporterMetrics {
86
// Note that we have to use reg here to use the same promhttp metrics for
87
// all expositions.
88
handler = promhttp.InstrumentMetricHandler(r, handler)
89
}
90
91
return handler, nil
92
}
93
94
// ScrapeConfigs satisfies Integration.ScrapeConfigs.
95
func (i *CollectorIntegration) ScrapeConfigs() []config.ScrapeConfig {
96
return []config.ScrapeConfig{{
97
JobName: i.name,
98
MetricsPath: "/metrics",
99
}}
100
}
101
102
// Run satisfies Integration.Run.
103
func (i *CollectorIntegration) Run(ctx context.Context) error {
104
return i.runner(ctx)
105
}
106
107