Преглед изворни кода

Rename metrics

tags/v2.0.0-rc1
9seconds пре 5 година
родитељ
комит
36d695118e
5 измењених фајлова са 75 додато и 100 уклоњено
  1. 6
    6
      stats/init.go
  2. 4
    6
      stats/pools.go
  3. 37
    31
      stats/prometheus.go
  4. 21
    15
      stats/statsd.go
  5. 7
    42
      stats/stream_info.go

+ 6
- 6
stats/init.go Прегледај датотеку

@@ -6,14 +6,14 @@ const (
6 6
 	DefaultStatsdMetricPrefix = DefaultMetricPrefix + "."
7 7
 	DefaultStatsdTagFormat    = "datadog"
8 8
 
9
-	MetricClientConnections           = "client_connections"
10
-	MetricTelegramConnections         = "telegram_connections"
11
-	MetricDomainDisguisingConnections = "domain_disguising_connections"
9
+	MetricClientConnections         = "client_connections"
10
+	MetricTelegramConnections       = "telegram_connections"
11
+	MetricDomainFrontingConnections = "domain_fronting_connections"
12 12
 
13
-	MetricTelegramTraffic         = "telegram_traffic"
14
-	MetricDomainDisguisingTraffic = "domain_disguising_traffic"
13
+	MetricTelegramTraffic       = "telegram_traffic"
14
+	MetricDomainFrontingTraffic = "domain_fronting_traffic"
15 15
 
16
-	MetricDomainDisguising   = "domain_disguising"
16
+	MetricDomainFronting     = "domain_fronting"
17 17
 	MetricConcurrencyLimited = "concurrency_limited"
18 18
 	MetricIPBlocklisted      = "ip_blocklisted"
19 19
 	MetricReplayAttacks      = "replay_attacks"

+ 4
- 6
stats/pools.go Прегледај датотеку

@@ -4,17 +4,15 @@ import "sync"
4 4
 
5 5
 var streamInfoPool = sync.Pool{
6 6
 	New: func() interface{} {
7
-		return &streamInfo{
8
-			tags: map[string]string{},
9
-		}
7
+		return streamInfo{}
10 8
 	},
11 9
 }
12 10
 
13
-func acquireStreamInfo() *streamInfo {
14
-	return streamInfoPool.Get().(*streamInfo)
11
+func acquireStreamInfo() streamInfo {
12
+	return streamInfoPool.Get().(streamInfo)
15 13
 }
16 14
 
17
-func releaseStreamInfo(info *streamInfo) {
15
+func releaseStreamInfo(info streamInfo) {
18 16
 	info.Reset()
19 17
 	streamInfoPool.Put(info)
20 18
 }

+ 37
- 31
stats/prometheus.go Прегледај датотеку

@@ -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)

+ 21
- 15
stats/statsd.go Прегледај датотеку

@@ -2,6 +2,7 @@ package stats
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"strconv"
5 6
 	"strings"
6 7
 	"time"
7 8
 
@@ -12,19 +13,24 @@ import (
12 13
 )
13 14
 
14 15
 type statsdProcessor struct {
15
-	streams map[string]*streamInfo
16
+	streams map[string]streamInfo
16 17
 	client  *statsd.Client
17 18
 }
18 19
 
19 20
 func (s statsdProcessor) 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
 	s.streams[evt.StreamID()] = info
24 30
 
25 31
 	s.client.GaugeDelta(MetricClientConnections,
26 32
 		1,
27
-		info.TV(TagIPFamily))
33
+		info.T(TagIPFamily))
28 34
 }
29 35
 
30 36
 func (s statsdProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
@@ -33,13 +39,13 @@ func (s statsdProcessor) 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
 	s.client.GaugeDelta(MetricTelegramConnections,
40 46
 		1,
41
-		info.TV(TagTelegramIP),
42
-		info.TV(TagDC))
47
+		info.T(TagTelegramIP),
48
+		info.T(TagDC))
43 49
 }
