-
Notifications
You must be signed in to change notification settings - Fork 202
/
metrics.go
113 lines (95 loc) · 2.88 KB
/
metrics.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
package metrics
import (
"os"
"time"
"github.com/Ehco1996/ehco/internal/config"
"github.com/prometheus/client_golang/prometheus"
)
const (
METRIC_NS = "ehco"
METRIC_SUBSYSTEM_TRAFFIC = "traffic"
METRIC_SUBSYSTEM_PING = "ping"
METRIC_LABEL_REMOTE = "remote"
METRIC_LABEL_CONN_FLOW = "flow"
METRIC_CONN_FLOW_WRITE = "write"
METRIC_CONN_FLOW_READ = "read"
METRIC_LABEL_CONN_TYPE = "type"
METRIC_CONN_TYPE_TCP = "tcp"
METRIC_CONN_TYPE_UDP = "udp"
EhcoAliveStateInit = 0
EhcoAliveStateRunning = 1
)
// ping metrics
var (
pingLabelNames = []string{"ip", "host", "label"}
pingBuckets = prometheus.ExponentialBuckets(0.001, 2, 12) // 1ms ~ 4s
pingInterval = time.Second * 30
PingResponseDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: METRIC_NS,
Subsystem: METRIC_SUBSYSTEM_PING,
Name: "response_duration_seconds",
Help: "A histogram of latencies for ping responses.",
Buckets: pingBuckets,
ConstLabels: ConstLabels,
},
pingLabelNames,
)
PingRequestTotal = prometheus.NewDesc(
prometheus.BuildFQName(METRIC_NS, METRIC_SUBSYSTEM_PING, "requests_total"),
"Number of ping requests sent",
pingLabelNames,
ConstLabels,
)
)
// traffic metrics
var (
Hostname, _ = os.Hostname()
ConstLabels = map[string]string{
"ehco_runner_hostname": Hostname,
}
EhcoAlive = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: METRIC_NS,
Subsystem: "",
Name: "alive_state",
Help: "ehco 存活状态",
ConstLabels: ConstLabels,
})
CurConnectionCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: METRIC_NS,
Subsystem: METRIC_SUBSYSTEM_TRAFFIC,
Name: "current_connection_count",
Help: "当前链接数",
ConstLabels: ConstLabels,
}, []string{METRIC_LABEL_REMOTE, METRIC_LABEL_CONN_TYPE})
NetWorkTransmitBytes = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: METRIC_NS,
Subsystem: METRIC_SUBSYSTEM_TRAFFIC,
Name: "network_transmit_bytes",
Help: "传输流量总量bytes",
ConstLabels: ConstLabels,
}, []string{METRIC_LABEL_REMOTE, METRIC_LABEL_CONN_TYPE, METRIC_LABEL_CONN_FLOW})
HandShakeDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Subsystem: METRIC_SUBSYSTEM_TRAFFIC,
Namespace: METRIC_NS,
Name: "handshake_duration",
Help: "握手时间ms",
ConstLabels: ConstLabels,
}, []string{METRIC_LABEL_REMOTE})
)
func RegisterEhcoMetrics(cfg *config.Config) error {
// traffic
prometheus.MustRegister(EhcoAlive)
prometheus.MustRegister(CurConnectionCount)
prometheus.MustRegister(NetWorkTransmitBytes)
prometheus.MustRegister(HandShakeDuration)
EhcoAlive.Set(EhcoAliveStateInit)
// ping
if cfg.EnablePing {
pg := NewPingGroup(cfg)
prometheus.MustRegister(PingResponseDurationSeconds)
prometheus.MustRegister(pg)
go pg.Run()
}
return nil
}