-
Notifications
You must be signed in to change notification settings - Fork 0
/
prom_tcp.go
71 lines (63 loc) · 1.64 KB
/
prom_tcp.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
package proxy
import (
"time"
"github.com/gernest/tt/zlg"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var totalTCPRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "tcp_requests_total",
Help: "Total number of tcp requests",
},
[]string{"acme", "fixed", "no_match", "protocol", "route", "server_name"},
)
func (m *ContextMeta) Complete() {
m.End = time.Now()
m.Log()
m.Emit()
}
// Emit based on m emits various metrics exposed by prometheus
func (m *ContextMeta) Emit() {
base := m.GetBaseLabels()
totalTCPRequests.With(base).Inc()
}
// Log write logs about the request
func (m *ContextMeta) Log() {
fields := []zapcore.Field{
zap.String("server_name", m.ServerName.String()),
zap.Bool("acme", m.ACME.Load()),
zap.Bool("fixed", m.ACME.Load()),
zap.String("protocol", Protocol(m.Protocol.Load()).String()),
zap.Duration("duration", m.End.Sub(m.Start)),
}
if m.NoMatch.Load() {
zlg.Info("FAILED", fields...)
return
}
zlg.Info("PASS", fields...)
}
func (m *ContextMeta) GetBaseLabels(lbs ...map[string]string) prometheus.Labels {
lbs = append(lbs, map[string]string{
"acme": m.ACME.String(),
"fixed": m.Fixed.String(),
"no_match": m.NoMatch.String(),
"protocol": Protocol(m.Protocol.Load()).String(),
"route": m.RouteName.String(),
"server_name": m.ServerName.String(),
})
return m.GetLabels(lbs...)
}
func (m *ContextMeta) GetLabels(lbs ...map[string]string) prometheus.Labels {
x := make(map[string]string)
for k, v := range m.Labels {
x[k] = v
}
for _, lb := range lbs {
for k, v := range lb {
x[k] = v
}
}
return x
}