From 711ab40cb54b50375f93d6c46edbf0f587d5e7de Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Thu, 30 Dec 2021 13:31:00 +1100 Subject: [PATCH] delete meter_helper Signed-off-by: Nikita Skrynnik --- pkg/networkservice/common/metrics/server.go | 30 ++++++-- .../opentelemetry/meterhelper/meter_helper.go | 74 ------------------- 2 files changed, 23 insertions(+), 81 deletions(-) delete mode 100644 pkg/tools/opentelemetry/meterhelper/meter_helper.go diff --git a/pkg/networkservice/common/metrics/server.go b/pkg/networkservice/common/metrics/server.go index 77f9cae6f6..45c07667d0 100644 --- a/pkg/networkservice/common/metrics/server.go +++ b/pkg/networkservice/common/metrics/server.go @@ -19,22 +19,28 @@ 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" - "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry/meterhelper" + "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" ) type metricServer struct { - helpers map[string]meterhelper.MeterHelper + recorderMap map[string]metric.Int64Histogram + meter metric.Meter } // NewServer returns a new metric server chain element func NewServer() networkservice.NetworkServiceServer { return &metricServer{ - helpers: make(map[string]meterhelper.MeterHelper), + recorderMap: make(map[string]metric.Int64Histogram), + meter: global.Meter(opentelemetry.InstrumentationName), } } @@ -64,11 +70,21 @@ func (t *metricServer) writeMetrics(ctx context.Context, path *networkservice.Pa if pathSegment.Metrics == nil { continue } - _, ok := t.helpers[pathSegment.Id] - if !ok { - t.helpers[pathSegment.Id] = meterhelper.NewMeterHelper(pathSegment.Name, path.GetPathSegments()[0].Id) + + 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)) } - t.helpers[pathSegment.Id].WriteMetrics(ctx, pathSegment.Metrics) } } } diff --git a/pkg/tools/opentelemetry/meterhelper/meter_helper.go b/pkg/tools/opentelemetry/meterhelper/meter_helper.go deleted file mode 100644 index 45ae2da729..0000000000 --- a/pkg/tools/opentelemetry/meterhelper/meter_helper.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2021 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 meterhelper provides a set of utilities to assist in working with opentelemetry metrics -package meterhelper - -import ( - "context" - "strconv" - - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/global" - - "github.com/networkservicemesh/sdk/pkg/tools/log" - "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" -) - -// MeterHelper - wrap opentelemetry Meter to simplify workflow -type MeterHelper interface { - WriteMetrics(ctx context.Context, metrics map[string]string) -} - -type meterHelper struct { - prefix string - connLabel attribute.KeyValue - meter metric.Meter - recorderMap map[string]metric.Int64Histogram -} - -// NewMeterHelper - constructs a meter helper from segmentName and connectionID. -func NewMeterHelper(segmentName, connectionID string) MeterHelper { - meter := global.Meter(opentelemetry.InstrumentationName) - return &meterHelper{ - prefix: segmentName + "_", - connLabel: attribute.String("connection", connectionID), - meter: meter, - recorderMap: make(map[string]metric.Int64Histogram), - } -} - -func (m *meterHelper) WriteMetrics(ctx context.Context, metrics map[string]string) { - if metrics == nil || !log.IsOpentelemetryEnabled() { - return - } - - for metricName, metricValue := range metrics { - /* Works with integers only */ - recVal, err := strconv.ParseInt(metricValue, 10, 64) - if err != nil { - continue - } - _, ok := m.recorderMap[metricName] - if !ok { - m.recorderMap[metricName] = metric.Must(m.meter).NewInt64Histogram( - m.prefix + metricName, - ) - } - m.recorderMap[metricName].Record(ctx, recVal, m.connLabel) - } -}