Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label propagation metrics #2076

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions pkg/neg/metrics/label_propagation_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
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 (
"github.com/prometheus/client_golang/prometheus"
)

const (
labelNumber = "label_number_per_endpoint"
annotationSize = "annotation_size_per_endpoint"
labelErrorNumber = "label_propagation_error_count"
numberOfEndpoints = "number_of_endpoints"
)

var (
labelPropagationErrorLabels = []string{
"error_type",
}

endpointAnnotationLabels = []string{
"with_annotation",
}

NumberOfEndpoints = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: negControllerSubsystem,
Name: numberOfEndpoints,
Help: "The total number of endpoints",
},
endpointAnnotationLabels,
)

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

LabelPropagationError = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: negControllerSubsystem,
Name: labelErrorNumber,
Help: "the number of errors occurred for label propagation",
},
labelPropagationErrorLabels,
)
)

// LabelPropagationStat contains stats related to label propagation.
type LabelPropagationStats struct {
EndpointsWithAnnotation int
NumberOfEndpoints int
}

// LabelPropagationMetrics contains aggregated label propagation related metrics.
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)
}
}
}
15 changes: 15 additions & 0 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type SyncerMetrics struct {
syncerEndpointStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap
// syncerEPSStateMap is a map between syncer and endpoint slice state counts
syncerEPSStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap
// syncerLabelProagationStats is a map between syncer and label propagation stats.
syncerLabelProagationStats map[negtypes.NegSyncerKey]LabelPropagationStats
// mu avoid race conditions and ensure correctness of metrics
mu sync.Mutex
// duration between metrics exports
Expand Down Expand Up @@ -106,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 {
swetharepakula marked this conversation as resolved.
Show resolved Hide resolved
sm.mu.Lock()
defer sm.mu.Unlock()

lpMetrics := LabelPropagationMetrics{}
for _, stats := range sm.syncerLabelProagationStats {
lpMetrics.EndpointsWithAnnotation += stats.EndpointsWithAnnotation
lpMetrics.NumberOfEndpoints += stats.NumberOfEndpoints
}
return lpMetrics
}