Path: blob/main/component/prometheus/exporter/blackbox/blackbox.go
4096 views
package blackbox12import (3"errors"4"fmt"5"time"67blackbox_config "github.com/prometheus/blackbox_exporter/config"8"gopkg.in/yaml.v2"910"github.com/grafana/agent/component"11"github.com/grafana/agent/component/discovery"12"github.com/grafana/agent/component/prometheus/exporter"13"github.com/grafana/agent/pkg/integrations"14"github.com/grafana/agent/pkg/integrations/blackbox_exporter"15)1617func init() {18component.Register(component.Registration{19Name: "prometheus.exporter.blackbox",20Args: Arguments{},21Exports: exporter.Exports{},22Build: exporter.NewMultiTarget(createExporter, "blackbox", buildBlackboxTargets),23})24}2526func createExporter(opts component.Options, args component.Arguments) (integrations.Integration, error) {27a := args.(Arguments)28return a.Convert().NewIntegration(opts.Logger)29}3031// buildBlackboxTargets creates the exporter's discovery targets based on the defined blackbox targets.32func buildBlackboxTargets(baseTarget discovery.Target, args component.Arguments) []discovery.Target {33var targets []discovery.Target3435a := args.(Arguments)36for _, tgt := range a.Targets {37target := make(discovery.Target)38for k, v := range baseTarget {39target[k] = v40}4142target["job"] = target["job"] + "/" + tgt.Name43target["__param_target"] = tgt.Target44if tgt.Module != "" {45target["__param_module"] = tgt.Module46}4748targets = append(targets, target)49}5051return targets52}5354// DefaultArguments holds non-zero default options for Arguments when it is55// unmarshaled from river.56var DefaultArguments = Arguments{57ProbeTimeoutOffset: 500 * time.Millisecond,58}5960// BlackboxTarget defines a target to be used by the exporter.61type BlackboxTarget struct {62Name string `river:",label"`63Target string `river:"address,attr"`64Module string `river:"module,attr,optional"`65}6667type TargetBlock []BlackboxTarget6869// Convert converts the component's TargetBlock to a slice of integration's BlackboxTarget.70func (t TargetBlock) Convert() []blackbox_exporter.BlackboxTarget {71targets := make([]blackbox_exporter.BlackboxTarget, 0, len(t))72for _, target := range t {73targets = append(targets, blackbox_exporter.BlackboxTarget{74Name: target.Name,75Target: target.Target,76Module: target.Module,77})78}79return targets80}8182type Arguments struct {83ConfigFile string `river:"config_file,attr,optional"`84Config string `river:"config,attr,optional"`85Targets TargetBlock `river:"target,block"`86ProbeTimeoutOffset time.Duration `river:"probe_timeout_offset,attr,optional"`87ConfigStruct blackbox_config.Config88}8990// UnmarshalRiver implements River unmarshalling for Arguments.91func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {92*a = DefaultArguments9394type args Arguments95if err := f((*args)(a)); err != nil {96return err97}9899if a.ConfigFile != "" && a.Config != "" {100return errors.New("config and config_file are mutually exclusive")101}102103err := yaml.UnmarshalStrict([]byte(a.Config), &a.ConfigStruct)104if err != nil {105return fmt.Errorf("invalid backbox_exporter config: %s", err)106}107108return nil109}110111// Convert converts the component's Arguments to the integration's Config.112func (a *Arguments) Convert() *blackbox_exporter.Config {113return &blackbox_exporter.Config{114BlackboxConfigFile: a.ConfigFile,115BlackboxConfig: a.ConfigStruct,116BlackboxTargets: a.Targets.Convert(),117ProbeTimeoutOffset: a.ProbeTimeoutOffset.Seconds(),118}119}120121122