Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/v2/blackbox_exporter/blackbox.go
5403 views
1
package blackbox_exporter_v2
2
3
import (
4
"context"
5
"fmt"
6
"net/http"
7
"path"
8
9
"github.com/go-kit/log"
10
"github.com/gorilla/mux"
11
"github.com/grafana/agent/pkg/integrations/blackbox_exporter"
12
"github.com/grafana/agent/pkg/integrations/v2"
13
"github.com/grafana/agent/pkg/integrations/v2/autoscrape"
14
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
15
blackbox_config "github.com/prometheus/blackbox_exporter/config"
16
"github.com/prometheus/blackbox_exporter/prober"
17
"github.com/prometheus/common/model"
18
"github.com/prometheus/prometheus/config"
19
"github.com/prometheus/prometheus/discovery"
20
"github.com/prometheus/prometheus/discovery/targetgroup"
21
)
22
23
type blackboxHandler struct {
24
cfg *Config
25
modules *blackbox_config.Config
26
log log.Logger
27
}
28
29
func (bbh *blackboxHandler) Targets(ep integrations.Endpoint) []*targetgroup.Group {
30
integrationNameValue := model.LabelValue("integrations/" + bbh.cfg.Name())
31
id, _ := bbh.cfg.Identifier(bbh.cfg.globals)
32
33
group := &targetgroup.Group{
34
Labels: model.LabelSet{
35
model.InstanceLabel: model.LabelValue(id),
36
model.JobLabel: integrationNameValue,
37
"agent_hostname": model.LabelValue(bbh.cfg.globals.AgentIdentifier),
38
39
// Meta labels that can be used during SD.
40
"__meta_agent_integration_name": model.LabelValue(bbh.cfg.Name()),
41
"__meta_agent_integration_instance": model.LabelValue(bbh.cfg.Name()),
42
"__meta_agent_integration_autoscrape": model.LabelValue(metricsutils.BoolToString(*bbh.cfg.Common.Autoscrape.Enable)),
43
},
44
Source: fmt.Sprintf("%s/%s", bbh.cfg.Name(), bbh.cfg.Name()),
45
}
46
47
for _, lbl := range bbh.cfg.Common.ExtraLabels {
48
group.Labels[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)
49
}
50
51
for _, t := range bbh.cfg.BlackboxTargets {
52
labelSet := model.LabelSet{
53
model.AddressLabel: model.LabelValue(ep.Host),
54
model.MetricsPathLabel: model.LabelValue(path.Join(ep.Prefix, "metrics")),
55
"blackbox_target": model.LabelValue(t.Target),
56
"__param_target": model.LabelValue(t.Target),
57
}
58
59
if t.Module != "" {
60
labelSet = labelSet.Merge(model.LabelSet{
61
"__param_module": model.LabelValue(t.Module),
62
})
63
}
64
group.Targets = append(group.Targets, labelSet)
65
}
66
67
return []*targetgroup.Group{group}
68
}
69
70
func (bbh *blackboxHandler) ScrapeConfigs(sd discovery.Configs) []*autoscrape.ScrapeConfig {
71
if !*bbh.cfg.Common.Autoscrape.Enable {
72
return nil
73
}
74
name := bbh.cfg.Name()
75
cfg := config.DefaultScrapeConfig
76
cfg.JobName = fmt.Sprintf("%s/%s", name, name)
77
cfg.Scheme = bbh.cfg.globals.AgentBaseURL.Scheme
78
cfg.ServiceDiscoveryConfigs = sd
79
cfg.ScrapeInterval = bbh.cfg.Common.Autoscrape.ScrapeInterval
80
cfg.ScrapeTimeout = bbh.cfg.Common.Autoscrape.ScrapeTimeout
81
cfg.RelabelConfigs = bbh.cfg.Common.Autoscrape.RelabelConfigs
82
cfg.MetricRelabelConfigs = bbh.cfg.Common.Autoscrape.MetricRelabelConfigs
83
84
return []*autoscrape.ScrapeConfig{{
85
Instance: bbh.cfg.Common.Autoscrape.MetricsInstance,
86
Config: cfg,
87
}}
88
}
89
90
func (bbh *blackboxHandler) Handler(prefix string) (http.Handler, error) {
91
r := mux.NewRouter()
92
r.Handle(path.Join(prefix, "metrics"), bbh.createHandler(bbh.cfg.BlackboxTargets))
93
94
return r, nil
95
}
96
97
func (bbh *blackboxHandler) createHandler(targets []blackbox_exporter.BlackboxTarget) http.HandlerFunc {
98
blackboxTargets := make(map[string]blackbox_exporter.BlackboxTarget)
99
for _, target := range targets {
100
blackboxTargets[target.Target] = target
101
}
102
return func(w http.ResponseWriter, r *http.Request) {
103
params := r.URL.Query()
104
105
targetName := params.Get("target")
106
t := blackboxTargets[targetName]
107
moduleName := params.Get("module")
108
if moduleName == "" {
109
params.Set("module", t.Module)
110
}
111
112
prober.Handler(w, r, bbh.modules, bbh.log, &prober.ResultHistory{}, bbh.cfg.ProbeTimeoutOffset, params)
113
}
114
}
115
116
// Static typecheck tests
117
var (
118
_ integrations.Integration = (*blackboxHandler)(nil)
119
_ integrations.HTTPIntegration = (*blackboxHandler)(nil)
120
_ integrations.MetricsIntegration = (*blackboxHandler)(nil)
121
)
122
123
func (bbh *blackboxHandler) RunIntegration(ctx context.Context) error {
124
<-ctx.Done()
125
return nil
126
}
127
128