forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitoring_on.go
53 lines (43 loc) · 1.5 KB
/
monitoring_on.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
//go:build monitoring
// +build monitoring
package monitoring
import (
"net/http"
"sync"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)
var started sync.Once
// GetPromInterceptors returns the set of interceptors for Prometheus
// monitoring.
func GetPromInterceptors() ([]grpc.UnaryServerInterceptor, []grpc.StreamServerInterceptor) {
unaryInterceptors := []grpc.UnaryServerInterceptor{
grpc_prometheus.UnaryServerInterceptor,
}
streamInterceptors := []grpc.StreamServerInterceptor{
grpc_prometheus.StreamServerInterceptor,
}
return unaryInterceptors, streamInterceptors
}
// ExportPrometheusMetrics sets server options, registers gRPC metrics and
// launches the Prometheus exporter on the specified address.
func ExportPrometheusMetrics(grpcServer *grpc.Server, cfg lncfg.Prometheus) error {
started.Do(func() {
log.Infof("Prometheus exporter started on %v/metrics", cfg.Listen)
grpc_prometheus.Register(grpcServer)
// Enable the histograms which can allow plotting latency
// distributions of inbound calls. However we guard this behind
// another flag as this can generate a lot of additional data,
// as its a high cardinality metric typically.
if cfg.PerfHistograms {
grpc_prometheus.EnableHandlingTimeHistogram()
}
http.Handle("/metrics", promhttp.Handler())
go func() {
http.ListenAndServe(cfg.Listen, nil)
}()
})
return nil
}