Skip to content

Commit

Permalink
Add an option to expose Prometheus metrics via http/s server
Browse files Browse the repository at this point in the history
Signed-off-by: Botond Szirtes <botond.szirtes@est.tech>
  • Loading branch information
bszirtes committed Aug 21, 2024
1 parent a7b2adb commit 0fc738e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/networkservicemesh/sdk v0.5.1-0.20240820090035-6fad31a9f0aa
github.com/networkservicemesh/sdk-kernel v0.0.0-20240820090342-573b7f288d21
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74
Expand Down Expand Up @@ -52,7 +53,6 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/open-policy-agent/opa v0.44.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
Expand Down
18 changes: 18 additions & 0 deletions pkg/networkservice/metrics/stats/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/prometheus"
"github.com/pkg/errors"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
Expand Down Expand Up @@ -67,6 +68,23 @@ func retrieveMetrics(ctx context.Context, statsConn *core.StatsConnection, segme
segment.Metrics[addName+"rx_packets"] = strconv.FormatUint(iface.Rx.Packets, 10)
segment.Metrics[addName+"tx_packets"] = strconv.FormatUint(iface.Tx.Packets, 10)
segment.Metrics[addName+"drops"] = strconv.FormatUint(iface.Drops, 10)

if prometheus.IsEnabled() {
if addName == "server_" {
ServerRxBytes.Set(float64(iface.Rx.Bytes))
ServerTxBytes.Set(float64(iface.Tx.Bytes))
ServerRxPackets.Set(float64(iface.Rx.Packets))
ServerTxPackets.Set(float64(iface.Tx.Packets))
ServerDrops.Set(float64(iface.Drops))
} else {
ClientRxBytes.Set(float64(iface.Rx.Bytes))
ClientTxBytes.Set(float64(iface.Tx.Bytes))
ClientRxPackets.Set(float64(iface.Rx.Packets))
ClientTxPackets.Set(float64(iface.Tx.Packets))
ClientDrops.Set(float64(iface.Drops))
}
}

break
}
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/networkservice/metrics/stats/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package stats

Check failure on line 1 in pkg/networkservice/metrics/stats/prometheus.go

View workflow job for this annotation

GitHub Actions / golangci-lint / golangci-lint

Missed header for check (goheader)

import (
prom "github.com/networkservicemesh/sdk/pkg/tools/prometheus"
"github.com/prometheus/client_golang/prometheus"
)

var (
ClientRxBytes = prometheus.NewGauge(

Check warning on line 9 in pkg/networkservice/metrics/stats/prometheus.go

View workflow job for this annotation

GitHub Actions / golangci-lint / golangci-lint

exported: exported var ClientRxBytes should have comment or be unexported (revive)
prometheus.GaugeOpts{
Name: "client_rx_bytes_total",
Help: "Total received bytes by client",
},
)
ClientTxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_tx_bytes_total",
Help: "Total transmitted bytes by client",
},
)
ClientRxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_rx_packets_total",
Help: "Total received packets by client",
},
)
ClientTxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_tx_packets_total",
Help: "Total transmitted packets by client",
},
)
ClientDrops = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_drops_total",
Help: "Total drops by client",
},
)
ServerRxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_rx_bytes_total",
Help: "Total received bytes by server",
},
)
ServerTxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_tx_bytes_total",
Help: "Total transmitted bytes by server",
},
)

ServerRxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_rx_packets_total",
Help: "Total received packets by server",
},
)

ServerTxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_tx_packets_total",
Help: "Total transmitted packets by server",
},
)

ServerDrops = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_drops_total",
Help: "Total drops by server",
},
)
)

func init() {

Check failure on line 74 in pkg/networkservice/metrics/stats/prometheus.go

View workflow job for this annotation

GitHub Actions / golangci-lint / golangci-lint

don't use `init` function (gochecknoinits)
if prom.IsEnabled() {
prometheus.MustRegister(ClientRxBytes)
prometheus.MustRegister(ClientTxBytes)
prometheus.MustRegister(ClientRxPackets)
prometheus.MustRegister(ClientTxPackets)
prometheus.MustRegister(ClientDrops)
prometheus.MustRegister(ServerRxBytes)
prometheus.MustRegister(ServerTxBytes)
prometheus.MustRegister(ServerRxPackets)
prometheus.MustRegister(ServerTxPackets)
prometheus.MustRegister(ServerDrops)
}
}

0 comments on commit 0fc738e

Please sign in to comment.