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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

prometheus.go 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package stats
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/juju/errors"
  6. "github.com/prometheus/client_golang/prometheus"
  7. "github.com/prometheus/client_golang/prometheus/promhttp"
  8. "github.com/9seconds/mtg/config"
  9. )
  10. const prometheusPollTime = time.Second
  11. type prometheusExporter struct {
  12. registry prometheus.Gatherer
  13. connections *prometheus.GaugeVec
  14. traffic *prometheus.GaugeVec
  15. speed *prometheus.GaugeVec
  16. crashes prometheus.Gauge
  17. }
  18. func (p *prometheusExporter) run() {
  19. for range time.Tick(prometheusPollTime) {
  20. instance := GetStats()
  21. p.connections.WithLabelValues("abridged", "v4").Set(float64(instance.Connections.Abridged.IPv4))
  22. p.connections.WithLabelValues("abridged", "v6").Set(float64(instance.Connections.Abridged.IPv6))
  23. p.connections.WithLabelValues("intermediate", "v4").Set(float64(instance.Connections.Intermediate.IPv4))
  24. p.connections.WithLabelValues("intermediate", "v6").Set(float64(instance.Connections.Intermediate.IPv6))
  25. p.connections.WithLabelValues("secure", "v4").Set(float64(instance.Connections.Secure.IPv4))
  26. p.connections.WithLabelValues("secure", "v6").Set(float64(instance.Connections.Secure.IPv6))
  27. p.traffic.WithLabelValues("ingress").Set(float64(instance.Traffic.ingress))
  28. p.traffic.WithLabelValues("egress").Set(float64(instance.Traffic.egress))
  29. p.speed.WithLabelValues("ingress").Set(float64(instance.Speed.ingress))
  30. p.speed.WithLabelValues("egress").Set(float64(instance.Speed.egress))
  31. p.crashes.Set(float64(instance.Crashes))
  32. }
  33. }
  34. func (p *prometheusExporter) getHTTPHandler() http.Handler {
  35. return promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{})
  36. }
  37. func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
  38. registry := prometheus.NewRegistry()
  39. connections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  40. Namespace: conf.Prometheus.Prefix,
  41. Name: "connections",
  42. Help: "Current number of connections to the proxy.",
  43. }, []string{"type", "protocol"})
  44. traffic := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  45. Namespace: conf.Prometheus.Prefix,
  46. Name: "traffic",
  47. Help: "Traffic passed through the proxy in bytes.",
  48. }, []string{"direction"})
  49. speed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  50. Namespace: conf.Prometheus.Prefix,
  51. Name: "speed",
  52. Help: "Current throughput in bytes per second.",
  53. }, []string{"direction"})
  54. crashes := prometheus.NewGauge(prometheus.GaugeOpts{
  55. Namespace: conf.Prometheus.Prefix,
  56. Name: "crashes",
  57. Help: "How many crashes happened.",
  58. })
  59. if err := registry.Register(connections); err != nil {
  60. return nil, errors.Annotate(err, "Cannot register connections collector")
  61. }
  62. if err := registry.Register(traffic); err != nil {
  63. return nil, errors.Annotate(err, "cannot register traffic collector")
  64. }
  65. if err := registry.Register(speed); err != nil {
  66. return nil, errors.Annotate(err, "cannot register speed collector")
  67. }
  68. if err := registry.Register(crashes); err != nil {
  69. return nil, errors.Annotate(err, "cannot register crashes collector")
  70. }
  71. return &prometheusExporter{
  72. registry: registry,
  73. connections: connections,
  74. traffic: traffic,
  75. speed: speed,
  76. crashes: crashes,
  77. }, nil
  78. }