From 213c56c8bceffe44409bb0ed41d520f715e9c038 Mon Sep 17 00:00:00 2001 From: Ruixian Song Date: Mon, 10 Apr 2023 15:20:06 -0700 Subject: [PATCH] Add functions for calculating label propagation metrics --- pkg/neg/metrics/label_propagation_metrics.go | 17 +++- pkg/neg/metrics/metrics_test.go | 96 ++++++++++++++++++++ pkg/neg/metrics/neg_metrics_collector.go | 13 +++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 pkg/neg/metrics/metrics_test.go diff --git a/pkg/neg/metrics/label_propagation_metrics.go b/pkg/neg/metrics/label_propagation_metrics.go index 0cc769e15f..9932210649 100644 --- a/pkg/neg/metrics/label_propagation_metrics.go +++ b/pkg/neg/metrics/label_propagation_metrics.go @@ -50,6 +50,8 @@ var ( Subsystem: negControllerSubsystem, Name: labelNumber, Help: "The number of labels per endpoint", + // custom buckets - [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, +Inf] + Buckets: prometheus.ExponentialBuckets(1, 2, 13), }, ) @@ -57,7 +59,9 @@ var ( prometheus.HistogramOpts{ Subsystem: negControllerSubsystem, Name: annotationSize, - Help: "The size of endpoint annotations per endpoint", + Help: "The size in byte of endpoint annotations per endpoint", + // custom buckets - [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, +Inf] + Buckets: prometheus.ExponentialBuckets(1, 2, 13), }, ) @@ -82,3 +86,14 @@ type LabelPropagationMetrics struct { EndpointsWithAnnotation int NumberOfEndpoints int } + +// PublishLabelPropagationError publishes error occured during label propagation. +func PublishLabelPropagationError(errType string) { + LabelPropagationError.WithLabelValues(errType).Inc() +} + +// PublishAnnotationMetrics publishes collected metrics for endpoint annotations. +func PublishAnnotationMetrics(annotationSize int, labelNumber int) { + AnnotationSize.Observe(float64(annotationSize)) + LabelNumber.Observe(float64(labelNumber)) +} diff --git a/pkg/neg/metrics/metrics_test.go b/pkg/neg/metrics/metrics_test.go new file mode 100644 index 0000000000..2c4075281f --- /dev/null +++ b/pkg/neg/metrics/metrics_test.go @@ -0,0 +1,96 @@ +/* +Copyright 2023 The Kubernetes Authors. + +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 ( + "testing" + "time" + + "github.com/google/go-cmp/cmp" + negtypes "k8s.io/ingress-gce/pkg/neg/types" + "k8s.io/klog/v2" +) + +func TestComputeLabelMetrics(t *testing.T) { + collector := NewNegMetricsCollector(10*time.Second, klog.TODO()) + syncer1 := negtypes.NegSyncerKey{ + Namespace: "ns1", + Name: "svc-1", + NegName: "neg-1", + NegType: negtypes.VmIpPortEndpointType, + EpCalculatorMode: negtypes.L7Mode, + } + syncer2 := negtypes.NegSyncerKey{ + Namespace: "ns1", + Name: "svc-2", + NegName: "neg-2", + NegType: negtypes.VmIpPortEndpointType, + EpCalculatorMode: negtypes.L7Mode, + } + for _, tc := range []struct { + desc string + syncerLabelProagationStats map[negtypes.NegSyncerKey]LabelPropagationStats + expect LabelPropagationMetrics + }{ + { + desc: "Empty Data", + syncerLabelProagationStats: map[negtypes.NegSyncerKey]LabelPropagationStats{ + syncer1: {}, + }, + expect: LabelPropagationMetrics{ + EndpointsWithAnnotation: 0, + NumberOfEndpoints: 0, + }, + }, + { + desc: "All endpoints have annotations", + syncerLabelProagationStats: map[negtypes.NegSyncerKey]LabelPropagationStats{ + syncer1: { + EndpointsWithAnnotation: 10, + NumberOfEndpoints: 10, + }, + }, + expect: LabelPropagationMetrics{ + EndpointsWithAnnotation: 10, + NumberOfEndpoints: 10, + }, + }, + { + desc: "Test with 2 syncers", + syncerLabelProagationStats: map[negtypes.NegSyncerKey]LabelPropagationStats{ + syncer1: { + EndpointsWithAnnotation: 10, + NumberOfEndpoints: 10, + }, + syncer2: { + EndpointsWithAnnotation: 5, + NumberOfEndpoints: 10, + }, + }, + expect: LabelPropagationMetrics{ + EndpointsWithAnnotation: 15, + NumberOfEndpoints: 20, + }, + }, + } { + collector.syncerLabelProagationStats = tc.syncerLabelProagationStats + out := collector.computeLabelMetrics() + if diff := cmp.Diff(out, tc.expect); diff != "" { + t.Errorf("For test case %s, got %+v, want %+v, diff: %s", tc.desc, out, tc.expect, diff) + } + } +} diff --git a/pkg/neg/metrics/neg_metrics_collector.go b/pkg/neg/metrics/neg_metrics_collector.go index 034159f843..2da126a4b8 100644 --- a/pkg/neg/metrics/neg_metrics_collector.go +++ b/pkg/neg/metrics/neg_metrics_collector.go @@ -108,3 +108,16 @@ func (sm *SyncerMetrics) SetSyncerEPMetrics(key negtypes.NegSyncerKey, endpointS } sm.syncerEPSStateMap[key] = endpointStat.EndpointSliceStateCount } + +// computeLabelMetrics aggregates label propagation metrics. +func (sm *SyncerMetrics) computeLabelMetrics() LabelPropagationMetrics { + sm.mu.Lock() + defer sm.mu.Unlock() + + lpMetrics := LabelPropagationMetrics{} + for _, stats := range sm.syncerLabelProagationStats { + lpMetrics.EndpointsWithAnnotation += stats.EndpointsWithAnnotation + lpMetrics.NumberOfEndpoints += stats.NumberOfEndpoints + } + return lpMetrics +}