Skip to content

Commit a06cbaa

Browse files
committed
Filter non-container cAdvisor metrics (#14)
1 parent b966954 commit a06cbaa

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ FROM golang:1.26.3-alpine3.22@sha256:be93003ee861b3b91b6ebcb22678524947e0cd786c2
2020
COPY --from=builder /app/binary /app/binary
2121

2222
HEALTHCHECK NONE
23+
24+
CMD ["/app/binary"]

scraper/scraper.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"log/slog"
88
"net/http"
9-
"strings"
109
"time"
1110

1211
"github.com/GoogleCloudPlatform/prometheus-engine/pkg/export"
@@ -186,13 +185,13 @@ func (s *Scraper) ProcessBody(bodyBytes []byte) ([]record.RefSample, map[string]
186185

187186
containerName := lset.Get("name")
188187

189-
isLibopsContainer := strings.HasPrefix(containerName, "libops-")
188+
hasContainerName := containerName != ""
190189
isTasksState := currMeta.Metric == "container_tasks_state"
191190
isPositiveValue := val > 0.0
192191
isCapContainer := containerName == "cap"
193192
matchesRegex := s.Cfg.FilterRegex.MatchString(lset.String())
194193

195-
if !isLibopsContainer && !isTasksState && isPositiveValue && !isCapContainer && matchesRegex {
194+
if hasContainerName && !isTasksState && isPositiveValue && !isCapContainer && matchesRegex {
196195
batch = append(batch, record.RefSample{
197196
Ref: chunks.HeadSeriesRef(ref),
198197
V: val,

scraper/scraper_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ container_cpu_usage_seconds_total{id="/",name="cap",namespace="default"} 100.0 1
3838
container_cpu_usage_seconds_total{id="/kubepods/burstable/pod1/c1",name="my-app",namespace="default"} 5.0 1678886400000
3939
container_cpu_usage_seconds_total{id="/kubepods/burstable/pod1/c2",name="libops-cache",namespace="default"} 1.0 1678886400000
4040
container_cpu_usage_seconds_total{id="/kubepods/burstable/pod1/c3",name="other-app",namespace="default"} 0.0 1678886400000
41+
container_cpu_usage_seconds_total{id="/system.slice/docker.service"} 2.0 1678886400000
4142
# HELP container_tasks_state The state of the container's tasks.
4243
# TYPE container_tasks_state gauge
4344
container_tasks_state{state="running",name="my-app"} 1.0 1678886400000
@@ -55,21 +56,25 @@ container_tasks_state{state="running",name="my-app"} 1.0 1678886400000
5556
// Expected results:
5657
// 1. "cap" container (name="cap") -> EXCLUDED
5758
// 2. "my-app" container (name="my-app", metric="container_cpu_usage_seconds_total", val=5.0) -> INCLUDED
58-
// 3. "libops-cache" container (name="libops-cache") -> EXCLUDED (HasPrefix)
59+
// 3. "libops-cache" container (name="libops-cache") -> INCLUDED
5960
// 4. "other-app" container (name="other-app", val=0.0) -> EXCLUDED (val <= 0.0)
60-
// 5. "my-app" tasks_state (metric="container_tasks_state") -> EXCLUDED (Metric name check)
61+
// 5. "/system.slice/docker.service" (no name label) -> EXCLUDED (not a Docker container series)
62+
// 6. "my-app" tasks_state (metric="container_tasks_state") -> EXCLUDED (Metric name check)
6163

62-
if len(batch) != 1 {
63-
t.Fatalf("Expected 1 metric sample after filtering, got %d", len(batch))
64+
if len(batch) != 2 {
65+
t.Fatalf("Expected 2 metric samples after filtering, got %d", len(batch))
6466
}
6567

66-
// Helper to find the original labels from the Ref
67-
labelsByRef := s.GetLabelsByRef(storage.SeriesRef(batch[0].Ref))
68-
if labelsByRef.Get("name") != "my-app" {
69-
t.Errorf("Included metric has wrong container name. Expected 'my-app', got '%s'", labelsByRef.Get("name"))
68+
got := make(map[string]float64, len(batch))
69+
for _, sample := range batch {
70+
labelsByRef := s.GetLabelsByRef(storage.SeriesRef(sample.Ref))
71+
got[labelsByRef.Get("name")] = sample.V
72+
}
73+
if got["my-app"] != 5.0 {
74+
t.Errorf("Expected my-app value 5.0, got %f", got["my-app"])
7075
}
71-
if batch[0].V != 5.0 {
72-
t.Errorf("Included metric has wrong value. Expected 5.0, got %f", batch[0].V)
76+
if got["libops-cache"] != 1.0 {
77+
t.Errorf("Expected libops-cache value 1.0, got %f", got["libops-cache"])
7378
}
7479
}
7580

0 commit comments

Comments
 (0)