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

Implement integration with statsd

tags/0.10
9seconds 7 лет назад
Родитель
Сommit
5ddf3d77d1
3 измененных файлов: 98 добавлений и 5 удалений
  1. 3
    1
      main.go
  2. 16
    4
      stats/server.go
  3. 79
    0
      stats/statsd.go

+ 3
- 1
main.go Просмотреть файл

@@ -158,7 +158,9 @@ func main() {
158 158
 		zap.S().Infow("Use direct connection to Telegram")
159 159
 	}
160 160
 
161
-	go stats.Start(conf)
161
+	if err := stats.Start(conf); err != nil {
162
+		panic(err)
163
+	}
162 164
 
163 165
 	server := proxy.NewProxy(conf)
164 166
 	if err := server.Serve(); err != nil {

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

@@ -14,7 +14,7 @@ import (
14 14
 var instance *stats
15 15
 
16 16
 // Start starts new statisitcs server.
17
-func Start(conf *config.Config) {
17
+func Start(conf *config.Config) error {
18 18
 	log := zap.S().Named("stats")
19 19
 
20 20
 	instance = &stats{
@@ -23,6 +23,14 @@ func Start(conf *config.Config) {
23 23
 		mutex:  &sync.RWMutex{},
24 24
 	}
25 25
 
26
+	if conf.StatsD.Enabled {
27
+		client, err := newStatsd(conf)
28
+		if err != nil {
29
+			return err
30
+		}
31
+		go client.run()
32
+	}
33
+
26 34
 	go crashManager()
27 35
 	go connectionManager()
28 36
 	go trafficManager()
@@ -51,7 +59,11 @@ func Start(conf *config.Config) {
51 59
 		}
52 60
 	})
53 61
 
54
-	if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
55
-		log.Fatalw("Stats server has been stopped", "error", err)
56
-	}
62
+	go func() {
63
+		if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
64
+			log.Fatalw("Stats server has been stopped", "error", err)
65
+		}
66
+	}()
67
+
68
+	return nil
57 69
 }

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

@@ -0,0 +1,79 @@
1
+package stats
2
+
3
+import (
4
+	"time"
5
+
6
+	"github.com/juju/errors"
7
+	statsd "gopkg.in/alexcesaro/statsd.v2"
8
+
9
+	"github.com/9seconds/mtg/config"
10
+)
11
+
12
+const (
13
+	statsdConnectionsAbridgedV4 = "connections.abridged.ipv4"
14
+	statsdConnectionsAbridgedV6 = "connections.abridged.ipv6"
15
+
16
+	statsdConnectionsIntermediateV4 = "connections.intermediate.ipv4"
17
+	statsdConnectionsIntermediateV6 = "connections.intermediate.ipv6"
18
+
19
+	statsdConnectionsSecureV4 = "connections.secure.ipv4"
20
+	statsdConnectionsSecureV6 = "connections.secure.ipv6"
21
+
22
+	statsdTrafficIngress = "traffic.ingress"
23
+	statsdTrafficEgress  = "traffic.egress"
24
+
25
+	statsdSpeedIngress = "speed.ingress"
26
+	statsdSpeedEgress  = "speed.egress"
27
+
28
+	statsdCrashes = "crashes"
29
+)
30
+
31
+const statsdPollTime = time.Second
32
+
33
+type statsdExporter struct {
34
+	client *statsd.Client
35
+}
36
+
37
+func (s *statsdExporter) run() {
38
+	for range time.Tick(statsdPollTime) {
39
+		instance.mutex.Lock()
40
+
41
+		s.client.Gauge(statsdConnectionsAbridgedV4, instance.Connections.Abridged.IPv4)
42
+		s.client.Gauge(statsdConnectionsAbridgedV6, instance.Connections.Abridged.IPv6)
43
+		s.client.Gauge(statsdConnectionsIntermediateV4, instance.Connections.Intermediate.IPv4)
44
+		s.client.Gauge(statsdConnectionsIntermediateV6, instance.Connections.Intermediate.IPv6)
45
+		s.client.Gauge(statsdConnectionsSecureV4, instance.Connections.Secure.IPv4)
46
+		s.client.Gauge(statsdConnectionsSecureV6, instance.Connections.Secure.IPv6)
47
+		s.client.Gauge(statsdTrafficIngress, uint64(instance.Traffic.Ingress))
48
+		s.client.Gauge(statsdTrafficEgress, uint64(instance.Traffic.Egress))
49
+		s.client.Gauge(statsdSpeedIngress, uint64(instance.Speed.Ingress))
50
+		s.client.Gauge(statsdSpeedEgress, uint64(instance.Speed.Egress))
51
+		s.client.Gauge(statsdCrashes, instance.Crashes)
52
+
53
+		instance.mutex.Unlock()
54
+	}
55
+}
56
+
57
+func newStatsd(conf *config.Config) (*statsdExporter, error) {
58
+	options := []statsd.Option{
59
+		statsd.Network(conf.StatsD.Addr.Network()),
60
+		statsd.Address(conf.StatsD.Addr.String()),
61
+		statsd.Prefix(conf.StatsD.Prefix),
62
+	}
63
+
64
+	if conf.StatsD.TagsFormat > 0 {
65
+		options = append(options, statsd.TagsFormat(conf.StatsD.TagsFormat))
66
+		tags := make([]string, len(conf.StatsD.Tags)*2)
67
+		for k, v := range conf.StatsD.Tags {
68
+			tags = append(tags, k, v)
69
+		}
70
+		options = append(options, statsd.Tags(tags...))
71
+	}
72
+
73
+	client, err := statsd.New(options...)
74
+	if err != nil {
75
+		return nil, errors.Annotate(err, "Cannot create statsd client")
76
+	}
77
+
78
+	return &statsdExporter{client: client}, nil
79
+}

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