Skip to content

Commit 2ed6cca

Browse files
committed
Implement container_processes metric
Signed-off-by: Gavin Lam <[email protected]>
1 parent 2687346 commit 2ed6cca

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

internal/lib/stats/descriptors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,12 @@ var (
163163
LabelKeys: baseLabelKeys,
164164
}
165165
)
166+
167+
// Process metrics.
168+
var (
169+
containerProcesses = &types.MetricDescriptor{
170+
Name: "container_processes",
171+
Help: "Number of processes running inside the container",
172+
LabelKeys: baseLabelKeys,
173+
}
174+
)

internal/lib/stats/metrics.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
MemoryMetrics = "memory"
1717
NetworkMetrics = "network"
1818
OOMMetrics = "oom"
19+
ProcessMetrics = "process"
1920
)
2021

2122
type metricValue struct {
@@ -90,6 +91,9 @@ func (ss *StatsServer) PopulateMetricDescriptors(includedKeys []string) map[stri
9091
OOMMetrics: {
9192
containerOomEventsTotal,
9293
},
94+
ProcessMetrics: {
95+
containerProcesses,
96+
},
9397
}
9498

9599
return descriptorsMap
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package statsserver
2+
3+
import (
4+
types "k8s.io/cri-api/pkg/apis/runtime/v1"
5+
6+
"github.com/cri-o/cri-o/internal/config/cgmgr"
7+
"github.com/cri-o/cri-o/internal/lib/sandbox"
8+
)
9+
10+
func generateSandboxProcessMetrics(sb *sandbox.Sandbox, pids *cgmgr.PidsStats) []*types.Metric {
11+
processMetrics := []*containerMetric{
12+
{
13+
desc: containerProcesses,
14+
valueFunc: func() metricValues {
15+
return metricValues{{
16+
value: pids.Current,
17+
metricType: types.MetricType_GAUGE,
18+
}}
19+
},
20+
},
21+
}
22+
23+
return computeSandboxMetrics(sb, processMetrics, "process")
24+
}

internal/lib/stats/stats_server_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ func (ss *StatsServer) containerMetricsFromCgStats(sb *sandbox.Sandbox, c *oci.C
267267
metrics = append(metrics, oomMetrics...)
268268
case NetworkMetrics:
269269
continue // Network metrics are collected at the pod level only.
270+
case ProcessMetrics:
271+
if processMetrics := generateSandboxProcessMetrics(sb, cgstats.Pid); processMetrics != nil {
272+
metrics = append(metrics, processMetrics...)
273+
}
270274
default:
271275
log.Warnf(ss.ctx, "Unknown metric: %s", m)
272276
}

test/cri-metrics.bats

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ EOF
169169
collection_period = 0
170170
included_pod_metrics = [
171171
"network",
172-
"cpu",
173172
"hugetlb",
174173
"memory",
175-
"oom",
176174
]
177175
EOF
178176
start_crio_no_setup
@@ -230,3 +228,31 @@ EOF
230228
echo 0 | tee /proc/sys/vm/nr_hugepages
231229
fi
232230
}
231+
232+
@test "container process metrics" {
233+
CONTAINER_ENABLE_METRICS="true" CONTAINER_METRICS_PORT=$(free_port) setup_crio
234+
cat << EOF > "$CRIO_CONFIG"
235+
[crio.stats]
236+
collection_period = 0
237+
included_pod_metrics = [
238+
"network",
239+
"memory",
240+
"process",
241+
]
242+
EOF
243+
start_crio_no_setup
244+
check_images
245+
246+
metrics_setup
247+
set_container_pod_cgroup_root "" "$CONTAINER_ID"
248+
249+
# run some processes in the container
250+
crictl exec --sync "$CONTAINER_ID" /bin/bash -c 'sleep 30 &'
251+
crictl exec --sync "$CONTAINER_ID" /bin/bash -c 'sleep 30 &'
252+
253+
metrics=$(crictl metricsp | jq '.podMetrics[0].containerMetrics[0].metrics[]')
254+
255+
# assert container_processes == 3
256+
metrics_container_processes=$(echo "$metrics" | jq 'select(.name == "container_processes") | .value.value | tonumber')
257+
[[ $metrics_container_processes == "3" ]]
258+
}

0 commit comments

Comments
 (0)