Przeglądaj źródła

Add docs for stats

tags/v2.0.0-rc1
9seconds 5 lat temu
rodzic
commit
b748603096
4 zmienionych plików z 108 dodań i 8 usunięć
  1. 82
    7
      stats/init.go
  2. 12
    0
      stats/prometheus.go
  3. 13
    0
      stats/statsd.go
  4. 1
    1
      timeattack/init.go

+ 82
- 7
stats/init.go Wyświetl plik

1
+// Stats package has implementations of events.Observers for different
2
+// monitoring systems.
3
+//
4
+// Observer is a consumer of events produced by mtg. Consumers, defined
5
+// in this package, process these events and provide information used by
6
+// different monitoring system or time series databases.
1
 package stats
7
 package stats
2
 
8
 
3
 const (
9
 const (
10
+	// DefaultMetricPrefix defines a base prefix for all metrics.
4
 	DefaultMetricPrefix = "mtg"
11
 	DefaultMetricPrefix = "mtg"
5
 
12
 
13
+	// DefaultStatsdMetricPrefix defines a base prefix for metrics
14
+	// which are passed to statsd.
6
 	DefaultStatsdMetricPrefix = DefaultMetricPrefix + "."
15
 	DefaultStatsdMetricPrefix = DefaultMetricPrefix + "."
7
-	DefaultStatsdTagFormat    = "datadog"
8
 
16
 
9
-	MetricClientConnections         = "client_connections"
10
-	MetricTelegramConnections       = "telegram_connections"
17
+	// DefaultStatsdTagFormat defines a format of tags for statsd
18
+	// observer.
19
+	DefaultStatsdTagFormat = "datadog"
20
+
21
+	// MetricClientConnections defines a metric which is responsible for a
22
+	// number of currently active connections established by client.
23
+	//
24
+	//     Type: gauge
25
+	//     Tags:
26
+	//       ip_family | A type of ip (ipv4 or ipv6) of the client.
27
+	MetricClientConnections = "client_connections"
28
+
29
+	// MetricTelegramConnections defines a metric which is responsible for
30
+	// a count of active connections to Telegram servers.
31
+	//
32
+	//     Type: gauge
33
+	//     Tags:
34
+	//       telegram_ip | IP address of the telegram server.
35
+	//       dc          | Index of the datacenter to connect to.
36
+	MetricTelegramConnections = "telegram_connections"
37
+
38
+	// MetricDomainFrontingConnections defines a metric which is
39
+	// responsible for a count of active connections to a fronting domain.
40
+	// Fronting domain is that one that is encoded in a secret.
41
+	//
42
+	//     Type: gauge
43
+	//     Tags:
44
+	//       ip_family | A type of IP (ipv4 or ipv6) that was used.
11
 	MetricDomainFrontingConnections = "domain_fronting_connections"
45
 	MetricDomainFrontingConnections = "domain_fronting_connections"
12
 
46
 
13
-	MetricTelegramTraffic       = "telegram_traffic"
47
+	// MetricTelegramTraffic defines a metric for traffic (in bytes) that
48
+	// is sent to and from Telegram servers.
49
+	//
50
+	//     Type: counter
51
+	//     Tags:
52
+	//       telegram_ip | IP address of the telegram server.
53
+	//       dc          | Index of the datacenter
54
+	//       direction   | Direction of the traffc flow. Values are
55
+	//                   | 'to_client' and 'from_client'
56
+	MetricTelegramTraffic = "telegram_traffic"
57
+
58
+	// MetricDomainFrontingTraffic defines a metric for traffic (in bytes)
59
+	// that is sent to and from fronting domain.
60
+	//
61
+	//     Type: counter
62
+	//     Tags:
63
+	//       direction   | Direction of the traffc flow. Values are
64
+	//                   | 'to_client' and 'from_client'
14
 	MetricDomainFrontingTraffic = "domain_fronting_traffic"
65
 	MetricDomainFrontingTraffic = "domain_fronting_traffic"
15
 
66
 
16
-	MetricDomainFronting     = "domain_fronting"
67
+	// MetricDomainFronting defines a metric for a number of domain
68
+	// fronting routing events.
69
+	//
70
+	//     Type: counter
71
+	MetricDomainFronting = "domain_fronting"
72
+
73
+	// MetricConcurrencyLimited defines a metric for a count of events,
74
+	// when the client was blocked due to the concurrency limit.
75
+	//
76
+	//     Type: counter
17
 	MetricConcurrencyLimited = "concurrency_limited"
77
 	MetricConcurrencyLimited = "concurrency_limited"
18
-	MetricIPBlocklisted      = "ip_blocklisted"
19
-	MetricReplayAttacks      = "replay_attacks"
20
 
78
 
79
+	// MetricIPBlocklisted defines a metric for a count of events, when
80
+	// client was blocked because her IP address was found in blocklists.
81
+	//
82
+	//     Type: counter
83
+	MetricIPBlocklisted = "ip_blocklisted"
84
+
85
+	// MetricReplayAttacks defines a metric for a count of events, when
86
+	// mtg has detected a replay attack. Just a reminder: mtg immediately
87
+	// routes a connection to a fronting domain if such event is detected.
88
+	//
89
+	//     Type: counter
90
+	MetricReplayAttacks = "replay_attacks"
91
+
92
+	// TagIPFamily defines a name of the 'ip_family' tag and all values.
21
 	TagIPFamily     = "ip_family"
93
 	TagIPFamily     = "ip_family"
22
 	TagIPFamilyIPv4 = "ipv4"
94
 	TagIPFamilyIPv4 = "ipv4"
23
 	TagIPFamilyIPv6 = "ipv6"
95
 	TagIPFamilyIPv6 = "ipv6"
24
 
96
 
97
+	// TagTelegramIP defines a name of the 'telegram_ip' tag.
25
 	TagTelegramIP = "telegram_ip"
98
 	TagTelegramIP = "telegram_ip"
26
 
99
 
100
+	// TagDC defines a name of the 'dc' tag.
27
 	TagDC = "dc"
101
 	TagDC = "dc"
28
 
102
 
103
+	// TagDirection defines a name of the 'direction' tag.
29
 	TagDirection           = "direction"
104
 	TagDirection           = "direction"
30
 	TagDirectionToClient   = "to_client"
105
 	TagDirectionToClient   = "to_client"
31
 	TagDirectionFromClient = "from_client"
106
 	TagDirectionFromClient = "from_client"

+ 12
- 0
stats/prometheus.go Wyświetl plik

126
 	p.streams = make(map[string]*streamInfo)
126
 	p.streams = make(map[string]*streamInfo)
127
 }
127
 }
