Skip to content

Commit c5d36b1

Browse files
committed
add process metrics
Signed-off-by: Akhil Mohan <[email protected]>
1 parent 9542cbf commit c5d36b1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

internal/cri/server/list_metric_descriptors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ const (
2222
NetworkUsageMetrics = "network"
2323
DiskIOMetrics = "diskIO"
2424
DiskUsageMetrics = "disk"
25+
ProcessMetrics = "process"
2526
)

internal/cri/server/list_metric_descriptors_linux.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,38 @@ func (c *criService) getMetricDescriptors() map[string][]*runtime.MetricDescript
265265
LabelKeys: append(baseLabelKeys, "device", "major", "minor", "operation"),
266266
},
267267
},
268+
ProcessMetrics: {
269+
{
270+
Name: "container_processes",
271+
Help: "Number of processes running inside the container.",
272+
LabelKeys: baseLabelKeys,
273+
},
274+
/*{
275+
Name: "container_file_descriptors",
276+
Help: "Number of open file descriptors for the container.",
277+
LabelKeys: baseLabelKeys,
278+
},
279+
{
280+
Name: "container_sockets",
281+
Help: "Number of open sockets for the container.",
282+
LabelKeys: baseLabelKeys,
283+
},*/
284+
{
285+
Name: "container_threads_max",
286+
Help: "Maximum number of threads allowed inside the container, infinity if value is zero",
287+
LabelKeys: baseLabelKeys,
288+
},
289+
/*{
290+
Name: "container_threads",
291+
Help: "Number of threads running inside the container",
292+
LabelKeys: baseLabelKeys,
293+
},
294+
{
295+
Name: "container_ulimits_soft",
296+
Help: "Soft ulimit values for the container root process. Unlimited if -1, except priority and nice",
297+
LabelKeys: append(baseLabelKeys, "ulimit"),
298+
},*/
299+
},
268300
}
269301
return descriptors
270302
}

internal/cri/server/list_pod_sandbox_metrics_linux.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ func (c *criService) collectContainerMetrics(ctx context.Context, container cont
275275
containerMetrics.Metrics = append(containerMetrics.Metrics, fsMetrics...)
276276
}
277277

278+
// Collect process metrics
279+
processMetrics, err := c.extractProcessMetrics(stats, containerLabels, timestamp)
280+
if err != nil {
281+
log.G(ctx).WithField("containerid", container.ID).WithError(err).Debug("failed to extract process metrics")
282+
} else {
283+
containerMetrics.Metrics = append(containerMetrics.Metrics, processMetrics...)
284+
}
285+
278286
return containerMetrics, nil
279287
}
280288

@@ -805,6 +813,58 @@ func (c *criService) extractDiskMetrics(stats interface{}, labels []string, time
805813
return metrics, nil
806814
}
807815

816+
// extractProcessMetrics extracts process-related metrics from container stats
817+
func (c *criService) extractProcessMetrics(stats interface{}, labels []string, timestamp int64) ([]*runtime.Metric, error) {
818+
var metrics []*runtime.Metric
819+
820+
switch s := stats.(type) {
821+
case *cg1.Metrics:
822+
if s.Pids != nil {
823+
metrics = append(metrics, []*runtime.Metric{
824+
{
825+
Name: "container_processes",
826+
Timestamp: timestamp,
827+
MetricType: runtime.MetricType_GAUGE,
828+
LabelValues: labels,
829+
Value: &runtime.UInt64Value{Value: s.Pids.Current},
830+
},
831+
{
832+
Name: "container_threads_max",
833+
Timestamp: timestamp,
834+
MetricType: runtime.MetricType_GAUGE,
835+
LabelValues: labels,
836+
Value: &runtime.UInt64Value{Value: s.Pids.Limit},
837+
},
838+
}...)
839+
}
840+
841+
case *cg2.Metrics:
842+
if s.Pids != nil {
843+
metrics = append(metrics, []*runtime.Metric{
844+
{
845+
Name: "container_processes",
846+
Timestamp: timestamp,
847+
MetricType: runtime.MetricType_GAUGE,
848+
LabelValues: labels,
849+
Value: &runtime.UInt64Value{Value: s.Pids.Current},
850+
},
851+
{
852+
Name: "container_threads_max",
853+
Timestamp: timestamp,
854+
MetricType: runtime.MetricType_GAUGE,
855+
LabelValues: labels,
856+
Value: &runtime.UInt64Value{Value: s.Pids.Limit},
857+
},
858+
}...)
859+
}
860+
861+
default:
862+
return nil, fmt.Errorf("unexpected metrics type: %T from %s", s, reflect.TypeOf(s).Elem().PkgPath())
863+
}
864+
865+
return metrics, nil
866+
}
867+
808868
// extractFilesystemMetrics extracts filesystem-related metrics from container stats
809869
func (c *criService) extractFilesystemMetrics(ctx context.Context, containerID string, labels []string, timestamp int64) ([]*runtime.Metric, error) {
810870
var metrics []*runtime.Metric

0 commit comments

Comments
 (0)