8000 adds container_start_time_seconds metric by shwejanraj · Pull Request #9282 · cri-o/cri-o · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

adds container_start_time_seconds metric #9282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/lib/stats/descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,12 @@ var (
LabelKeys: baseLabelKeys,
}
)

// other metrics.
var (
containerStartTimeSeconds = &types.MetricDescriptor{
Name: "container_start_time_seconds",
Help: "Start time of the container since unix epoch in seconds",
LabelKeys: baseLabelKeys,
}
)
4 changes: 4 additions & 0 deletions internal/lib/stats/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
MemoryMetrics = "memory"
NetworkMetrics = "network"
OOMMetrics = "oom"
OtherMetrics = "other"
)
type metricValue struct {
Expand Down Expand Up @@ -85,6 +86,9 @@
OOMMetrics: {
containerOomEventsTotal,
},
OtherMetrics: {
containerStartTimeSeconds,
},

Check warning on line 91 in internal/lib/stats/metrics.go

View check run for this annotation

Codecov / codecov/patch

internal/lib/stats/metrics.go#L89-L91

Added lines #L89 - L91 were not covered by tests
}

return descriptorsMap
Expand Down
24 changes: 24 additions & 0 deletions internal/lib/stats/other_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package statsserver

import (
types "k8s.io/cri-api/pkg/apis/runtime/v1"

"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 {
otherMetrics := []*containerMetric{
{
desc: containerStartTimeSeconds,
valueFunc: func() metricValues {
return metricValues{{
value: uint64(others.Started.Unix()),
metricType: types.MetricType_GAUGE,
}}
},

Check warning on line 19 in internal/lib/stats/other_metrics.go

View check run for this annotation

Codecov / codecov/patch

internal/lib/stats/other_metrics.go#L10-L19

Added lines #L10 - L19 were not covered by tests
},
}

return computeSandboxMetrics(sb, otherMetrics, "other")

Check warning on line 23 in internal/lib/stats/other_metrics.go

View check run for this annotation

Codecov / codecov/patch

internal/lib/stats/other_metrics.go#L23

Added line #L23 was not covered by tests
}
3 changes: 3 additions & 0 deletions internal/lib/stats/stats_server_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@
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...)

Check warning on line 268 in internal/lib/stats/stats_server_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/lib/stats/stats_server_linux.go#L266-L268

Added lines #L266 - L268 were not covered by tests
default:
log.Warnf(ss.ctx, "Unknown metric: %s", m)
}
Expand Down
18 changes: 18 additions & 0 deletions test/metrics.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]]
}
Loading
0