| 9 | const ( |
| 10 | namespace = "frp" |
| 11 | serverSubsystem = "server" |
| 12 | ) |
| 13 |
| 14 | var ServerMetrics metrics.ServerMetrics = newServerMetrics() |
| 15 |
| 16 | type serverMetrics struct { |
| 17 | clientCount prometheus.Gauge |
| 18 | proxyCount *prometheus.GaugeVec |
| 19 | proxyCountDetailed *prometheus.GaugeVec |
| 20 | connectionCount *prometheus.GaugeVec |
| 21 | trafficIn *prometheus.CounterVec |
| 22 | trafficOut *prometheus.CounterVec |
| 23 | } |
| 24 |
| 25 | func (m *serverMetrics) NewClient() { |
| 26 | m.clientCount.Inc() |
| 27 | } |
| 28 |
| 29 | func (m *serverMetrics) CloseClient() { |
| 30 | m.clientCount.Dec() |
| 31 | } |
| 32 |
| 33 | func (m *serverMetrics) NewProxy(name string, proxyType string, _ string, _ string) { |
| 34 | m.proxyCount.WithLabelValues(proxyType).Inc() |
| 35 | m.proxyCountDetailed.WithLabelValues(proxyType, name).Inc() |
| 36 | } |
| 37 |
| 38 | func (m *serverMetrics) CloseProxy(name string, proxyType string) { |
| 39 | m.proxyCount.WithLabelValues(proxyType).Dec() |
| 40 | m.proxyCountDetailed.WithLabelValues(proxyType, name).Dec() |
| 41 | } |
| 42 |
| 43 | func (m *serverMetrics) OpenConnection(name string, proxyType string) { |
| 44 | m.connectionCount.WithLabelValues(name, proxyType).Inc() |
| 45 | } |
| 46 |
| 47 | func (m *serverMetrics) CloseConnection(name string, proxyType string) { |
| 48 | m.connectionCount.WithLabelValues(name, proxyType).Dec() |
| 49 | } |
| 50 |
| 51 | func (m *serverMetrics) AddTrafficIn(name string, proxyType string, trafficBytes int64) { |
| 52 | m.trafficIn.WithLabelValues(name, proxyType).Add(float64(trafficBytes)) |
| 53 | } |
| 54 |
| 55 | func (m *serverMetrics) AddTrafficOut(name string, proxyType string, trafficBytes int64) { |
| 56 | m.trafficOut.WithLabelValues(name, proxyType).Add(float64(trafficBytes)) |
| 57 | } |
| 58 |
| 59 | func newServerMetrics() *serverMetrics { |
| 60 | m := &serverMetrics{ |
| 61 | clientCount: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 62 | Namespace: namespace, |
| 63 | Subsystem: serverSubsystem, |
| 64 | Name: "client_counts", |
| 65 | Help: "The current client counts of frps", |
| 66 | }), |
| 67 | proxyCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 68 | Namespace: namespace, |
| 69 | Subsystem: serverSubsystem, |
| 70 | Name: "proxy_counts", |
| 71 | Help: "The current proxy counts", |
| 72 | }, []string{"type"}), |
| 73 | proxyCountDetailed: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 74 | Namespace: namespace, |
| 75 | Subsystem: serverSubsystem, |
| 76 | Name: "proxy_counts_detailed", |
| 77 | Help: "The current number of proxies grouped by type and name", |
| 78 | }, []string{"type", "name"}), |
| 79 | connectionCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 80 | Namespace: namespace, |
| 81 | Subsystem: serverSubsystem, |
| 82 | Name: "connection_counts", |
| 83 | Help: "The current connection counts", |
| 84 | }, []string{"name", "type"}), |
| 85 | trafficIn: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 86 | Namespace: namespace, |
| 87 | Subsystem: serverSubsystem, |
| 88 | Name: "traffic_in", |
| 89 | Help: "The total in traffic", |
| 90 | }, []string{"name", "type"}), |
| 91 | trafficOut: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 92 | Namespace: namespace, |
| 93 | Subsystem: serverSubsystem, |
| 94 | Name: "traffic_out", |
| 95 | Help: "The total out traffic", |
| 96 | }, []string{"name", "type"}), |
| 97 | } |
| 98 | prometheus.MustRegister(m.clientCount) |
| 99 | prometheus.MustRegister(m.proxyCount) |
| 100 | prometheus.MustRegister(m.proxyCountDetailed) |
| 101 | prometheus.MustRegister(m.connectionCount) |
| 102 | prometheus.MustRegister(m.trafficIn) |
| 103 | prometheus.MustRegister(m.trafficOut) |
| 104 | return m |
| 105 | } |