Skip to content

Commit

Permalink
Added proxy metrics (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScruffyPants authored Dec 21, 2023
1 parent 3335f4c commit 71be35f
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 88 deletions.
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package api

import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"time"

Expand All @@ -42,6 +43,7 @@ func NewServer(addr string, storage stickySaver, dt domainTracker) *http.Server
gin.SetMode(gin.ReleaseMode)
ginEngine := gin.Default()

ginEngine.GET("/metrics", gin.WrapH(promhttp.Handler()))
v1 := ginEngine.Group("/api/v1")
{
v1.POST("/map", func(c *gin.Context) {
Expand Down
27 changes: 17 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ module github.com/mysteriumnetwork/openvpn-forwarder
go 1.18

require (
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575
github.com/gin-gonic/gin v1.4.0
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b
github.com/magefile/mage v1.8.0
github.com/mysteriumnetwork/go-ci v0.0.0-20190726090015-267593de918b
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.17.0
github.com/soheilhy/cmux v0.1.4
github.com/stretchr/testify v1.5.1
github.com/tidwall/buntdb v1.1.0
golang.org/x/net v0.0.0-20201021035429-f5854403a974
golang.org/x/net v0.10.0
)

require (
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/json-iterator/go v1.1.7 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/tidwall/btree v0.0.0-20170113224114-9876f1454cf0 // indirect
github.com/tidwall/gjson v1.3.2 // indirect
github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb // indirect
Expand All @@ -33,11 +40,11 @@ require (
github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e // indirect
github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/grpc/examples v0.0.0-20221116180207-817c1e8c417e // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
81 changes: 59 additions & 22 deletions go.sum

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"flag"
"fmt"
"github.com/mysteriumnetwork/openvpn-forwarder/metrics"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -115,7 +116,14 @@ func main() {
_ = log.Criticalf("Failed to parse port map: %v", err)
os.Exit(1)
}
proxyServer := proxy.NewServer(allowedSubnets, allowedIPs, dialer, sm, domainTracer, portMap)
metricService, err := metrics.NewMetricsService()
if err != nil {
_ = log.Criticalf("Failed to start metrics service: %s", err)
os.Exit(1)
}

proxyServer := proxy.NewServer(allowedSubnets, allowedIPs, dialer, sm, domainTracer, portMap, metricService.ProxyHandlerMiddleware)
proxyServer.AddListener(metricService)

var wg sync.WaitGroup
for p := range portMap {
Expand Down
110 changes: 110 additions & 0 deletions metrics/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package metrics

import (
"github.com/mysteriumnetwork/openvpn-forwarder/proxy"
"github.com/prometheus/client_golang/prometheus"
"time"
)

var _ proxy.Listener = (*Service)(nil)

type Service struct {
proxyRequestDuration *prometheus.HistogramVec
proxyRequestData *prometheus.CounterVec
proxyNumberOfLiveConnecions *prometheus.GaugeVec
proxyNumberOfIncommingConnections *prometheus.CounterVec
proxyNumberOfProcessedConnections *prometheus.CounterVec
}

func NewMetricsService() (*Service, error) {
proxyRequestDuration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "proxy_request_duration",
Help: "Proxy request duration in seconds",
}, []string{"request_type"})

if err := prometheus.Register(proxyRequestDuration); err != nil {
return nil, err
}

proxyRequestData := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_request_data",
Help: "Proxy request data in bytes",
}, []string{"request_type", "direction"})

if err := prometheus.Register(proxyRequestData); err != nil {
return nil, err
}

proxyNumberOfLiveConnections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "proxy_number_of_live_connections",
Help: "Number of currently live connections",
}, []string{"request_type"})

if err := prometheus.Register(proxyNumberOfLiveConnections); err != nil {
return nil, err
}

proxyNumberOfIncommingConnections := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_number_of_incomming_connections",
Help: "Number of incomming connections (failed and successful)",
}, []string{})

if err := prometheus.Register(proxyNumberOfIncommingConnections); err != nil {
return nil, err
}

proxyNumberOfProcessedConnections := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_number_of_processed_connections",
Help: "Number of incmming connections which were succesfully assigned and processed",
}, []string{"request_type"})

if err := prometheus.Register(proxyNumberOfProcessedConnections); err != nil {
return nil, err
}

return &Service{
proxyRequestDuration: proxyRequestDuration,
proxyRequestData: proxyRequestData,
proxyNumberOfLiveConnecions: proxyNumberOfLiveConnections,
proxyNumberOfIncommingConnections: proxyNumberOfIncommingConnections,
proxyNumberOfProcessedConnections: proxyNumberOfProcessedConnections,
}, nil
}

func (s *Service) ProxyHandlerMiddleware(next func(c *proxy.Context), proxyHandlerType string) func(c *proxy.Context) {
return func(c *proxy.Context) {
startTime := time.Now()

s.proxyNumberOfLiveConnecions.With(prometheus.Labels{
"request_type": proxyHandlerType,
}).Inc()

next(c)

s.proxyNumberOfLiveConnecions.With(prometheus.Labels{
"request_type": proxyHandlerType,
}).Dec()

s.proxyRequestDuration.With(prometheus.Labels{
"request_type": proxyHandlerType,
}).Observe(time.Since(startTime).Seconds())

s.proxyRequestData.With(prometheus.Labels{
"request_type": proxyHandlerType,
"direction": "sent",
}).Add(float64(c.BytesSent()))

s.proxyRequestData.With(prometheus.Labels{
"request_type": proxyHandlerType,
"direction": "received",
}).Add(float64(c.BytesReceived()))

s.proxyNumberOfProcessedConnections.With(prometheus.Labels{
"request_type": proxyHandlerType,
}).Inc()
}
}

func (s *Service) OnProxyConnectionAccept() {
s.proxyNumberOfIncommingConnections.With(prometheus.Labels{}).Inc()
}
11 changes: 11 additions & 0 deletions proxy/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ type Context struct {

destinationHost string
destinationAddress string

bytesSent int64
bytesReceived int64
}

func (c *Context) BytesSent() int64 {
return c.bytesSent
}

func (c *Context) BytesReceived() int64 {
return c.bytesReceived
}
Loading

0 comments on commit 71be35f

Please sign in to comment.