Path: blob/main/pkg/integrations/consul_exporter/consul_exporter.go
5393 views
// Package consul_exporter embeds https://github.com/prometheus/consul_exporter1package consul_exporter //nolint:golint23import (4"fmt"5"net/url"6"time"78"github.com/go-kit/log"9"github.com/grafana/agent/pkg/integrations"10integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"11"github.com/grafana/agent/pkg/integrations/v2/metricsutils"12consul_api "github.com/hashicorp/consul/api"13"github.com/prometheus/consul_exporter/pkg/exporter"14)1516// DefaultConfig holds the default settings for the consul_exporter integration.17var DefaultConfig = Config{18Server: "http://localhost:8500",19Timeout: 500 * time.Millisecond,20AllowStale: true,21KVFilter: ".*",22HealthSummary: true,23}2425// Config controls the consul_exporter integration.26type Config struct {27Server string `yaml:"server,omitempty"`28CAFile string `yaml:"ca_file,omitempty"`29CertFile string `yaml:"cert_file,omitempty"`30KeyFile string `yaml:"key_file,omitempty"`31ServerName string `yaml:"server_name,omitempty"`32Timeout time.Duration `yaml:"timeout,omitempty"`33InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`34RequestLimit int `yaml:"concurrent_request_limit,omitempty"`35AllowStale bool `yaml:"allow_stale,omitempty"`36RequireConsistent bool `yaml:"require_consistent,omitempty"`3738KVPrefix string `yaml:"kv_prefix,omitempty"`39KVFilter string `yaml:"kv_filter,omitempty"`40HealthSummary bool `yaml:"generate_health_summary,omitempty"`41}4243// UnmarshalYAML implements yaml.Unmarshaler for Config.44func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {45*c = DefaultConfig4647type plain Config48return unmarshal((*plain)(c))49}5051// Name returns the name of the integration.52func (c *Config) Name() string {53return "consul_exporter"54}5556// InstanceKey returns the hostname:port of the Consul server.57func (c *Config) InstanceKey(agentKey string) (string, error) {58u, err := url.Parse(c.Server)59if err != nil {60return "", fmt.Errorf("could not parse url: %w", err)61}62return u.Host, nil63}6465// NewIntegration converts the config into an instance of an integration.66func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {67return New(l, c)68}6970func init() {71integrations.RegisterIntegration(&Config{})72integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("consul"))73}7475// New creates a new consul_exporter integration. The integration scrapes76// metrics from a consul process.77func New(log log.Logger, c *Config) (integrations.Integration, error) {78var (79consulOpts = exporter.ConsulOpts{80CAFile: c.CAFile,81CertFile: c.CertFile,82Insecure: c.InsecureSkipVerify,83KeyFile: c.KeyFile,84RequestLimit: c.RequestLimit,85ServerName: c.ServerName,86Timeout: c.Timeout,87URI: c.Server,88}89queryOptions = consul_api.QueryOptions{90AllowStale: c.AllowStale,91RequireConsistent: c.RequireConsistent,92}93)9495e, err := exporter.New(consulOpts, queryOptions, c.KVPrefix, c.KVFilter, c.HealthSummary, log)96if err != nil {97return nil, err98}99100return integrations.NewCollectorIntegration(c.Name(), integrations.WithCollectors(e)), nil101}102103104