github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/kvstore/metrics.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kvstore
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/cilium/cilium/pkg/metrics"
    23  	"github.com/cilium/cilium/pkg/option"
    24  )
    25  
    26  const (
    27  	metricDelete = "delete"
    28  	metricRead   = "read"
    29  	metricSet    = "set"
    30  )
    31  
    32  func getScopeFromKey(key string) string {
    33  	s := strings.SplitN(key, "/", 5)
    34  	if len(s) != 5 {
    35  		if len(key) >= 12 {
    36  			return key[:12]
    37  		}
    38  		return key
    39  	}
    40  	return fmt.Sprintf("%s/%s", s[2], s[3])
    41  }
    42  
    43  func increaseMetric(key, kind, action string, duration time.Duration, err error) {
    44  	if !option.Config.MetricsConfig.KVStoreOperationsDurationEnabled {
    45  		return
    46  	}
    47  	namespace := getScopeFromKey(key)
    48  	outcome := metrics.Error2Outcome(err)
    49  	metrics.KVStoreOperationsDuration.
    50  		WithLabelValues(namespace, kind, action, outcome).Observe(duration.Seconds())
    51  }
    52  
    53  func trackEventQueued(key string, typ EventType, duration time.Duration) {
    54  	if !option.Config.MetricsConfig.KVStoreEventsQueueDurationEnabled {
    55  		return
    56  	}
    57  	metrics.KVStoreEventsQueueDuration.WithLabelValues(getScopeFromKey(key), typ.String()).Observe(duration.Seconds())
    58  }