Просмотр исходного кода

Rename metrics

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
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
 	DefaultStatsdMetricPrefix = DefaultMetricPrefix + "."
6
 	DefaultStatsdMetricPrefix = DefaultMetricPrefix + "."
7
 	DefaultStatsdTagFormat    = "datadog"
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
 	MetricConcurrencyLimited = "concurrency_limited"
17
 	MetricConcurrencyLimited = "concurrency_limited"
18
 	MetricIPBlocklisted      = "ip_blocklisted"
18
 	MetricIPBlocklisted      = "ip_blocklisted"
19
 	MetricReplayAttacks      = "replay_attacks"
19
 	MetricReplayAttacks      = "replay_attacks"

+ 4
- 6
stats/pools.go Просмотреть файл

4
 
4
 
5
 var streamInfoPool = sync.Pool{
5
 var streamInfoPool = sync.Pool{
6
 	New: func() interface{} {
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
 	info.Reset()
16
 	info.Reset()
19
 	streamInfoPool.Put(info)
17
 	streamInfoPool.Put(info)
20
 }
18
 }

+ 37
- 31
stats/prometheus.go Просмотреть файл

4
 	"context"
4
 	"context"
5
 	"net"
5
 	"net"
6
 	"net/http"
6
 	"net/http"
7
+	"strconv"
7
 
8
 
8
 	"github.com/9seconds/mtg/v2/events"
9
 	"github.com/9seconds/mtg/v2/events"
9
 	"github.com/9seconds/mtg/v2/mtglib"
10
 	"github.com/9seconds/mtg/v2/mtglib"
12
 )
13
 )
13
 
14
 
14
 type prometheusProcessor struct {
15
 type prometheusProcessor struct {
15
-	streams map[string]*streamInfo
16
+	streams map[string]streamInfo
16
 	factory *PrometheusFactory
17
 	factory *PrometheusFactory
17
 }
18
 }
18
 
19
 
19
 func (p prometheusProcessor) EventStart(evt mtglib.EventStart) {
20
 func (p prometheusProcessor) EventStart(evt mtglib.EventStart) {
20
 	info := acquireStreamInfo()
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
 	p.streams[evt.StreamID()] = info
29
 	p.streams[evt.StreamID()] = info
24
 
30
 
25
 	p.factory.metricClientConnections.
31
 	p.factory.metricClientConnections.
26
-		WithLabelValues(info.V(TagIPFamily)).
32
+		WithLabelValues(info[TagIPFamily]).
27
 		Inc()
33
 		Inc()
28
 }
34
 }
29
 
35
 
33
 		return
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
 	p.factory.metricTelegramConnections.
45
 	p.factory.metricTelegramConnections.
40
-		WithLabelValues(info.V(TagTelegramIP), info.V(TagDC)).
46
+		WithLabelValues(info[TagTelegramIP], info[TagDC]).
41
 		Inc()
47
 		Inc()
42
 }
48
 }
43
 
49
 
48
 	}
54
 	}
49
 
55
 
50
 	p.factory.metricTelegramTraffic.
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
 		Add(float64(evt.Traffic))
58
 		Add(float64(evt.Traffic))
53
 }
59
 }
54
 
60
 
64
 	}()
70
 	}()
65
 
71
 
66
 	p.factory.metricClientConnections.
72
 	p.factory.metricClientConnections.
67
-		WithLabelValues(info.V(TagIPFamily)).
73
+		WithLabelValues(info[TagIPFamily]).
68
 		Dec()
74
 		Dec()
69
 
75
 
70
-	if info.V(TagTelegramIP) != "" {
76
+	if telegramIP, ok := info[TagTelegramIP]; ok {
71
 		p.factory.metricTelegramConnections.
77
 		p.factory.metricTelegramConnections.
72
-			WithLabelValues(info.V(TagTelegramIP), info.V(TagDC)).
78
+			WithLabelValues(telegramIP, info[TagDC]).
73
 			Dec()
79
 			Dec()
74
 	}
80
 	}
