forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
38 lines (31 loc) · 1.06 KB
/
server.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
package metrics
import (
"context"
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
// PrometheusConfig defines a config for a metrics server
type PrometheusConfig struct {
Enabled bool `json:"enabled,omitempty"`
Path string `json:"path,omitempty"`
Port string `json:"port,omitempty"`
}
// RunServer starts a metrics server
func RunServer(ctx context.Context, config PrometheusConfig, registry *prometheus.Registry) {
mux := http.NewServeMux()
mux.Handle(config.Path, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
srv := &http.Server{Addr: fmt.Sprintf(":%s", config.Port), Handler: mux}
defer func() {
if cerr := srv.Close(); cerr != nil {
log.Fatalf("Encountered an '%s' error when tried to close the metrics server running on '%s'", cerr, config.Port)
}
}()
log.Infof("Starting prometheus metrics server at 0.0.0.0:%s%s", config.Port, config.Path)
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
<-ctx.Done()
}