Skip to content

Commit

Permalink
Add an option to expose Prometheus metrics via http/s server (#839)
Browse files Browse the repository at this point in the history
Signed-off-by: Botond Szirtes <botond.szirtes@est.tech>
  • Loading branch information
bszirtes authored Aug 27, 2024
1 parent c77c50e commit 441b551
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
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
1 change: 1 addition & 0 deletions pkg/networkservice/metrics/stats/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type statsClient struct {

// NewClient provides a NetworkServiceClient chain elements that retrieves vpp interface metrics.
func NewClient(ctx context.Context, options ...Option) networkservice.NetworkServiceClient {
prometheusInitOnce.Do(registerMetrics)
opts := &statsOptions{}
for _, opt := range options {
opt(opts)
Expand Down
22 changes: 21 additions & 1 deletion pkg/networkservice/metrics/stats/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ 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"
)

const serverPref string = "server_"

// Save retrieved vpp interface metrics in pathSegment
func retrieveMetrics(ctx context.Context, statsConn *core.StatsConnection, segment *networkservice.PathSegment, isClient bool) {
swIfIndex, ok := ifindex.Load(ctx, isClient)
Expand All @@ -49,7 +52,7 @@ func retrieveMetrics(ctx context.Context, statsConn *core.StatsConnection, segme
return
}

addName := "server_"
addName := serverPref
if isClient {
addName = "client_"
}
Expand All @@ -67,6 +70,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 == serverPref {
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
114 changes: 114 additions & 0 deletions pkg/networkservice/metrics/stats/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2024 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package stats

import (
"sync"

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

var (
prometheusInitOnce sync.Once

// ClientRxBytes - Total received bytes by client
ClientRxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_rx_bytes_total",
Help: "Total received bytes by client",
},
)
// ClientTxBytes - Total transmitted bytes by client
ClientTxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_tx_bytes_total",
Help: "Total transmitted bytes by client",
},
)
// ClientRxPackets - Total received packets by client
ClientRxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_rx_packets_total",
Help: "Total received packets by client",
},
)
// ClientTxPackets - Total transmitted packets by client
ClientTxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_tx_packets_total",
Help: "Total transmitted packets by client",
},
)
// ClientDrops - Total drops by client
ClientDrops = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "client_drops_total",
Help: "Total drops by client",
},
)
// ServerRxBytes - Total received bytes by server
ServerRxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_rx_bytes_total",
Help: "Total received bytes by server",
},
)
// ServerTxBytes - Total transmitted bytes by server
ServerTxBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_tx_bytes_total",
Help: "Total transmitted bytes by server",
},
)
// ServerRxPackets - Total received packets by server
ServerRxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_rx_packets_total",
Help: "Total received packets by server",
},
)
// ServerTxPackets - Total transmitted packets by server
ServerTxPackets = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_tx_packets_total",
Help: "Total transmitted packets by server",
},
)
// ServerDrops - Total drops by server
ServerDrops = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "server_drops_total",
Help: "Total drops by server",
},
)
)

func registerMetrics() {
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)
}
}
1 change: 1 addition & 0 deletions pkg/networkservice/metrics/stats/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type statsServer struct {

// NewServer provides a NetworkServiceServer chain elements that retrieves vpp interface statistics.
func NewServer(ctx context.Context, options ...Option) networkservice.NetworkServiceServer {
prometheusInitOnce.Do(registerMetrics)
opts := &statsOptions{}
for _, opt := range options {
opt(opts)
Expand Down

0 comments on commit 441b551

Please sign in to comment.