mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 22:24:02 +03:00
Add new stats metric
This commit is contained in:
+46
-17
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
@@ -13,10 +14,11 @@ import (
|
||||
)
|
||||
|
||||
type statsPrometheus struct {
|
||||
connections *prometheus.GaugeVec
|
||||
traffic *prometheus.GaugeVec
|
||||
crashes prometheus.Gauge
|
||||
antiReplays prometheus.Gauge
|
||||
connections *prometheus.GaugeVec
|
||||
telegramConnections *prometheus.GaugeVec
|
||||
traffic *prometheus.GaugeVec
|
||||
crashes prometheus.Gauge
|
||||
antiReplays prometheus.Counter
|
||||
}
|
||||
|
||||
func (s *statsPrometheus) IngressTraffic(traffic int) {
|
||||
@@ -38,18 +40,39 @@ func (s *statsPrometheus) ClientDisconnected(connectionType conntypes.Connection
|
||||
func (s *statsPrometheus) changeConnections(connectionType conntypes.ConnectionType,
|
||||
addr *net.TCPAddr,
|
||||
increment float64) {
|
||||
var labels [2]string
|
||||
labels := [...]string{
|
||||
"intermediate",
|
||||
"ipv4",
|
||||
}
|
||||
|
||||
switch connectionType {
|
||||
case conntypes.ConnectionTypeAbridged:
|
||||
labels[0] = "abridged"
|
||||
case conntypes.ConnectionTypeSecure:
|
||||
labels[0] = "secured"
|
||||
default:
|
||||
labels[0] = "intermediate"
|
||||
}
|
||||
|
||||
labels[1] = "ipv4"
|
||||
if addr.IP.To4() == nil {
|
||||
labels[1] = "ipv6" // nolint: goconst
|
||||
}
|
||||
|
||||
s.connections.WithLabelValues(labels[:]...).Add(increment)
|
||||
}
|
||||
|
||||
func (s *statsPrometheus) TelegramConnected(dc conntypes.DC, addr *net.TCPAddr) {
|
||||
s.changeTelegramConnections(dc, addr, 1.0)
|
||||
}
|
||||
|
||||
func (s *statsPrometheus) TelegramDisconnected(dc conntypes.DC, addr *net.TCPAddr) {
|
||||
s.changeTelegramConnections(dc, addr, -1.0)
|
||||
}
|
||||
|
||||
func (s *statsPrometheus) changeTelegramConnections(dc conntypes.DC, addr *net.TCPAddr, increment float64) {
|
||||
labels := [...]string{
|
||||
strconv.Itoa(int(dc)),
|
||||
"ipv4",
|
||||
}
|
||||
|
||||
if addr.IP.To4() == nil {
|
||||
labels[1] = "ipv6"
|
||||
}
|
||||
@@ -65,26 +88,32 @@ func (s *statsPrometheus) AntiReplayDetected() {
|
||||
s.antiReplays.Inc()
|
||||
}
|
||||
|
||||
func newStatsPrometheus(mux *http.ServeMux) (Stats, error) {
|
||||
registry := prometheus.NewRegistry()
|
||||
func newStatsPrometheus(mux *http.ServeMux) (Interface, error) {
|
||||
registry := prometheus.NewPedanticRegistry()
|
||||
|
||||
instance := &statsPrometheus{
|
||||
connections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: config.C.PrometheusStats.Prefix,
|
||||
Namespace: config.C.StatsNamespace,
|
||||
Name: "connections",
|
||||
Help: "Current number of connections to the proxy.",
|
||||
Help: "Current number of client connections to the proxy.",
|
||||
}, []string{"type", "protocol"}),
|
||||
telegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: config.C.StatsNamespace,
|
||||
Name: "telegram_connections",
|
||||
Help: "Current number of telegram connections established by this proxy.",
|
||||
}, []string{"dc", "protocol"}),
|
||||
traffic: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: config.C.PrometheusStats.Prefix,
|
||||
Namespace: config.C.StatsNamespace,
|
||||
Name: "traffic",
|
||||
Help: "Traffic passed through the proxy in bytes.",
|
||||
}, []string{"direction"}),
|
||||
crashes: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: config.C.PrometheusStats.Prefix,
|
||||
Namespace: config.C.StatsNamespace,
|
||||
Name: "crashes",
|
||||
Help: "How many crashes happened.",
|
||||
}),
|
||||
antiReplays: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: config.C.PrometheusStats.Prefix,
|
||||
antiReplays: prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Namespace: config.C.StatsNamespace,
|
||||
Name: "anti_replays",
|
||||
Help: "How many anti replay attacks were prevented.",
|
||||
}),
|
||||
@@ -104,7 +133,7 @@ func newStatsPrometheus(mux *http.ServeMux) (Stats, error) {
|
||||
}
|
||||
|
||||
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
|
||||
mux.Handle("/prometheus", handler)
|
||||
mux.Handle("/", handler)
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user