-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexporterkit.go
120 lines (98 loc) · 2.52 KB
/
exporterkit.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
118
119
120
package exporterkit
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/giantswarm/microendpoint/endpoint/healthz"
"github.com/giantswarm/microerror"
"github.com/giantswarm/microkit/server"
"github.com/giantswarm/micrologger"
"github.com/prometheus/client_golang/prometheus"
)
const (
// DefaultAddress is the address the Exporter will run on if no address is configured.
DefaultAddress = "http://0.0.0.0:8000"
)
// Config if the configuration to create an Exporter.
type Config struct {
Address string
Collectors []prometheus.Collector
ExtraEndpoints []server.Endpoint
Logger micrologger.Logger
}
// Exporter runs a slice of Prometheus Collectors.
type Exporter struct {
address string
collectors []prometheus.Collector
extraEndpoints []server.Endpoint
logger micrologger.Logger
}
// New creates a new Exporter, given a Config.
func New(config Config) (*Exporter, error) {
if config.Collectors == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Collectors must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.Address == "" {
config.Address = DefaultAddress
}
for _, e := range config.ExtraEndpoints {
if e.Path() == healthz.Path {
return nil, microerror.Maskf(invalidConfigError, "%T.ExtraEndpoints: endpoints with path %q can not be added", config, healthz.Path)
}
}
exporter := Exporter{
address: config.Address,
collectors: config.Collectors,
extraEndpoints: config.ExtraEndpoints,
logger: config.Logger,
}
return &exporter, nil
}
// Run starts the Exporter.
func (e *Exporter) Run() {
var err error
var healthzEndpoint *healthz.Endpoint
{
c := healthz.Config{
Logger: e.logger,
}
healthzEndpoint, err = healthz.New(c)
if err != nil {
panic(fmt.Sprintf("%#v\n", err))
}
}
var newServer server.Server
{
c := server.Config{
EnableDebugServer: true,
Endpoints: append(e.extraEndpoints, healthzEndpoint),
ListenAddress: e.address,
Logger: e.logger,
}
newServer, err = server.New(c)
if err != nil {
panic(fmt.Sprintf("%#v\n", err))
}
}
prometheus.MustRegister(e.collectors...)
go newServer.Boot()
listener := make(chan os.Signal, 2)
signal.Notify(listener, os.Interrupt, syscall.SIGTERM)
<-listener
go func() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
newServer.Shutdown()
}()
os.Exit(0)
}()
<-listener
os.Exit(0)
}