This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
117 lines (104 loc) · 3.02 KB
/
prometheus.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
105
106
107
108
109
110
111
112
113
114
115
116
117
package metrics
import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net"
"net/http"
"time"
prometheusmetrics "github.com/deathowl/go-metrics-prometheus"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/exoscale/go-reporter/config"
)
// PrometheusConfiguration is the configuration for exporting metrics to
// files.
type PrometheusConfiguration struct {
Listen config.Addr
Interval config.Duration
Namespace string
Subsystem string
CertFile config.FilePath
KeyFile config.FilePath
CacertFile config.FilePath
}
// UnmarshalYAML parses the configuration from YAML.
func (c *PrometheusConfiguration) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawPrometheusConfiguration PrometheusConfiguration
raw := rawPrometheusConfiguration{}
if err := unmarshal(&raw); err != nil {
return errors.Wrap(err, "unable to decode prometheus configuration")
}
if raw.Listen == "" {
return errors.Errorf("missing listen value")
}
if raw.Namespace == "" {
return errors.Errorf("missing namespace value")
}
if raw.Interval == config.Duration(0) {
return errors.Errorf("missing interval value for prometheus configuration")
}
if (raw.CertFile != "" || raw.KeyFile != "" || raw.CacertFile != "") &&
(raw.CertFile == "" || raw.KeyFile == "" || raw.CacertFile == "") {
return errors.Errorf("certfile, keyfile and cacertfile should be configured")
}
*c = PrometheusConfiguration(raw)
return nil
}
// initExporter initialize the Prometheus exporter
func (c *PrometheusConfiguration) initExporter(metrics *Metrics) error {
prometheusRegistry := prometheus.DefaultRegisterer
pClient := prometheusmetrics.NewPrometheusProvider(
metrics.Registry,
c.Namespace,
c.Subsystem,
prometheusRegistry,
time.Duration(c.Interval))
metrics.t.Go(func() error {
tick := time.NewTicker(time.Duration(c.Interval))
for {
select {
case <-tick.C:
pClient.UpdatePrometheusMetricsOnce() // nolint: errcheck
case <-metrics.t.Dying():
return nil
}
}
})
address := c.Listen
listener, err := net.Listen("tcp", address.String())
if err != nil {
return errors.Wrapf(err, "unable to listen to %v", address)
}
server := &http.Server{
Addr: address.String(),
Handler: promhttp.Handler(),
}
metrics.t.Go(func() error {
if c.CertFile == "" && c.KeyFile == "" && c.CacertFile == "" {
return server.Serve(listener)
}
cacert, err := ioutil.ReadFile(string(c.CacertFile))
if err != nil {
return err
}
certpool := x509.NewCertPool()
certpool.AppendCertsFromPEM(cacert)
tlsConfig := &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: certpool,
}
server.TLSConfig = tlsConfig
return server.ServeTLS(listener, string(c.CertFile), string(c.KeyFile))
})
// Handle stop correctly
metrics.t.Go(func() error {
<-metrics.t.Dying()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return server.Shutdown(ctx)
})
return nil
}