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

Add interface names into NSM metrics #1560

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions pkg/networkservice/common/mechanisms/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -28,13 +28,24 @@ import (
)

type mechanismsClient struct {
mechanisms map[string]networkservice.NetworkServiceClient
mechanisms map[string]networkservice.NetworkServiceClient
withMetrics bool
}

// NewClient - returns a new mechanisms networkservicemesh.NetworkServiceClient
func NewClient(mechanisms map[string]networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
return newClient(mechanisms, false)
}

// NewClientWithMetrics - same as NewClient, but will also print the interface type/name metric to Path
func NewClientWithMetrics(mechanisms map[string]networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
return newClient(mechanisms, true)
}

func newClient(mechanisms map[string]networkservice.NetworkServiceClient, withMetrics bool) networkservice.NetworkServiceClient {
result := &mechanismsClient{
mechanisms: make(map[string]networkservice.NetworkServiceClient),
mechanisms: make(map[string]networkservice.NetworkServiceClient),
withMetrics: withMetrics,
}
for m, c := range mechanisms {
result.mechanisms[m] = chain.NewNetworkServiceClient(c)
Expand All @@ -44,9 +55,13 @@ func NewClient(mechanisms map[string]networkservice.NetworkServiceClient) networ
}

func (mc *mechanismsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if request.GetConnection().GetMechanism() != nil {
srv, ok := mc.mechanisms[request.GetConnection().GetMechanism().GetType()]
mech := request.GetConnection().GetMechanism()
if mech != nil {
srv, ok := mc.mechanisms[mech.GetType()]
if ok {
if mc.withMetrics {
storeMetrics(request.GetConnection(), mech, true)
}
return srv.Request(ctx, request, opts...)
}
return nil, errUnsupportedMech
Expand All @@ -57,6 +72,11 @@ func (mc *mechanismsClient) Request(ctx context.Context, request *networkservice
if ok {
req := request.Clone()
var resp *networkservice.Connection

if mc.withMetrics {
storeMetrics(req.GetConnection(), mechanism, true)
}

resp, respErr := cm.Request(ctx, req, opts...)
if respErr == nil {
return resp, nil
Expand All @@ -70,6 +90,9 @@ func (mc *mechanismsClient) Request(ctx context.Context, request *networkservice
func (mc *mechanismsClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
c, ok := mc.mechanisms[conn.GetMechanism().GetType()]
if ok {
if mc.withMetrics {
storeMetrics(conn, conn.GetMechanism(), true)
}
return c.Close(ctx, conn)
}
return nil, errCannotSupportMech
Expand Down
64 changes: 62 additions & 2 deletions pkg/networkservice/common/mechanisms/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -16,7 +16,67 @@

package mechanisms

import "github.com/pkg/errors"
import (
"fmt"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
)

var errCannotSupportMech = errors.New("cannot support any of the requested mechanism")
var errUnsupportedMech = errors.New("unsupported mechanism")

const (
clientMetricKey = "client_interface"
serverMetricKey = "server_interface"
nameKey = "name"
unresolvedName = "unknown"
)

type interfacesInfo struct {
interfaceName string
interfaceType string
}

func (i *interfacesInfo) getInterfaceDetails() string {
return fmt.Sprintf("%s/%s", i.interfaceType, i.interfaceName)
}

// Save interface details in Path
func storeMetrics(conn *networkservice.Connection, mechanism *networkservice.Mechanism, isClient bool) {
path := conn.GetPath()
if path == nil {
return
}

segments := path.GetPathSegments()
if segments == nil {
return
}

segment := segments[path.Index]
params := mechanism.GetParameters()

name := unresolvedName
if params != nil {
name = params[nameKey]
}

info := &interfacesInfo{
interfaceName: name,
interfaceType: mechanism.GetType(),
}

if segment.Metrics == nil {
segment.Metrics = make(map[string]string)
}

metricKey := clientMetricKey
if !isClient {
metricKey = serverMetricKey
}

if _, ok := segment.Metrics[metricKey]; !ok {
segment.Metrics[metricKey] = info.getInterfaceDetails()
}
}
29 changes: 25 additions & 4 deletions pkg/networkservice/common/mechanisms/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
)

type mechanismsServer struct {
mechanisms map[string]networkservice.NetworkServiceServer // key is Mechanism.Type
mechanisms map[string]networkservice.NetworkServiceServer // key is Mechanism.Type
withMetrics bool
}

// NewServer - returns new NetworkServiceServer chain element that will attempt to meet the request.MechanismPreferences using
Expand All @@ -42,8 +43,18 @@ type mechanismsServer struct {
// value: NetworkServiceServer that only handles the work for the specified mechanismType
// Note: Supplied NetworkServiceServer elements should not call next.Server(ctx).{Request,Close} themselves
func NewServer(mechanisms map[string]networkservice.NetworkServiceServer) networkservice.NetworkServiceServer {
return newServer(mechanisms, false)
}

// NewServerWithMetrics - same as NewServer, but will also print the interface type/name metric to Path
func NewServerWithMetrics(mechanisms map[string]networkservice.NetworkServiceServer) networkservice.NetworkServiceServer {
return newServer(mechanisms, true)
}

func newServer(mechanisms map[string]networkservice.NetworkServiceServer, withMetrics bool) networkservice.NetworkServiceServer {
rv := &mechanismsServer{
mechanisms: make(map[string]networkservice.NetworkServiceServer),
mechanisms: make(map[string]networkservice.NetworkServiceServer),
withMetrics: withMetrics,
}
for mechanismType, server := range mechanisms {
// We wrap in a chain here to make sure that if the 'server' is calling next.Server(ctx) it doesn't
Expand All @@ -54,9 +65,13 @@ func NewServer(mechanisms map[string]networkservice.NetworkServiceServer) networ
}

func (ms *mechanismsServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if request.GetConnection().GetMechanism() != nil {
srv, ok := ms.mechanisms[request.GetConnection().GetMechanism().GetType()]
mech := request.GetConnection().GetMechanism()
if mech != nil {
srv, ok := ms.mechanisms[mech.GetType()]
if ok {
if ms.withMetrics {
storeMetrics(request.GetConnection(), mech, false)
}
return srv.Request(ctx, request)
}
return nil, errors.WithStack(errUnsupportedMech)
Expand All @@ -70,6 +85,9 @@ func (ms *mechanismsServer) Request(ctx context.Context, request *networkservice
var resp *networkservice.Connection
resp, respErr := srv.Request(ctx, req)
if respErr == nil {
if ms.withMetrics {
storeMetrics(resp, resp.GetMechanism(), false)
}
return resp, nil
}
err = errors.Wrap(err, respErr.Error())
Expand All @@ -81,6 +99,9 @@ func (ms *mechanismsServer) Request(ctx context.Context, request *networkservice
func (ms *mechanismsServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
srv, ok := ms.mechanisms[conn.GetMechanism().GetType()]
if ok {
if ms.withMetrics {
storeMetrics(conn, conn.GetMechanism(), false)
}
return srv.Close(ctx, conn)
}
return nil, errCannotSupportMech
Expand Down
Loading