-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
36 lines (31 loc) · 1.1 KB
/
metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package middleware
import (
"github.com/D3rus/go-library/app"
"github.com/D3rus/go-library/metric"
metric2 "github.com/D3rus/go-library/socket/metric"
"github.com/D3rus/go-library/socket/server"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type ConnectionCounter struct {
counterVec *prometheus.CounterVec
}
func NewConnectionCounter(appConfig *app.Config) *ConnectionCounter {
return &ConnectionCounter{
counterVec: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "go_socket_server_connection_count",
Help: "counter of all new connections of all protocols and interfaces",
ConstLabels: map[string]string{
metric.AppNameFieldName: appConfig.Name,
},
}, []string{metric2.ProtocolFieldName}),
}
}
func (middleware *ConnectionCounter) Middleware(next server.Handler) server.Handler {
return server.HandlerFunc(func(connection *server.Connection) error {
middleware.counterVec.With(prometheus.Labels{
metric2.ProtocolFieldName: connection.Conn.RemoteAddr().Network(),
}).Inc()
return next.Handle(connection)
})
}