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