|
|
@@ -4,6 +4,7 @@ import (
|
|
4
|
4
|
"context"
|
|
5
|
5
|
"net"
|
|
6
|
6
|
"net/http"
|
|
|
7
|
+ "strconv"
|
|
7
|
8
|
|
|
8
|
9
|
"github.com/9seconds/mtg/v2/events"
|
|
9
|
10
|
"github.com/9seconds/mtg/v2/mtglib"
|
|
|
@@ -12,18 +13,23 @@ import (
|
|
12
|
13
|
)
|
|
13
|
14
|
|
|
14
|
15
|
type prometheusProcessor struct {
|
|
15
|
|
- streams map[string]*streamInfo
|
|
|
16
|
+ streams map[string]streamInfo
|
|
16
|
17
|
factory *PrometheusFactory
|
|
17
|
18
|
}
|
|
18
|
19
|
|
|
19
|
20
|
func (p prometheusProcessor) EventStart(evt mtglib.EventStart) {
|
|
20
|
21
|
info := acquireStreamInfo()
|
|
21
|
|
- info.SetStartTime(evt.CreatedAt)
|
|
22
|
|
- info.SetClientIP(evt.RemoteIP)
|
|
|
22
|
+
|
|
|
23
|
+ if evt.RemoteIP.To4() != nil {
|
|
|
24
|
+ info[TagIPFamily] = TagIPFamilyIPv4
|
|
|
25
|
+ } else {
|
|
|
26
|
+ info[TagIPFamily] = TagIPFamilyIPv6
|
|
|
27
|
+ }
|
|
|
28
|
+
|
|
23
|
29
|
p.streams[evt.StreamID()] = info
|
|
24
|
30
|
|
|
25
|
31
|
p.factory.metricClientConnections.
|
|
26
|
|
- WithLabelValues(info.V(TagIPFamily)).
|
|
|
32
|
+ WithLabelValues(info[TagIPFamily]).
|
|
27
|
33
|
Inc()
|
|
28
|
34
|
}
|
|
29
|
35
|
|
|
|
@@ -33,11 +39,11 @@ func (p prometheusProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
|
|
33
|
39
|
return
|
|
34
|
40
|
}
|
|
35
|
41
|
|
|
36
|
|
- info.SetTelegramIP(evt.RemoteIP)
|
|
37
|
|
- info.SetDC(evt.DC)
|
|
|
42
|
+ info[TagTelegramIP] = evt.RemoteIP.String()
|
|
|
43
|
+ info[TagDC] = strconv.Itoa(evt.DC)
|
|
38
|
44
|
|
|
39
|
45
|
p.factory.metricTelegramConnections.
|
|
40
|
|
- WithLabelValues(info.V(TagTelegramIP), info.V(TagDC)).
|
|
|
46
|
+ WithLabelValues(info[TagTelegramIP], info[TagDC]).
|
|
41
|
47
|
Inc()
|
|
42
|
48
|
}
|
|
43
|
49
|
|
|
|
@@ -48,7 +54,7 @@ func (p prometheusProcessor) EventTraffic(evt mtglib.EventTraffic) {
|
|
48
|
54
|
}
|
|
49
|
55
|
|
|
50
|
56
|
p.factory.metricTelegramTraffic.
|
|
51
|
|
- WithLabelValues(info.V(TagTelegramIP), info.V(TagDC), getDirection(evt.IsRead)).
|
|
|
57
|
+ WithLabelValues(info[TagTelegramIP], info[TagDC], getDirection(evt.IsRead)).
|
|
52
|
58
|
Add(float64(evt.Traffic))
|
|
53
|
59
|
}
|
|
54
|
60
|
|
|
|
@@ -64,12 +70,12 @@ func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
|
|
64
|
70
|
}()
|
|
65
|
71
|
|
|
66
|
72
|
p.factory.metricClientConnections.
|
|
67
|
|
- WithLabelValues(info.V(TagIPFamily)).
|
|
|
73
|
+ WithLabelValues(info[TagIPFamily]).
|
|
68
|
74
|
Dec()
|
|
69
|
75
|
|
|
70
|
|
- if info.V(TagTelegramIP) != "" {
|
|
|
76
|
+ if telegramIP, ok := info[TagTelegramIP]; ok {
|
|
71
|
77
|
p.factory.metricTelegramConnections.
|
|
72
|
|
- WithLabelValues(info.V(TagTelegramIP), info.V(TagDC)).
|
|
|
78
|
+ WithLabelValues(telegramIP, info[TagDC]).
|
|
73
|
79
|
Dec()
|
|
74
|
80
|
}
|
|
75
|
81
|
}
|
|
|
@@ -83,20 +89,20 @@ func (p prometheusProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
|
|
83
|
89
|
}
|
|
84
|
90
|
|
|
85
|
91
|
func (p prometheusProcessor) Shutdown() {
|
|
86
|
|
- p.streams = make(map[string]*streamInfo)
|
|
|
92
|
+ p.streams = make(map[string]streamInfo)
|
|
87
|
93
|
}
|
|
88
|
94
|
|
|
89
|
95
|
type PrometheusFactory struct {
|
|
90
|
96
|
httpServer *http.Server
|
|
91
|
97
|
|
|
92
|
|
- metricClientConnections *prometheus.GaugeVec
|
|
93
|
|
- metricTelegramConnections *prometheus.GaugeVec
|
|
94
|
|
- metricDomainDisguisingConnections *prometheus.GaugeVec
|
|
|
98
|
+ metricClientConnections *prometheus.GaugeVec
|
|
|
99
|
+ metricTelegramConnections *prometheus.GaugeVec
|
|
|
100
|
+ metricDomainFrontingConnections *prometheus.GaugeVec
|
|
95
|
101
|
|
|
96
|
|
- metricTelegramTraffic *prometheus.CounterVec
|
|
97
|
|
- metricDomainDisguisingTraffic *prometheus.CounterVec
|
|
|
102
|
+ metricTelegramTraffic *prometheus.CounterVec
|
|
|
103
|
+ metricDomainFrontingTraffic *prometheus.CounterVec
|
|
98
|
104
|
|
|
99
|
|
- metricDomainDisguising prometheus.Counter
|
|
|
105
|
+ metricDomainFronting prometheus.Counter
|
|
100
|
106
|
metricConcurrencyLimited prometheus.Counter
|
|
101
|
107
|
metricIPBlocklisted prometheus.Counter
|
|
102
|
108
|
metricReplayAttacks prometheus.Counter
|
|
|
@@ -104,7 +110,7 @@ type PrometheusFactory struct {
|
|
104
|
110
|
|
|
105
|
111
|
func (p *PrometheusFactory) Make() events.Observer {
|
|
106
|
112
|
return prometheusProcessor{
|
|
107
|
|
- streams: make(map[string]*streamInfo),
|
|
|
113
|
+ streams: make(map[string]streamInfo),
|
|
108
|
114
|
factory: p,
|
|
109
|
115
|
}
|
|
110
|
116
|
}
|
|
|
@@ -141,10 +147,10 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
|
|
141
|
147
|
Name: MetricTelegramConnections,
|
|
142
|
148
|
Help: "A number of connections to Telegram servers.",
|
|
143
|
149
|
}, []string{TagTelegramIP, TagDC}),
|
|
144
|
|
- metricDomainDisguisingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
150
|
+ metricDomainFrontingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
145
|
151
|
Namespace: metricPrefix,
|
|
146
|
|
- Name: MetricDomainDisguisingConnections,
|
|
147
|
|
- Help: "A number of connections which talk with disguising domain.",
|
|
|
152
|
+ Name: MetricDomainFronting,
|
|
|
153
|
+ Help: "A number of connections which talk with front domain.",
|
|
148
|
154
|
}, []string{TagIPFamily}),
|
|
149
|
155
|
|
|
150
|
156
|
metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
@@ -152,16 +158,16 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
|
|
152
|
158
|
Name: MetricTelegramTraffic,
|
|
153
|
159
|
Help: "Traffic which is generated talking with Telegram servers.",
|
|
154
|
160
|
}, []string{TagTelegramIP, TagDC, TagDirection}),
|
|
155
|
|
- metricDomainDisguisingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
161
|
+ metricDomainFrontingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
156
|
162
|
Namespace: metricPrefix,
|
|
157
|
|
- Name: MetricDomainDisguisingTraffic,
|
|
158
|
|
- Help: "Traffic which is generated talking with disguising domain.",
|
|
|
163
|
+ Name: MetricDomainFrontingTraffic,
|
|
|
164
|
+ Help: "Traffic which is generated talking with front domain.",
|
|
159
|
165
|
}, []string{TagDirection}),
|
|
160
|
166
|
|
|
161
|
|
- metricDomainDisguising: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
167
|
+ metricDomainFronting: prometheus.NewCounter(prometheus.CounterOpts{
|
|
162
|
168
|
Namespace: metricPrefix,
|
|
163
|
|
- Name: MetricDomainDisguising,
|
|
164
|
|
- Help: "A number of routings to disguising domain.",
|
|
|
169
|
+ Name: MetricDomainFronting,
|
|
|
170
|
+ Help: "A number of routings to front domain.",
|
|
165
|
171
|
}),
|
|
166
|
172
|
metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
|
|
167
|
173
|
Namespace: metricPrefix,
|
|
|
@@ -182,12 +188,12 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
|
|
182
|
188
|
|
|
183
|
189
|
registry.MustRegister(factory.metricClientConnections)
|
|
184
|
190
|
registry.MustRegister(factory.metricTelegramConnections)
|
|
185
|
|
- registry.MustRegister(factory.metricDomainDisguisingConnections)
|
|
|
191
|
+ registry.MustRegister(factory.metricDomainFrontingConnections)
|
|
186
|
192
|
|
|
187
|
193
|
registry.MustRegister(factory.metricTelegramTraffic)
|
|
188
|
|
- registry.MustRegister(factory.metricDomainDisguisingTraffic)
|
|
|
194
|
+ registry.MustRegister(factory.metricDomainFrontingTraffic)
|
|
189
|
195
|
|
|
190
|
|
- registry.MustRegister(factory.metricDomainDisguising)
|
|
|
196
|
+ registry.MustRegister(factory.metricDomainFronting)
|
|
191
|
197
|
registry.MustRegister(factory.metricConcurrencyLimited)
|
|
192
|
198
|
registry.MustRegister(factory.metricIPBlocklisted)
|
|
193
|
199
|
registry.MustRegister(factory.metricReplayAttacks)
|