feat: add prometheus metrics (#342)

This commit is contained in:
Slawomir CALUCH
2024-11-09 18:43:51 +00:00
committed by GitHub
parent da378b9154
commit 2624e25c94
6 changed files with 106 additions and 21 deletions
+3
View File
@@ -5,6 +5,7 @@ import (
"net/http"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
@@ -15,6 +16,8 @@ func StartApiServer(apiBinding string) {
apiRoutes.Path("/vars").Handler(expvar.Handler())
apiRoutes.Path("/metrics").Handler(promhttp.Handler())
go func() {
logrus.WithError(
http.ListenAndServe(apiBinding, apiRoutes)).Error("API server failed")
+7 -6
View File
@@ -26,10 +26,11 @@ const (
var noDeadline time.Time
type ConnectorMetrics struct {
Errors metrics.Counter
BytesTransmitted metrics.Counter
Connections metrics.Counter
ActiveConnections metrics.Gauge
Errors metrics.Counter
BytesTransmitted metrics.Counter
ConnectionsFrontend metrics.Counter
ConnectionsBackend metrics.Counter
ActiveConnections metrics.Gauge
}
func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool, receiveProxyProto bool, trustedProxyNets []*net.IPNet) *Connector {
@@ -158,7 +159,7 @@ func (c *Connector) acceptConnections(ctx context.Context, ln net.Listener, conn
}
func (c *Connector) HandleConnection(ctx context.Context, frontendConn net.Conn) {
c.metrics.Connections.With("side", "frontend").Add(1)
c.metrics.ConnectionsFrontend.Add(1)
//noinspection GoUnhandledErrorResult
defer frontendConn.Close()
@@ -276,7 +277,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
return
}
c.metrics.Connections.With("side", "backend", "host", resolvedHost).Add(1)
c.metrics.ConnectionsBackend.With("host", resolvedHost).Add(1)
c.metrics.ActiveConnections.Set(float64(
atomic.AddInt32(&c.activeConnections, 1)))