128
 
128
 
129
+// PrometheusFactory is a factory of events.Observers which collect
130
+// information in a format suitable for Prometheus.
131
+//
132
+// This factory can also serve on a given listener. In that case it
133
+// starts HTTP server with a single endpoint - a Prometheus-compatible
134
+// scrape output.
129
 type PrometheusFactory struct {
135
 type PrometheusFactory struct {
130
 	httpServer *http.Server
136
 	httpServer *http.Server
131
 
137
 
142
 	metricReplayAttacks      prometheus.Counter
148
 	metricReplayAttacks      prometheus.Counter
143
 }
149
 }
144
 
150
 
151
+// Make builds a new observer.
145
 func (p *PrometheusFactory) Make() events.Observer {
152
 func (p *PrometheusFactory) Make() events.Observer {
146
 	return prometheusProcessor{
153
 	return prometheusProcessor{
147
 		streams: make(map[string]*streamInfo),
154
 		streams: make(map[string]*streamInfo),
149
 	}
156
 	}
150
 }
157
 }
151
 
158
 
159
+// Serve starts an HTTP server on a given listener.
152
 func (p *PrometheusFactory) Serve(listener net.Listener) error {
160
 func (p *PrometheusFactory) Serve(listener net.Listener) error {
153
 	return p.httpServer.Serve(listener)
161
 	return p.httpServer.Serve(listener)
154
 }
162
 }
155
 
163
 
164
+// Close stops a factory. Please pay attention that underlying listener
165
+// is not closed.
156
 func (p *PrometheusFactory) Close() error {
166
 func (p *PrometheusFactory) Close() error {
157
 	return p.httpServer.Shutdown(context.Background())
167
 	return p.httpServer.Shutdown(context.Background())
158
 }
168
 }
159
 
169
 
170
+// NewPrometheus builds an events.ObserverFactory which can serve HTTP
171
+// endpoint with Prometheus scrape data.
160
 func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint: funlen
172
 func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint: funlen
161
 	registry := prometheus.NewPedanticRegistry()
173
 	registry := prometheus.NewPedanticRegistry()
162
 	httpHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
174
 	httpHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{

+ 13
- 0
stats/statsd.go Wyświetl plik

138
 	}
138
 	}
139
 }
139
 }
140
 
140
 
141
+// StatsdFactory is a factory of events.Observers which dumps
142
+// information to statsd.
143
+//
144
+// Please beware that we support ONLY UDP endpoints there. And this
145
+// factory won't use mtglib.Network so it won't use a proxy if you
146
+// provide any. If you need it, I would recommend starting a local
147
+// statsd and route metrics further by features of the chosen server.
141
 type StatsdFactory struct {
148
 type StatsdFactory struct {
142
 	client *statsd.Client
149
 	client *statsd.Client
143
 }
150
 }
144
 
151
 
152
+// Close stops sending requests to statsd.
145
 func (s StatsdFactory) Close() error {
153
 func (s StatsdFactory) Close() error {
146
 	return s.client.Close()
154
 	return s.client.Close()
147
 }
155
 }
148
 
156
 
157
+// Make build a new observer.
149
 func (s StatsdFactory) Make() events.Observer {
158
 func (s StatsdFactory) Make() events.Observer {
150
 	return statsdProcessor{
159
 	return statsdProcessor{
151
 		client:  s.client,
160
 		client:  s.client,
153
 	}
162
 	}
154
 }
163
 }
155
 
164
 
165
+// NewStatsd builds an events.ObserverFactory that sends events
166
+// to statsd.
167
+//
168
+// Valid tagFormats are 'datadog', 'influxdb' and 'graphite'.
156
 func NewStatsd(address string, log logger.StdLikeLogger,
169
 func NewStatsd(address string, log logger.StdLikeLogger,
157
 	metricPrefix, tagFormat string) (StatsdFactory, error) {
170
 	metricPrefix, tagFormat string) (StatsdFactory, error) {
158
 	options := []statsd.Option{
171
 	options := []statsd.Option{

+ 1
- 1
timeattack/init.go Wyświetl plik

1
-// TimeAttack has implementation of mtglib.TimeAttackDetector>
1
+// TimeAttack has implementation of mtglib.TimeAttackDetector.
2
 package timeattack
2
 package timeattack
3
 
3
 
4
 import "time"
4
 import "time"

Ładowanie…
Anuluj
Zapisz