Skip to content

Commit f387bd2

Browse files
ILL1Achancez
authored andcommitted
metrics: Add metric for k8s resource sync duration
This change adds a new metric to track the sync duration of individual k8s resources. Right now, even using logs, it is almost impossible to determine exactly how long a specific resource takes to sync. Having this metric is useful because resource syncing can account for a significant portion of the agent startup time. To give an example, at scale, CiliumNode sync is often the reason for hive startup hitting the timeout. This metric would allow to monitor regressions and provide a clear distribution of sync timings. This commit introduces the new `kubernetes_resource_sync_duration` gauge metric, labeled by scope, where scope represents the resource name. Signed-off-by: Illia Kolisnyk <illiakolisnyk@google.com>
1 parent b0f5029 commit f387bd2

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

Documentation/observability/metrics.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ Kubernetes
680680
=========================================== ================================================== ========== ========================================================
681681
Name Labels Default Description
682682
=========================================== ================================================== ========== ========================================================
683+
``kubernetes_resource_sync_duration`` ``scope`` Enabled Duration in seconds of a specific Kubernetes resource sync
683684
``kubernetes_events_received_total`` ``scope``, ``action``, ``validity``, ``equal`` Enabled Number of Kubernetes events received
684685
``kubernetes_events_total`` ``scope``, ``action``, ``outcome`` Enabled Number of Kubernetes events processed
685686
``k8s_cnp_status_completion_seconds`` ``attempts``, ``outcome`` Enabled Duration in seconds in how long it took to complete a CNP status update

Documentation/operations/upgrade.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ Added Metrics
370370
proxy redirects that were missing during endpoint policy calculation.
371371
* ``cilium_endpoint_component_status`` was added and reports the number of endpoints
372372
tagged by the status (``OK``, ``Warning``, ``Failure``) of each component (``BPF``, ``Policy``).
373+
* ``cilium_kubernetes_resource_sync_duration`` was added and reports duration in seconds
374+
of a specific Kubernetes resource sync.
373375

374376
Changed Metrics
375377
###############

pkg/k8s/resource/resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/cilium/cilium/pkg/lock"
2929
"github.com/cilium/cilium/pkg/metrics"
3030
"github.com/cilium/cilium/pkg/promise"
31+
"github.com/cilium/cilium/pkg/spanstat"
3132
)
3233

3334
// Resource provides access to a Kubernetes resource through either
@@ -143,6 +144,7 @@ func New[T k8sRuntime.Object](lc cell.Lifecycle, lw cache.ListerWatcher, mp work
143144
needed: make(chan struct{}, 1),
144145
lw: lw,
145146
metricsProvider: mp,
147+
duration: spanstat.Start(),
146148
}
147149
r.opts.sourceObj = func() k8sRuntime.Object {
148150
var obj T
@@ -242,6 +244,8 @@ type resource[T k8sRuntime.Object] struct {
242244
storeResolver promise.Resolver[Store[T]]
243245

244246
metricsProvider workqueue.MetricsProvider
247+
248+
duration *spanstat.SpanStat
245249
}
246250

247251
var _ Resource[*corev1.Node] = &resource[*corev1.Node]{}
@@ -276,6 +280,7 @@ func (r *resource[T]) metricEventProcessed(eventKind EventKind, status bool) {
276280
var action string
277281
switch eventKind {
278282
case Sync:
283+
metrics.KubernetesResourceSyncDuration.WithLabelValues(r.opts.metricScope).Set(r.duration.End(status).Total().Seconds())
279284
return
280285
case Upsert:
281286
action = "update"

pkg/metrics/metrics.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ var (
436436

437437
// Kubernetes Events
438438

439+
// KubernetesResourceSyncDuration is the Kubernetes resource sync duration labeled by scope.
440+
KubernetesResourceSyncDuration = NoOpGaugeVec
441+
439442
// KubernetesEventProcessed is the number of Kubernetes events
440443
// processed labeled by scope, action and execution result
441444
KubernetesEventProcessed = NoOpCounterVec
@@ -673,6 +676,7 @@ type LegacyMetrics struct {
673676
ControllerRuns metric.Vec[metric.Counter]
674677
ControllerRunsDuration metric.Vec[metric.Observer]
675678
SubprocessStart metric.Vec[metric.Counter]
679+
KubernetesResourceSyncDuration metric.Vec[metric.Gauge]
676680
KubernetesEventProcessed metric.Vec[metric.Counter]
677681
KubernetesEventReceived metric.Vec[metric.Counter]
678682
KubernetesAPIInteractions metric.Vec[metric.Observer]
@@ -1022,6 +1026,13 @@ func NewLegacyMetrics() *LegacyMetrics {
10221026
Help: "Number of times that Cilium has started a subprocess, labeled by subsystem",
10231027
}, []string{LabelSubsystem}),
10241028

1029+
KubernetesResourceSyncDuration: metric.NewGaugeVec(metric.GaugeOpts{
1030+
ConfigName: Namespace + "_kubernetes_resource_sync_duration",
1031+
Namespace: Namespace,
1032+
Name: "kubernetes_resource_sync_duration",
1033+
Help: "Duration in seconds of a specific Kubernetes resource sync",
1034+
}, []string{LabelScope}),
1035+
10251036
KubernetesEventProcessed: metric.NewCounterVec(metric.CounterOpts{
10261037
ConfigName: Namespace + "_kubernetes_events_total",
10271038
Namespace: Namespace,
@@ -1348,6 +1359,7 @@ func NewLegacyMetrics() *LegacyMetrics {
13481359
ControllerRuns = lm.ControllerRuns
13491360
ControllerRunsDuration = lm.ControllerRunsDuration
13501361
SubprocessStart = lm.SubprocessStart
1362+
KubernetesResourceSyncDuration = lm.KubernetesResourceSyncDuration
13511363
KubernetesEventProcessed = lm.KubernetesEventProcessed
13521364
KubernetesEventReceived = lm.KubernetesEventReceived
13531365
KubernetesAPIInteractions = lm.KubernetesAPIInteractions

0 commit comments

Comments
 (0)