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 <[email protected]>
  • Loading branch information
bszirtes committed Aug 26, 2024
1 parent a7b2adb commit 19a731f
Show file tree
Hide file tree
Showing 5 changed files with 130 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
106 changes: 106 additions & 0 deletions pkg/networkservice/metrics/stats/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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 = prometheus.NewGauge(

Check warning on line 28 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 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 19a731f

Please sign in to comment.