75
 }
81
 }
83
 }
89
 }
84
 
90
 
85
 func (p prometheusProcessor) Shutdown() {
91
 func (p prometheusProcessor) Shutdown() {
86
-	p.streams = make(map[string]*streamInfo)
92
+	p.streams = make(map[string]streamInfo)
87
 }
93
 }
88
 
94
 
89
 type PrometheusFactory struct {
95
 type PrometheusFactory struct {
90
 	httpServer *http.Server
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
 	metricConcurrencyLimited prometheus.Counter
106
 	metricConcurrencyLimited prometheus.Counter
101
 	metricIPBlocklisted      prometheus.Counter
107
 	metricIPBlocklisted      prometheus.Counter
102
 	metricReplayAttacks      prometheus.Counter
108
 	metricReplayAttacks      prometheus.Counter
104
 
110
 
105
 func (p *PrometheusFactory) Make() events.Observer {
111
 func (p *PrometheusFactory) Make() events.Observer {
106
 	return prometheusProcessor{
112
 	return prometheusProcessor{
107
-		streams: make(map[string]*streamInfo),
113
+		streams: make(map[string]streamInfo),
108
 		factory: p,
114
 		factory: p,
109
 	}
115
 	}
110
 }
116
 }
141
 			Name:      MetricTelegramConnections,
147
 			Name:      MetricTelegramConnections,
142
 			Help:      "A number of connections to Telegram servers.",
148
 			Help:      "A number of connections to Telegram servers.",
143
 		}, []string{TagTelegramIP, TagDC}),
149
 		}, []string{TagTelegramIP, TagDC}),
144
-		metricDomainDisguisingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
150
+		metricDomainFrontingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
145
 			Namespace: metricPrefix,
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
 		}, []string{TagIPFamily}),
154
 		}, []string{TagIPFamily}),
149
 
155
 
150
 		metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
156
 		metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
152
 			Name:      MetricTelegramTraffic,
158
 			Name:      MetricTelegramTraffic,
153
 			Help:      "Traffic which is generated talking with Telegram servers.",
159
 			Help:      "Traffic which is generated talking with Telegram servers.",
154
 		}, []string{TagTelegramIP, TagDC, TagDirection}),
160
 		}, []string{TagTelegramIP, TagDC, TagDirection}),
155
-		metricDomainDisguisingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
161
+		metricDomainFrontingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
156
 			Namespace: metricPrefix,
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
 		}, []string{TagDirection}),
165
 		}, []string{TagDirection}),
160
 
166
 
161
-		metricDomainDisguising: prometheus.NewCounter(prometheus.CounterOpts{
167
+		metricDomainFronting: prometheus.NewCounter(prometheus.CounterOpts{
162
 			Namespace: metricPrefix,
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
 		metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
172
 		metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
167
 			Namespace: metricPrefix,
173
 			Namespace: metricPrefix,
182
 
188
 
183
 	registry.MustRegister(factory.metricClientConnections)
189
 	registry.MustRegister(factory.metricClientConnections)
184
 	registry.MustRegister(factory.metricTelegramConnections)
190
 	registry.MustRegister(factory.metricTelegramConnections)
185
-	registry.MustRegister(factory.metricDomainDisguisingConnections)
191
+	registry.MustRegister(factory.metricDomainFrontingConnections)
186
 
192
 
187
 	registry.MustRegister(factory.metricTelegramTraffic)
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
 	registry.MustRegister(factory.metricConcurrencyLimited)
197
 	registry.MustRegister(factory.metricConcurrencyLimited)
192
 	registry.MustRegister(factory.metricIPBlocklisted)
198
 	registry.MustRegister(factory.metricIPBlocklisted)
193
 	registry.MustRegister(factory.metricReplayAttacks)
199
 	registry.MustRegister(factory.metricReplayAttacks)

+ 21
- 15
stats/statsd.go Просмотреть файл

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

Загрузка…
Отмена
Сохранить