add metrics

This commit is contained in:
BuckarooBanzay 2023-09-20 19:16:47 +02:00
parent 539141ea06
commit ccdd62b619
5 changed files with 83 additions and 0 deletions

13
go.mod
View File

@ -3,3 +3,16 @@ module minetest-web
go 1.21.1 go 1.21.1
require github.com/gorilla/websocket v1.5.0 require github.com/gorilla/websocket v1.5.0
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
golang.org/x/sys v0.8.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

28
go.sum
View File

@ -1,2 +1,30 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM=
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

View File

@ -6,6 +6,8 @@ import (
"minetest-web/public" "minetest-web/public"
"net/http" "net/http"
"os" "os"
"github.com/prometheus/client_golang/prometheus/promhttp"
) )
func main() { func main() {
@ -29,6 +31,9 @@ func main() {
// websocket proxy // websocket proxy
http.HandleFunc("/proxy", HandleProxy) http.HandleFunc("/proxy", HandleProxy)
// metrics
http.Handle("/metrics", promhttp.Handler())
// app info endpoint // app info endpoint
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{ json.NewEncoder(w).Encode(map[string]any{

31
metrics.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var WStoUDPBytes = promauto.NewCounter(prometheus.CounterOpts{
Name: "ws_to_udp_bytes",
Help: "Number of bytes sent from websocket to udp",
})
var WStoUDPPackets = promauto.NewCounter(prometheus.CounterOpts{
Name: "ws_to_udp_packets",
Help: "Number of packets sent from websocket to udp",
})
var UDPtoWSBytes = promauto.NewCounter(prometheus.CounterOpts{
Name: "udp_to_ws_bytes",
Help: "Number of bytes sent from udp to websocket",
})
var UDPtoWSPackets = promauto.NewCounter(prometheus.CounterOpts{
Name: "udp_to_ws_packets",
Help: "Number of packets sent from udp to websocket",
})
var Clients = promauto.NewGauge(prometheus.GaugeOpts{
Name: "client_udp_count",
Help: "Number of connected websocket udp clients",
})

6
ws.go
View File

@ -139,6 +139,7 @@ func forwardData(conn *websocket.Conn, host string, port int64) error {
errchan := make(chan error, 1) errchan := make(chan error, 1)
run := atomic.Bool{} run := atomic.Bool{}
run.Store(true) run.Store(true)
Clients.Inc()
conn.WriteMessage(websocket.TextMessage, []byte("PROXY OK")) conn.WriteMessage(websocket.TextMessage, []byte("PROXY OK"))
@ -155,6 +156,8 @@ func forwardData(conn *websocket.Conn, host string, port int64) error {
errchan <- err errchan <- err
return return
} }
UDPtoWSBytes.Add(float64(len))
UDPtoWSPackets.Add(1)
} }
}() }()
@ -183,11 +186,14 @@ func forwardData(conn *websocket.Conn, host string, port int64) error {
errchan <- err errchan <- err
return return
} }
WStoUDPBytes.Add(float64(len(data)))
WStoUDPPackets.Add(1)
} }
}() }()
err = <-errchan err = <-errchan
run.Store(false) run.Store(false)
conn.Close() conn.Close()
Clients.Dec()
return err return err
} }