Merge pull request #102523 from stlaz/rootca_metrics_cleanup

rootcacertpublisher: drop the namespace label from metrics to reduce its cardinality
This commit is contained in:
Kubernetes Prow Robot 2021-09-20 13:54:24 -07:00 committed by GitHub
commit b34a735bbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 11 deletions

View File

@ -37,7 +37,7 @@ var (
Help: "Number of namespace syncs happened in root ca cert publisher.", Help: "Number of namespace syncs happened in root ca cert publisher.",
StabilityLevel: metrics.ALPHA, StabilityLevel: metrics.ALPHA,
}, },
[]string{"namespace", "code"}, []string{"code"},
) )
syncLatency = metrics.NewHistogramVec( syncLatency = metrics.NewHistogramVec(
&metrics.HistogramOpts{ &metrics.HistogramOpts{
@ -47,19 +47,19 @@ var (
Buckets: metrics.ExponentialBuckets(0.001, 2, 15), Buckets: metrics.ExponentialBuckets(0.001, 2, 15),
StabilityLevel: metrics.ALPHA, StabilityLevel: metrics.ALPHA,
}, },
[]string{"namespace", "code"}, []string{"code"},
) )
) )
func recordMetrics(start time.Time, ns string, err error) { func recordMetrics(start time.Time, err error) {
code := "500" code := "500"
if err == nil { if err == nil {
code = "200" code = "200"
} else if se, ok := err.(*apierrors.StatusError); ok && se.Status().Code != 0 { } else if se, ok := err.(*apierrors.StatusError); ok && se.Status().Code != 0 {
code = strconv.Itoa(int(se.Status().Code)) code = strconv.Itoa(int(se.Status().Code))
} }
syncLatency.WithLabelValues(ns, code).Observe(time.Since(start).Seconds()) syncLatency.WithLabelValues(code).Observe(time.Since(start).Seconds())
syncCounter.WithLabelValues(ns, code).Inc() syncCounter.WithLabelValues(code).Inc()
} }
var once sync.Once var once sync.Once

View File

@ -44,7 +44,7 @@ func TestSyncCounter(t *testing.T) {
want: ` want: `
# HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher. # HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher.
# TYPE root_ca_cert_publisher_sync_total counter # TYPE root_ca_cert_publisher_sync_total counter
root_ca_cert_publisher_sync_total{code="200",namespace="test-ns"} 1 root_ca_cert_publisher_sync_total{code="200"} 1
`, `,
}, },
{ {
@ -56,7 +56,7 @@ root_ca_cert_publisher_sync_total{code="200",namespace="test-ns"} 1
want: ` want: `
# HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher. # HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher.
# TYPE root_ca_cert_publisher_sync_total counter # TYPE root_ca_cert_publisher_sync_total counter
root_ca_cert_publisher_sync_total{code="404",namespace="test-ns"} 1 root_ca_cert_publisher_sync_total{code="404"} 1
`, `,
}, },
{ {
@ -68,7 +68,7 @@ root_ca_cert_publisher_sync_total{code="404",namespace="test-ns"} 1
want: ` want: `
# HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher. # HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher.
# TYPE root_ca_cert_publisher_sync_total counter # TYPE root_ca_cert_publisher_sync_total counter
root_ca_cert_publisher_sync_total{code="500",namespace="test-ns"} 1 root_ca_cert_publisher_sync_total{code="500"} 1
`, `,
}, },
{ {
@ -80,14 +80,14 @@ root_ca_cert_publisher_sync_total{code="500",namespace="test-ns"} 1
want: ` want: `
# HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher. # HELP root_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in root ca cert publisher.
# TYPE root_ca_cert_publisher_sync_total counter # TYPE root_ca_cert_publisher_sync_total counter
root_ca_cert_publisher_sync_total{code="500",namespace="test-ns"} 1 root_ca_cert_publisher_sync_total{code="500"} 1
`, `,
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) { t.Run(tc.desc, func(t *testing.T) {
recordMetrics(time.Now(), "test-ns", tc.err) recordMetrics(time.Now(), tc.err)
defer syncCounter.Reset() defer syncCounter.Reset()
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metrics...); err != nil { if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metrics...); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -182,7 +182,7 @@ func (c *Publisher) processNextWorkItem() bool {
func (c *Publisher) syncNamespace(ns string) (err error) { func (c *Publisher) syncNamespace(ns string) (err error) {
startTime := time.Now() startTime := time.Now()
defer func() { defer func() {
recordMetrics(startTime, ns, err) recordMetrics(startTime, err)
klog.V(4).Infof("Finished syncing namespace %q (%v)", ns, time.Since(startTime)) klog.V(4).Infof("Finished syncing namespace %q (%v)", ns, time.Since(startTime))
}() }()