44 50
 
45 51
 func (s statsdProcessor) EventTraffic(evt mtglib.EventTraffic) {
@@ -50,8 +56,8 @@ func (s statsdProcessor) EventTraffic(evt mtglib.EventTraffic) {
50 56
 
51 57
 	s.client.Incr(MetricTelegramTraffic,
52 58
 		int64(evt.Traffic),
53
-		info.TV(TagTelegramIP),
54
-		info.TV(TagDC),
59
+		info.T(TagTelegramIP),
60
+		info.T(TagDC),
55 61
 		statsd.StringTag(TagDirection, getDirection(evt.IsRead)))
56 62
 }
57 63
 
@@ -68,13 +74,13 @@ func (s statsdProcessor) EventFinish(evt mtglib.EventFinish) {
68 74
 
69 75
 	s.client.GaugeDelta(MetricClientConnections,
70 76
 		-1,
71
-		info.TV(TagIPFamily))
77
+		info.T(TagIPFamily))
72 78
 
73
-	if info.V(TagTelegramIP) != "" {
79
+	if _, ok := info[TagTelegramIP]; ok {
74 80
 		s.client.GaugeDelta(MetricTelegramConnections,
75 81
 			-1,
76
-			info.TV(TagTelegramIP),
77
-			info.TV(TagDC))
82
+			info.T(TagTelegramIP),
83
+			info.T(TagDC))
78 84
 	}
79 85
 }
80 86
 
@@ -113,7 +119,7 @@ func (s StatsdFactory) Close() error {
113 119
 func (s StatsdFactory) Make() events.Observer {
114 120
 	return statsdProcessor{
115 121
 		client:  s.client,
116
-		streams: make(map[string]*streamInfo),
122
+		streams: make(map[string]streamInfo),
117 123
 	}
118 124
 }
119 125
 

+ 7
- 42
stats/stream_info.go Прегледај датотеку

@@ -1,51 +1,16 @@
1 1
 package stats
2 2
 
3
-import (
4
-	"net"
5
-	"strconv"
6
-	"time"
3
+import statsd "github.com/smira/go-statsd"
7 4
 
8
-	statsd "github.com/smira/go-statsd"
9
-)
5
+type streamInfo map[string]string
10 6
 
11
-type streamInfo struct {
12
-	startTime time.Time
13
-	tags      map[string]string
7
+func (s streamInfo) T(key string) statsd.Tag {
8
+	return statsd.StringTag(key, s[key])
14 9
 }
15 10
 
16
-func (s *streamInfo) SetStartTime(tme time.Time) {
17
-	s.startTime = tme
18
-}
19
-
20
-func (s *streamInfo) SetClientIP(ip net.IP) {
21
-	if ip.To4() != nil {
22
-		s.tags[TagIPFamily] = TagIPFamilyIPv4
23
-	} else {
24
-		s.tags[TagIPFamily] = TagIPFamilyIPv6
25
-	}
26
-}
27
-
28
-func (s *streamInfo) SetTelegramIP(ip net.IP) {
29
-	s.tags[TagTelegramIP] = ip.String()
30
-}
31
-
32
-func (s *streamInfo) SetDC(dc int) {
33
-	s.tags[TagDC] = strconv.Itoa(dc)
34
-}
35
-
36
-func (s *streamInfo) V(key string) string {
37
-	return s.tags[key]
38
-}
39
-
40
-func (s *streamInfo) TV(key string) statsd.Tag {
41
-	return statsd.StringTag(key, s.tags[key])
42
-}
43
-
44
-func (s *streamInfo) Reset() {
45
-	s.startTime = time.Time{}
46
-
47
-	for k := range s.tags {
48
-		delete(s.tags, k)
11
+func (s streamInfo) Reset() {
12
+	for k := range s {
13
+		delete(s, k)
49 14
 	}
50 15
 }
51 16
 

Loading…
Откажи
Сачувај