File size: 1,195 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package app

import (
	"net"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/spf13/viper"
)

func initPromServer() error {
	addr := viper.GetString("listen-prometheus")
	if addr == "" {
		return nil
	}

	l, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	mux := http.NewServeMux()

	http.DefaultTransport = promhttp.InstrumentRoundTripperDuration(promauto.NewHistogramVec(prometheus.HistogramOpts{
		Namespace: "goalert",
		Subsystem: "http_client",
		Name:      "requests_duration_seconds",
		Help:      "Duration of outgoing HTTP requests in seconds.",
	}, []string{"code", "method"}), http.DefaultTransport)
	http.DefaultTransport = promhttp.InstrumentRoundTripperInFlight(promauto.NewGauge(prometheus.GaugeOpts{
		Namespace: "goalert",
		Subsystem: "http_client",
		Name:      "requests_in_flight",
		Help:      "Number of outgoing HTTP requests currently active.",
	}), http.DefaultTransport)

	mux.Handle("/metrics", promhttp.Handler())
	srv := http.Server{
		Handler: mux,
	}
	go func() { _ = srv.Serve(l) }()
	return nil
}