Skip to content

Commit

Permalink
Add opentelemetry support (#1205)
Browse files Browse the repository at this point in the history
* Update protobuf to v1.26.0 needed for opentelemetry

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>

* Add opentelemetry support

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>

* fixes

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* update dependencies

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fixes due to version update

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add jaeger exporter

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* goleaks disappeared somewhere

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* minor fixes for jaeger

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add zipkin exporter

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* go leaks related to transfer of the spans

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add opentelemetry collector support

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add metrics support

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add metrics test

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add README.md file

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fixes after cherry-pick

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* more fixes

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* run go mod tidy

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* lint fixes

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* revert test and updatepath files

Signed-off-by: Nikita.Skrynnik <nikita.nstu@gmail.com>

* revert refresh/client.go file

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* delete meter_helper

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fill empty event names + delete instrumentationname

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* Revert "revert refresh/client.go file"

This reverts commit d5cd9f9.

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* remove opentracing

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix linter issue

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* change TELEMETRY env value

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

Co-authored-by: Artem Glazychev <artem.glazychev@xored.com>
Co-authored-by: Nikita.Skrynnik <nikita.nstu@gmail.com>
  • Loading branch information
3 people authored Jan 12, 2022
1 parent fe9702d commit b112919
Show file tree
Hide file tree
Showing 47 changed files with 828 additions and 321 deletions.
36 changes: 21 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@ module github.com/networkservicemesh/sdk
go 1.16

require (
github.com/HdrHistogram/hdrhistogram-go v1.0.1 // indirect
github.com/RoaringBitmap/roaring v0.9.4
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/benbjohnson/clock v1.1.0
github.com/benbjohnson/clock v1.3.0
github.com/edwarnicke/exechelper v1.0.2
github.com/edwarnicke/grpcfd v0.1.1
github.com/edwarnicke/serialize v1.0.7
github.com/fsnotify/fsnotify v1.4.9
github.com/ghodss/yaml v1.0.0
github.com/golang-jwt/jwt/v4 v4.1.0
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.4
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/nats-io/nats-streaming-server v0.22.1
github.com/nats-io/stan.go v0.10.0
github.com/networkservicemesh/api v1.0.1-0.20211110183123-3038992da61a
github.com/open-policy-agent/opa v0.16.1
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/sirupsen/logrus v1.8.1
github.com/spiffe/go-spiffe/v2 v2.0.0-alpha.4.0.20200528145730-dc11d0c74e85
github.com/stretchr/testify v1.7.0
github.com/uber/jaeger-client-go v2.21.1+incompatible
github.com/uber/jaeger-lib v2.4.0+incompatible // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.27.0
go.opentelemetry.io/otel v1.3.0
go.opentelemetry.io/otel/exporters/jaeger v1.3.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.26.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.26.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.3.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.3.0
go.opentelemetry.io/otel/exporters/zipkin v1.3.0
go.opentelemetry.io/otel/metric v0.26.0
go.opentelemetry.io/otel/sdk v1.3.0
go.opentelemetry.io/otel/sdk/metric v0.26.0
go.opentelemetry.io/otel/trace v1.3.0
go.uber.org/atomic v1.7.0
go.uber.org/goleak v1.1.10
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/tools v0.1.2 // indirect
go.uber.org/goleak v1.1.12
gonum.org/v1/gonum v0.6.2
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v2 v2.2.2
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12 // indirect
google.golang.org/grpc v1.42.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v2 v2.4.0
)
202 changes: 165 additions & 37 deletions go.sum

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pkg/networkservice/chains/nsmgr/scale_test.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-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -41,6 +41,7 @@ func TestCreateEndpointDuringRequest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)

defer cancel()

domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(2).
SetRegistryProxySupplier(nil).
Expand Down
6 changes: 4 additions & 2 deletions pkg/networkservice/chains/nsmgr/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -40,6 +40,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/excludedprefixes"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/metrics"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/registry"
registryclientinfo "github.com/networkservicemesh/sdk/pkg/registry/common/clientinfo"
Expand Down Expand Up @@ -204,6 +205,7 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options
),
excludedprefixes.NewServer(ctx),
recvfd.NewServer(), // Receive any files passed
metrics.NewServer(),
connect.NewServer(
client.NewClient(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/chains/nsmgr/unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/kernel/vlan/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Nordix Foundation.
// Copyright (c) 2021-2022 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/mechanisms/recvfd/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/recvfd/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/recvfd/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/recvfd/server_test.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-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/sendfd/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/sendfd/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/sendfd/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
89 changes: 89 additions & 0 deletions pkg/networkservice/common/metrics/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// 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 metrics provides a chain element that sends metrics to collector
package metrics

import (
"context"
"strconv"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type metricServer struct {
recorderMap map[string]metric.Int64Histogram
meter metric.Meter
}

// NewServer returns a new metric server chain element
func NewServer() networkservice.NetworkServiceServer {
return &metricServer{
recorderMap: make(map[string]metric.Int64Histogram),
meter: global.Meter(""),
}
}

func (t *metricServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
conn, err := next.Server(ctx).Request(ctx, request)
if err != nil {
return nil, err
}

t.writeMetrics(ctx, conn.GetPath())
return conn, nil
}

func (t *metricServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
_, err := next.Server(ctx).Close(ctx, conn)
if err != nil {
return nil, err
}

t.writeMetrics(ctx, conn.GetPath())
return &empty.Empty{}, nil
}

func (t *metricServer) writeMetrics(ctx context.Context, path *networkservice.Path) {
if path != nil {
for _, pathSegment := range path.GetPathSegments() {
if pathSegment.Metrics == nil {
continue
}

for metricName, metricValue := range pathSegment.Metrics {
/* Works with integers only */
recVal, err := strconv.ParseInt(metricValue, 10, 64)
if err != nil {
continue
}
_, ok := t.recorderMap[metricName]
if !ok {
t.recorderMap[metricName] = metric.Must(t.meter).NewInt64Histogram(
pathSegment.Name + "_" + metricName,
)
}
t.recorderMap[metricName].Record(ctx, recVal, attribute.String("connection", path.GetPathSegments()[0].Id))
}
}
}
}
22 changes: 6 additions & 16 deletions pkg/networkservice/common/refresh/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
//
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -24,9 +24,7 @@ import (
"context"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand Down Expand Up @@ -59,12 +57,7 @@ func (t *refreshClient) Request(ctx context.Context, request *networkservice.Net
}

// Compute refreshAfter
refreshAfter, err := after(ctx, conn)
if err != nil {
// If we can't refresh, we should close down chain
_, _ = t.Close(ctx, conn)
return nil, err
}
refreshAfter := after(ctx, conn)

// Create a cancel context.
cancelCtx, cancel := context.WithCancel(t.chainCtx)
Expand Down Expand Up @@ -105,16 +98,13 @@ func (t *refreshClient) Close(ctx context.Context, conn *networkservice.Connecti
return next.Client(ctx).Close(ctx, conn, opts...)
}

func after(ctx context.Context, conn *networkservice.Connection) (time.Duration, error) {
func after(ctx context.Context, conn *networkservice.Connection) time.Duration {
clockTime := clock.FromContext(ctx)

var minTimeout *time.Duration
var expireTime time.Time
for _, segment := range conn.GetPath().GetPathSegments() {
expTime, err := ptypes.Timestamp(segment.GetExpires())
if err != nil {
return 0, errors.WithStack(err)
}
expTime := segment.GetExpires().AsTime()

timeout := clockTime.Until(expTime)

Expand All @@ -133,7 +123,7 @@ func after(ctx context.Context, conn *networkservice.Connection) (time.Duration,
}

if minTimeout == nil || *minTimeout <= 0 {
return 1, nil
return 1
}

// A heuristic to reduce the number of redundant requests in a chain
Expand All @@ -147,5 +137,5 @@ func after(ctx context.Context, conn *networkservice.Connection) (time.Duration,
}
duration := time.Duration(float64(*minTimeout) * scale)

return duration, nil
return duration
}
4 changes: 2 additions & 2 deletions pkg/networkservice/common/roundrobin/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
// Copyright (c) 2020-2022 Cisco Systems, Inc.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
14 changes: 3 additions & 11 deletions pkg/networkservice/common/updatetoken/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -19,9 +19,8 @@ package updatetoken
import (
"context"

"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/golang/protobuf/ptypes"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -51,15 +50,8 @@ func updateToken(ctx context.Context, conn *networkservice.Connection, tokenGene
return errors.WithStack(err)
}

// Convert the expireTime to proto
var expires *timestamp.Timestamp
expires, err = ptypes.TimestampProto(expireTime)
if err != nil {
return errors.WithStack(err)
}

// Update the PathSegment
path.GetPathSegments()[path.GetIndex()].Token = tok
path.GetPathSegments()[path.GetIndex()].Expires = expires
path.GetPathSegments()[path.GetIndex()].Expires = timestamppb.New(expireTime)
return nil
}
18 changes: 4 additions & 14 deletions pkg/networkservice/common/updatetoken/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco Systems, Inc.
// Copyright (c) 2020-2022 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -20,11 +20,9 @@ package updatetoken
import (
"context"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
Expand All @@ -50,11 +48,7 @@ func (u *updateTokenServer) Request(ctx context.Context, request *networkservice
if err != nil {
log.FromContext(ctx).Warnf("an error during getting token from the context: %+v", err)
} else {
var expires *timestamp.Timestamp
expires, err = ptypes.TimestampProto(expireTime.Local())
if err != nil {
return nil, errors.WithStack(err)
}
expires := timestamppb.New(expireTime.Local())

prev.Expires = expires
prev.Token = tok
Expand All @@ -77,11 +71,7 @@ func (u *updateTokenServer) Close(ctx context.Context, conn *networkservice.Conn
if err != nil {
log.FromContext(ctx).Warnf("an error during getting token from the context: %+v", err)
} else {
var expires *timestamp.Timestamp
expires, err = ptypes.TimestampProto(expireTime.Local())
if err != nil {
return nil, errors.WithStack(err)
}
expires := timestamppb.New(expireTime.Local())

prev.Expires = expires
prev.Token = tok
Expand Down
Loading

0 comments on commit b112919

Please sign in to comment.