Skip to content

Commit

Permalink
Add functions for calculating label propagation metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixiansong committed Apr 14, 2023
1 parent 6ca59bf commit 213c56c
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/neg/metrics/label_propagation_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ 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),
},
)

AnnotationSize = prometheus.NewHistogram(
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),
},
)

Expand All @@ -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))
}
96 changes: 96 additions & 0 deletions pkg/neg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
13 changes: 13 additions & 0 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 213c56c

Please sign in to comment.