Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

statsd.go 2.4KB

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