Skip to content

Commit

Permalink
Fix metrics concurrency
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art committed Jun 16, 2022
1 parent 6abdf1e commit 0ae9660
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
33 changes: 33 additions & 0 deletions pkg/networkservice/common/metrics/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022 Cisco 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

import (
"context"

"go.opentelemetry.io/otel/metric"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type keyType struct{}
type metricsMap map[string]metric.Int64Histogram

func loadOrStore(ctx context.Context, metrics metricsMap) (value metricsMap, ok bool) {
rawValue, ok := metadata.Map(ctx, false).LoadOrStore(keyType{}, metrics)
return rawValue.(metricsMap), ok
}
13 changes: 6 additions & 7 deletions pkg/networkservice/common/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ import (
)

type metricServer struct {
recorderMap map[string]metric.Int64Histogram
meter metric.Meter
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(""),
meter: global.Meter(""),
}
}

Expand Down Expand Up @@ -70,19 +68,20 @@ func (t *metricServer) writeMetrics(ctx context.Context, path *networkservice.Pa
continue
}

metrics, _ := loadOrStore(ctx, make(map[string]metric.Int64Histogram))
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]
_, ok := metrics[metricName]
if !ok {
t.recorderMap[metricName] = metric.Must(t.meter).NewInt64Histogram(
metrics[metricName] = metric.Must(t.meter).NewInt64Histogram(
pathSegment.Name + "_" + metricName,
)
}
t.recorderMap[metricName].Record(ctx, recVal, attribute.String("connection", path.GetPathSegments()[0].Id))
metrics[metricName].Record(ctx, recVal, attribute.String("connection", path.GetPathSegments()[0].Id))
}
}
}
Expand Down

0 comments on commit 0ae9660

Please sign in to comment.