Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/consul_exporter/consul_exporter.go
5393 views
1
// Package consul_exporter embeds https://github.com/prometheus/consul_exporter
2
package consul_exporter //nolint:golint
3
4
import (
5
"fmt"
6
"net/url"
7
"time"
8
9
"github.com/go-kit/log"
10
"github.com/grafana/agent/pkg/integrations"
11
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
12
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
13
consul_api "github.com/hashicorp/consul/api"
14
"github.com/prometheus/consul_exporter/pkg/exporter"
15
)
16
17
// DefaultConfig holds the default settings for the consul_exporter integration.
18
var DefaultConfig = Config{
19
Server: "http://localhost:8500",
20
Timeout: 500 * time.Millisecond,
21
AllowStale: true,
22
KVFilter: ".*",
23
HealthSummary: true,
24
}
25
26
// Config controls the consul_exporter integration.
27
type Config struct {
28
Server string `yaml:"server,omitempty"`
29
CAFile string `yaml:"ca_file,omitempty"`
30
CertFile string `yaml:"cert_file,omitempty"`
31
KeyFile string `yaml:"key_file,omitempty"`
32
ServerName string `yaml:"server_name,omitempty"`
33
Timeout time.Duration `yaml:"timeout,omitempty"`
34
InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`
35
RequestLimit int `yaml:"concurrent_request_limit,omitempty"`
36
AllowStale bool `yaml:"allow_stale,omitempty"`
37
RequireConsistent bool `yaml:"require_consistent,omitempty"`
38
39
KVPrefix string `yaml:"kv_prefix,omitempty"`
40
KVFilter string `yaml:"kv_filter,omitempty"`
41
HealthSummary bool `yaml:"generate_health_summary,omitempty"`
42
}
43
44
// UnmarshalYAML implements yaml.Unmarshaler for Config.
45
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
46
*c = DefaultConfig
47
48
type plain Config
49
return unmarshal((*plain)(c))
50
}
51
52
// Name returns the name of the integration.
53
func (c *Config) Name() string {
54
return "consul_exporter"
55
}
56
57
// InstanceKey returns the hostname:port of the Consul server.
58
func (c *Config) InstanceKey(agentKey string) (string, error) {
59
u, err := url.Parse(c.Server)
60
if err != nil {
61
return "", fmt.Errorf("could not parse url: %w", err)
62
}
63
return u.Host, nil
64
}
65
66
// NewIntegration converts the config into an instance of an integration.
67
func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {
68
return New(l, c)
69
}
70
71
func init() {
72
integrations.RegisterIntegration(&Config{})
73
integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("consul"))
74
}
75
76
// New creates a new consul_exporter integration. The integration scrapes
77
// metrics from a consul process.
78
func New(log log.Logger, c *Config) (integrations.Integration, error) {
79
var (
80
consulOpts = exporter.ConsulOpts{
81
CAFile: c.CAFile,
82
CertFile: c.CertFile,
83
Insecure: c.InsecureSkipVerify,
84
KeyFile: c.KeyFile,
85
RequestLimit: c.RequestLimit,
86
ServerName: c.ServerName,
87
Timeout: c.Timeout,
88
URI: c.Server,
89
}
90
queryOptions = consul_api.QueryOptions{
91
AllowStale: c.AllowStale,
92
RequireConsistent: c.RequireConsistent,
93
}
94
)
95
96
e, err := exporter.New(consulOpts, queryOptions, c.KVPrefix, c.KVFilter, c.HealthSummary, log)
97
if err != nil {
98
return nil, err
99
}
100
101
return integrations.NewCollectorIntegration(c.Name(), integrations.WithCollectors(e)), nil
102
}
103
104