From 37d786552e191c9f3275408c398efef00953a9dc Mon Sep 17 00:00:00 2001 From: "Shwejan Raj, Bhupathi" Date: Tue, 17 Jun 2025 16:46:27 +0530 Subject: [PATCH 1/3] adds container_start_time_seconds metric Signed-off-by: Shwejan Raj, Bhupathi --- internal/lib/stats/descriptors.go | 10 ++++++++++ internal/lib/stats/metrics.go | 4 ++++ internal/lib/stats/other_metrics.go | 26 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 internal/lib/stats/other_metrics.go diff --git a/internal/lib/stats/descriptors.go b/internal/lib/stats/descriptors.go index ce1ee0d8805..13e70af1520 100644 --- a/internal/lib/stats/descriptors.go +++ b/internal/lib/stats/descriptors.go @@ -149,3 +149,13 @@ var ( LabelKeys: baseLabelKeys, } ) + + +// other metrics +var ( + containerStartTimeSeconds = &types.MetricDescriptor{ + Name: "container_start_time_seconds", + Help: "Start time of the container since unix epoch", + LabelKeys: baseLabelKeys, + } +) \ No newline at end of file diff --git a/internal/lib/stats/metrics.go b/internal/lib/stats/metrics.go index 2d44b73c722..ab99442b1e8 100644 --- a/internal/lib/stats/metrics.go +++ b/internal/lib/stats/metrics.go @@ -15,6 +15,7 @@ const ( MemoryMetrics = "memory" NetworkMetrics = "network" OOMMetrics = "oom" + OtherMetrics = "other" ) type metricValue struct { @@ -85,6 +86,9 @@ func (ss *StatsServer) PopulateMetricDescriptors(includedKeys []string) map[stri OOMMetrics: { containerOomEventsTotal, }, + OtherMetrics: { + containerStartTimeSeconds, + }, } return descriptorsMap diff --git a/internal/lib/stats/other_metrics.go b/internal/lib/stats/other_metrics.go new file mode 100644 index 00000000000..ec7600a382d --- /dev/null +++ b/internal/lib/stats/other_metrics.go @@ -0,0 +1,26 @@ +package statsserver + +import ( + + types "k8s.io/cri-api/pkg/apis/runtime/v1" + + "github.com/cri-o/cri-o/internal/oci" + "github.com/cri-o/cri-o/internal/lib/sandbox" + +) + +func generateSandboxOtherMetrics(sb *sandbox.Sandbox, others *oci.ContainerState) []*types.Metric { + otherMetrics := []*containerMetric{ + { + desc: containerStartTimeSeconds, + valueFunc: func() metricValues { + return metricValues{{ + value: uint64(others.Started.Unix()) , + metricType: types.MetricType_GAUGE, + }} + }, + }, + } + + return computeSandboxMetrics(sb, otherMetrics, "other") +} \ No newline at end of file From 8d2c1723a656dbe6bb83cd25216c1ee0ccb68853 Mon Sep 17 00:00:00 2001 From: Bhupathi Shwejan Raj Date: Wed, 25 Jun 2025 08:22:25 -0400 Subject: [PATCH 2/3] adds metrics generator fun to stats server and lints the project Signed-off-by: Bhupathi Shwejan Raj --- internal/lib/stats/descriptors.go | 5 ++--- internal/lib/stats/other_metrics.go | 8 +++----- internal/lib/stats/stats_server_linux.go | 3 +++ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/lib/stats/descriptors.go b/internal/lib/stats/descriptors.go index 13e70af1520..cd0883c8323 100644 --- a/internal/lib/stats/descriptors.go +++ b/internal/lib/stats/descriptors.go @@ -150,12 +150,11 @@ var ( } ) - -// other metrics +// other metrics. var ( containerStartTimeSeconds = &types.MetricDescriptor{ Name: "container_start_time_seconds", Help: "Start time of the container since unix epoch", LabelKeys: baseLabelKeys, } -) \ No newline at end of file +) diff --git a/internal/lib/stats/other_metrics.go b/internal/lib/stats/other_metrics.go index ec7600a382d..2cb12e12377 100644 --- a/internal/lib/stats/other_metrics.go +++ b/internal/lib/stats/other_metrics.go @@ -1,12 +1,10 @@ package statsserver import ( - types "k8s.io/cri-api/pkg/apis/runtime/v1" - "github.com/cri-o/cri-o/internal/oci" "github.com/cri-o/cri-o/internal/lib/sandbox" - + "github.com/cri-o/cri-o/internal/oci" ) func generateSandboxOtherMetrics(sb *sandbox.Sandbox, others *oci.ContainerState) []*types.Metric { @@ -15,7 +13,7 @@ func generateSandboxOtherMetrics(sb *sandbox.Sandbox, others *oci.ContainerState desc: containerStartTimeSeconds, valueFunc: func() metricValues { return metricValues{{ - value: uint64(others.Started.Unix()) , + value: uint64(others.Started.Unix()), metricType: types.MetricType_GAUGE, }} }, @@ -23,4 +21,4 @@ func generateSandboxOtherMetrics(sb *sandbox.Sandbox, others *oci.ContainerState } return computeSandboxMetrics(sb, otherMetrics, "other") -} \ No newline at end of file +} diff --git a/internal/lib/stats/stats_server_linux.go b/internal/lib/stats/stats_server_linux.go index 8910c9e4af5..948593b1229 100644 --- a/internal/lib/stats/stats_server_linux.go +++ b/internal/lib/stats/stats_server_linux.go @@ -263,6 +263,9 @@ func (ss *StatsServer) containerMetricsFromCgStats(sb *sandbox.Sandbox, c *oci.C metrics = append(metrics, oomMetrics...) case NetworkMetrics: continue // Network metrics are collected at the pod level only. + case OtherMetrics: + otherMetrics := generateSandboxOtherMetrics(sb, c.State()) + metrics = append(metrics, otherMetrics...) default: log.Warnf(ss.ctx, "Unknown metric: %s", m) } From ffd6d7445eea455a2a522795d84a0c05ba53bf99 Mon Sep 17 00:00:00 2001 From: Bhupathi Shwejan Raj Date: Sat, 28 Jun 2025 10:06:35 -0400 Subject: [PATCH 3/3] adds integration test --- internal/lib/stats/descriptors.go | 2 +- test/metrics.bats | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/lib/stats/descriptors.go b/internal/lib/stats/descriptors.go index cd0883c8323..4654888ae7b 100644 --- a/internal/lib/stats/descriptors.go +++ b/internal/lib/stats/descriptors.go @@ -154,7 +154,7 @@ var ( var ( containerStartTimeSeconds = &types.MetricDescriptor{ Name: "container_start_time_seconds", - Help: "Start time of the container since unix epoch", + Help: "Start time of the container since unix epoch in seconds", LabelKeys: baseLabelKeys, } ) diff --git a/test/metrics.bats b/test/metrics.bats index e9ad99ccd64..f4520cd5ed3 100644 --- a/test/metrics.bats +++ b/test/metrics.bats @@ -129,3 +129,21 @@ function teardown() { # crictl rmp -fa # ! curl -sf "http://localhost:$PORT/metrics" | grep 'crio_containers_oom{' #} + +@test "container_start_time_seconds metric is exposed" { + # Start CRI-O with metrics enabled on a random port + PORT=$(free_port) + CONTAINER_ENABLE_METRICS=true CONTAINER_METRICS_PORT="$PORT" start_crio + + # Create a pod and a container + pod_id=$(crictl runp "$TESTDATA"/sandbox_config.json) + ctr_id=$(crictl create "$pod_id" "$TESTDATA"/container_config.json "$TESTDATA"/container_config.json) + crictl start "$ctr_id" + + # Allow some time for the metric to be populated + sleep 2 + + # Fetch metrics and check for the enw metric + METRIC=$(curl -sf "http://localhost:$PORT/metrics" | grep '^container_start_time_seconds{') + [[ -n "$METRIC" ]] +} \ No newline at end of file