Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Prometheus metrics for node #84

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/go-kit/kit v0.11.0
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/rs/cors v1.8.0
github.com/rs/zerolog v1.23.0
github.com/sentinel-official/hub v0.7.0
Expand Down
219 changes: 219 additions & 0 deletions rest/metrics/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package metrics

import (
"fmt"
"net/http"

"github.com/cosmos/cosmos-sdk/version"
"github.com/sentinel-official/dvpn-node/context"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
nodeInfoGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_node_info",
Help: "dVPN node info",
},
[]string{"address", "moniker", "operator", "version"},
)

locationInfoGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_location_info",
Help: "dVPN location info",
},
[]string{"address", "moniker", "operator", "city", "country", "latitude", "longitude"},
)

handshakeEnabledGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_handshake_enabled",
Help: "Is Handshake enabled for dVPN node?",
},
[]string{"address", "moniker", "operator"},
)

handshakePeersGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_handshake_peers",
Help: "dVPN node Handshake peers",
},
[]string{"address", "moniker", "operator"},
)

bandwidthDownloadGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_bandwidth_download",
Help: "dVPN node bandwidth download",
},
[]string{"address", "moniker", "operator"},
)

bandwidthUploadGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_bandwidth_upwnload",
Help: "dVPN node bandwidth download",
},
[]string{"address", "moniker", "operator"},
)

intervalSetSessionsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_interval_set_sessions",
Help: "dVPN node set session interval",
},
[]string{"address", "moniker", "operator"},
)

intervalUpdateSessionsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_interval_update_sessions",
Help: "dVPN node update session interval",
},
[]string{"address", "moniker", "operator"},
)

intervalUpdateStatusGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_interval_update_status",
Help: "dVPN node update status interval",
},
[]string{"address", "moniker", "operator"},
)

peersConnectedGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_peers_connected",
Help: "dVPN peers connected",
},
[]string{"address", "moniker", "operator"},
)

peersMaxGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_peers_max",
Help: "dVPN peers max",
},
[]string{"address", "moniker", "operator"},
)

priceGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dvpn_price",
Help: "dVPN price per GB",
},
[]string{"address", "moniker", "operator", "denom"},
)

registry = prometheus.NewRegistry()
)

func HandlerGetMetrics(ctx *context.Context) http.HandlerFunc {
registry.MustRegister(nodeInfoGauge)
registry.MustRegister(locationInfoGauge)
registry.MustRegister(handshakeEnabledGauge)
registry.MustRegister(handshakePeersGauge)
registry.MustRegister(bandwidthDownloadGauge)
registry.MustRegister(bandwidthUploadGauge)
registry.MustRegister(intervalSetSessionsGauge)
registry.MustRegister(intervalUpdateSessionsGauge)
registry.MustRegister(intervalUpdateStatusGauge)
registry.MustRegister(peersConnectedGauge)

return func(w http.ResponseWriter, r *http.Request) {
nodeInfoGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
"version": version.Version,
}).Set(1)

locationInfoGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
"city": ctx.Location().City,
"country": ctx.Location().Country,
"latitude": fmt.Sprintf("%f", ctx.Location().Latitude),
"longitude": fmt.Sprintf("%f", ctx.Location().Longitude),
}).Set(1)

var handshakeEnabled float64 = 0
if ctx.Config().Handshake.Enable {
handshakeEnabled = 1
}

handshakeEnabledGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(handshakeEnabled)

handshakePeersGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(float64(ctx.Config().Handshake.Peers))

bandwidthDownloadGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(float64(ctx.Bandwidth().Download.Int64()))

bandwidthUploadGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(float64(ctx.Bandwidth().Upload.Int64()))

intervalSetSessionsGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(ctx.IntervalSetSessions().Seconds())

intervalUpdateSessionsGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(ctx.IntervalUpdateSessions().Seconds())

intervalUpdateStatusGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(ctx.IntervalUpdateStatus().Seconds())

peersConnectedGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(float64(ctx.Service().PeersCount()))

peersMaxGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
}).Set(float64(ctx.Config().QOS.MaxPeers))

for _, coin := range ctx.Price() {
priceGauge.With(prometheus.Labels{
"address": ctx.Address().String(),
"moniker": ctx.Moniker(),
"operator": ctx.Operator().String(),
"denom": coin.Denom,
}).Set(float64(coin.Amount.Int64()))
}

promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
EnableOpenMetrics: true,
},
).ServeHTTP(w, r)
}
}
15 changes: 15 additions & 0 deletions rest/metrics/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package metrics

import (
"net/http"

"github.com/gorilla/mux"

"github.com/sentinel-official/dvpn-node/context"
)

func RegisterRoutes(ctx *context.Context, r *mux.Router) {
r.Name("GetMetrics").
Methods(http.MethodGet).Path("/metrics").
HandlerFunc(HandlerGetMetrics(ctx))
}
2 changes: 2 additions & 0 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"github.com/gorilla/mux"

"github.com/sentinel-official/dvpn-node/context"
"github.com/sentinel-official/dvpn-node/rest/metrics"
"github.com/sentinel-official/dvpn-node/rest/session"
"github.com/sentinel-official/dvpn-node/rest/status"
)

func RegisterRoutes(ctx *context.Context, r *mux.Router) {
session.RegisterRoutes(ctx, r)
status.RegisterRoutes(ctx, r)
metrics.RegisterRoutes(ctx, r)
}