Path: blob/main/pkg/integrations/v2/blackbox_exporter/blackbox.go
5403 views
package blackbox_exporter_v212import (3"context"4"fmt"5"net/http"6"path"78"github.com/go-kit/log"9"github.com/gorilla/mux"10"github.com/grafana/agent/pkg/integrations/blackbox_exporter"11"github.com/grafana/agent/pkg/integrations/v2"12"github.com/grafana/agent/pkg/integrations/v2/autoscrape"13"github.com/grafana/agent/pkg/integrations/v2/metricsutils"14blackbox_config "github.com/prometheus/blackbox_exporter/config"15"github.com/prometheus/blackbox_exporter/prober"16"github.com/prometheus/common/model"17"github.com/prometheus/prometheus/config"18"github.com/prometheus/prometheus/discovery"19"github.com/prometheus/prometheus/discovery/targetgroup"20)2122type blackboxHandler struct {23cfg *Config24modules *blackbox_config.Config25log log.Logger26}2728func (bbh *blackboxHandler) Targets(ep integrations.Endpoint) []*targetgroup.Group {29integrationNameValue := model.LabelValue("integrations/" + bbh.cfg.Name())30id, _ := bbh.cfg.Identifier(bbh.cfg.globals)3132group := &targetgroup.Group{33Labels: model.LabelSet{34model.InstanceLabel: model.LabelValue(id),35model.JobLabel: integrationNameValue,36"agent_hostname": model.LabelValue(bbh.cfg.globals.AgentIdentifier),3738// Meta labels that can be used during SD.39"__meta_agent_integration_name": model.LabelValue(bbh.cfg.Name()),40"__meta_agent_integration_instance": model.LabelValue(bbh.cfg.Name()),41"__meta_agent_integration_autoscrape": model.LabelValue(metricsutils.BoolToString(*bbh.cfg.Common.Autoscrape.Enable)),42},43Source: fmt.Sprintf("%s/%s", bbh.cfg.Name(), bbh.cfg.Name()),44}4546for _, lbl := range bbh.cfg.Common.ExtraLabels {47group.Labels[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)48}4950for _, t := range bbh.cfg.BlackboxTargets {51labelSet := model.LabelSet{52model.AddressLabel: model.LabelValue(ep.Host),53model.MetricsPathLabel: model.LabelValue(path.Join(ep.Prefix, "metrics")),54"blackbox_target": model.LabelValue(t.Target),55"__param_target": model.LabelValue(t.Target),56}5758if t.Module != "" {59labelSet = labelSet.Merge(model.LabelSet{60"__param_module": model.LabelValue(t.Module),61})62}63group.Targets = append(group.Targets, labelSet)64}6566return []*targetgroup.Group{group}67}6869func (bbh *blackboxHandler) ScrapeConfigs(sd discovery.Configs) []*autoscrape.ScrapeConfig {70if !*bbh.cfg.Common.Autoscrape.Enable {71return nil72}73name := bbh.cfg.Name()74cfg := config.DefaultScrapeConfig75cfg.JobName = fmt.Sprintf("%s/%s", name, name)76cfg.Scheme = bbh.cfg.globals.AgentBaseURL.Scheme77cfg.ServiceDiscoveryConfigs = sd78cfg.ScrapeInterval = bbh.cfg.Common.Autoscrape.ScrapeInterval79cfg.ScrapeTimeout = bbh.cfg.Common.Autoscrape.ScrapeTimeout80cfg.RelabelConfigs = bbh.cfg.Common.Autoscrape.RelabelConfigs81cfg.MetricRelabelConfigs = bbh.cfg.Common.Autoscrape.MetricRelabelConfigs8283return []*autoscrape.ScrapeConfig{{84Instance: bbh.cfg.Common.Autoscrape.MetricsInstance,85Config: cfg,86}}87}8889func (bbh *blackboxHandler) Handler(prefix string) (http.Handler, error) {90r := mux.NewRouter()91r.Handle(path.Join(prefix, "metrics"), bbh.createHandler(bbh.cfg.BlackboxTargets))9293return r, nil94}9596func (bbh *blackboxHandler) createHandler(targets []blackbox_exporter.BlackboxTarget) http.HandlerFunc {97blackboxTargets := make(map[string]blackbox_exporter.BlackboxTarget)98for _, target := range targets {99blackboxTargets[target.Target] = target100}101return func(w http.ResponseWriter, r *http.Request) {102params := r.URL.Query()103104targetName := params.Get("target")105t := blackboxTargets[targetName]106moduleName := params.Get("module")107if moduleName == "" {108params.Set("module", t.Module)109}110111prober.Handler(w, r, bbh.modules, bbh.log, &prober.ResultHistory{}, bbh.cfg.ProbeTimeoutOffset, params)112}113}114115// Static typecheck tests116var (117_ integrations.Integration = (*blackboxHandler)(nil)118_ integrations.HTTPIntegration = (*blackboxHandler)(nil)119_ integrations.MetricsIntegration = (*blackboxHandler)(nil)120)121122func (bbh *blackboxHandler) RunIntegration(ctx context.Context) error {123<-ctx.Done()124return nil125}126127128