/
node.go
77 lines (61 loc) · 1.6 KB
/
node.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
package prometheus
import (
"net/http"
"context"
tmtLog "github.com/tendermint/tendermint/libs/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/lightstreams-network/lightchain/prometheus/collectors"
lcLog "github.com/lightstreams-network/lightchain/log"
)
type Node struct {
cfg Config
httpSrv *http.Server
registry *prometheus.Registry
logger tmtLog.Logger
}
func NewNode(cfg Config) *Node {
logger := lcLog.NewLogger().With("service", "prometheus")
registry := prometheus.NewPedanticRegistry()
return &Node{
cfg: cfg,
httpSrv: nil,
logger: logger,
registry: registry,
}
}
func (n *Node) Registry() *prometheus.Registry {
return n.registry
}
func (n *Node) Start() error {
if !n.cfg.enabled {
return nil
}
n.logger.Info("Starting prometheus node...")
n.httpSrv = &http.Server{
Addr: n.cfg.http.Addr,
Handler: promhttp.HandlerFor(
n.registry,
promhttp.HandlerOpts{MaxRequestsInFlight: 3},
),
ReadTimeout: n.cfg.http.ReadTimeout,
WriteTimeout: n.cfg.http.WriteTimeout,
}
collectors.NewCollectors(n.registry, n.cfg.ethDialUrl)
n.logger.Info("Prometheus endpoint opened", "addr", n.cfg.http.Addr)
if err := n.httpSrv.ListenAndServe(); err != nil {
return err
}
return nil
}
func (n *Node) Stop() error {
if !n.cfg.enabled {
return nil
}
n.logger.Info("Stopping prometheus node...")
if err := n.httpSrv.Shutdown(context.Background()); err != nil {
return err
}
n.logger.Info("Prometheus node stopped")
return nil
}