-
Notifications
You must be signed in to change notification settings - Fork 486
/
squid_exporter.go
104 lines (85 loc) · 2.51 KB
/
squid_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
103
104
package squid_exporter
import (
"errors"
"fmt"
"net"
"strconv"
se "github.com/boynux/squid-exporter/collector"
"github.com/go-kit/log"
"github.com/grafana/agent/pkg/integrations"
config_util "github.com/prometheus/common/config"
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
)
var (
ErrNoAddress = errors.New("no address was provided")
ErrNoHostname = errors.New("no hostname in provided address")
ErrNoPort = errors.New("no port in provided address")
)
// Config is the configuration for the squid integration
type Config struct {
Address string `yaml:"address"`
Username string `yaml:"username"`
Password config_util.Secret `yaml:"password"`
Host string
Port int
}
func (c *Config) validate() error {
if c.Address == "" {
return ErrNoAddress
}
host, port, err := net.SplitHostPort(c.Address)
if err != nil {
return err
}
if host == "" {
return ErrNoHostname
}
c.Host = host
if port == "" {
return ErrNoPort
}
if sp, err := strconv.Atoi(port); err != nil {
return err
} else {
c.Port = sp
return nil
}
}
// UnmarshalYAML implements yaml.Unmarshaler for Config
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = Config{}
type plain Config
return unmarshal((*plain)(c))
}
// Name returns the integration name this config is associated with.
func (c *Config) Name() string {
return "squid"
}
// InstanceKey returns the addr of the squid instance.
func (c *Config) InstanceKey(agentKey string) (string, error) {
return c.Address, nil
}
// NewIntegration returns the Squid Exporter Integration
func (c *Config) NewIntegration(log log.Logger) (integrations.Integration, error) {
return New(c)
}
func init() {
integrations.RegisterIntegration(&Config{})
integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("squid"))
}
// New creates a new squid integration. The integration scrapes metrics
// from an Squid exporter running with the https://github.com/boynux/squid-exporter
func New(c *Config) (integrations.Integration, error) {
se.ExtractServiceTimes = true
if err := c.validate(); err != nil {
return nil, fmt.Errorf("failed to validate config: %w", err)
}
seExporter := se.New(&se.CollectorConfig{
Hostname: c.Host,
Port: c.Port,
Login: c.Username,
Password: string(c.Password),
})
return integrations.NewCollectorIntegration(c.Name(), integrations.WithCollectors(seExporter)), nil
}