server.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package prometheus
  2. import (
  3. "github.com/prometheus/client_golang/prometheus"
  4. "github.com/fatedier/frp/server/metrics"
  5. )
  6. const (
  7. namespace = "frp"
  8. serverSubsystem = "server"
  9. )
  10. var ServerMetrics metrics.ServerMetrics = newServerMetrics()
  11. type serverMetrics struct {
  12. clientCount prometheus.Gauge
  13. proxyCount *prometheus.GaugeVec
  14. connectionCount *prometheus.GaugeVec
  15. trafficIn *prometheus.CounterVec
  16. trafficOut *prometheus.CounterVec
  17. }
  18. func (m *serverMetrics) NewClient() {
  19. m.clientCount.Inc()
  20. }
  21. func (m *serverMetrics) CloseClient() {
  22. m.clientCount.Dec()
  23. }
  24. func (m *serverMetrics) NewProxy(_ string, proxyType string) {
  25. m.proxyCount.WithLabelValues(proxyType).Inc()
  26. }
  27. func (m *serverMetrics) CloseProxy(_ string, proxyType string) {
  28. m.proxyCount.WithLabelValues(proxyType).Dec()
  29. }
  30. func (m *serverMetrics) OpenConnection(name string, proxyType string) {
  31. m.connectionCount.WithLabelValues(name, proxyType).Inc()
  32. }
  33. func (m *serverMetrics) CloseConnection(name string, proxyType string) {
  34. m.connectionCount.WithLabelValues(name, proxyType).Dec()
  35. }
  36. func (m *serverMetrics) AddTrafficIn(name string, proxyType string, trafficBytes int64) {
  37. m.trafficIn.WithLabelValues(name, proxyType).Add(float64(trafficBytes))
  38. }
  39. func (m *serverMetrics) AddTrafficOut(name string, proxyType string, trafficBytes int64) {
  40. m.trafficOut.WithLabelValues(name, proxyType).Add(float64(trafficBytes))
  41. }
  42. func newServerMetrics() *serverMetrics {
  43. m := &serverMetrics{
  44. clientCount: prometheus.NewGauge(prometheus.GaugeOpts{
  45. Namespace: namespace,
  46. Subsystem: serverSubsystem,
  47. Name: "client_counts",
  48. Help: "The current client counts of frps",
  49. }),
  50. proxyCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  51. Namespace: namespace,
  52. Subsystem: serverSubsystem,
  53. Name: "proxy_counts",
  54. Help: "The current proxy counts",
  55. }, []string{"type"}),
  56. connectionCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  57. Namespace: namespace,
  58. Subsystem: serverSubsystem,
  59. Name: "connection_counts",
  60. Help: "The current connection counts",
  61. }, []string{"name", "type"}),
  62. trafficIn: prometheus.NewCounterVec(prometheus.CounterOpts{
  63. Namespace: namespace,
  64. Subsystem: serverSubsystem,
  65. Name: "traffic_in",
  66. Help: "The total in traffic",
  67. }, []string{"name", "type"}),
  68. trafficOut: prometheus.NewCounterVec(prometheus.CounterOpts{
  69. Namespace: namespace,
  70. Subsystem: serverSubsystem,
  71. Name: "traffic_out",
  72. Help: "The total out traffic",
  73. }, []string{"name", "type"}),
  74. }
  75. prometheus.MustRegister(m.clientCount)
  76. prometheus.MustRegister(m.proxyCount)
  77. prometheus.MustRegister(m.connectionCount)
  78. prometheus.MustRegister(m.trafficIn)
  79. prometheus.MustRegister(m.trafficOut)
  80. return m
  81. }