github.com/cilium/cilium@v1.16.2/pkg/kvstore/metrics.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package kvstore 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/cilium/cilium/pkg/metrics" 11 "github.com/cilium/cilium/pkg/time" 12 ) 13 14 const ( 15 metricDelete = "delete" 16 metricRead = "read" 17 metricSet = "set" 18 ) 19 20 func GetScopeFromKey(key string) string { 21 s := strings.SplitN(key, "/", 5) 22 if len(s) < 4 { 23 if len(key) >= 12 { 24 return key[:12] 25 } 26 return key 27 } 28 return fmt.Sprintf("%s/%s", s[2], s[3]) 29 } 30 31 func increaseMetric(key, kind, action string, duration time.Duration, err error) { 32 if !metrics.KVStoreOperationsDuration.IsEnabled() { 33 return 34 } 35 namespace := GetScopeFromKey(key) 36 outcome := metrics.Error2Outcome(err) 37 metrics.KVStoreOperationsDuration. 38 WithLabelValues(namespace, kind, action, outcome).Observe(duration.Seconds()) 39 } 40 41 func trackEventQueued(scope string, typ EventType, duration time.Duration) { 42 if !metrics.KVStoreEventsQueueDuration.IsEnabled() { 43 return 44 } 45 metrics.KVStoreEventsQueueDuration.WithLabelValues(scope, typ.String()).Observe(duration.Seconds()) 46 } 47 48 func recordQuorumError(err string) { 49 if !metrics.KVStoreQuorumErrors.IsEnabled() { 50 return 51 } 52 metrics.KVStoreQuorumErrors.WithLabelValues(err).Inc() 53 }