Skip to content
Merged
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
5 changes: 2 additions & 3 deletions scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"log/slog"
"net/http"
"strings"
"time"

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

containerName := lset.Get("name")

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

if !isLibopsContainer && !isTasksState && isPositiveValue && !isCapContainer && matchesRegex {
if hasContainerName && !isTasksState && isPositiveValue && !isCapContainer && matchesRegex {
batch = append(batch, record.RefSample{
Ref: chunks.HeadSeriesRef(ref),
V: val,
Expand Down
25 changes: 15 additions & 10 deletions scraper/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ container_cpu_usage_seconds_total{id="/",name="cap",namespace="default"} 100.0 1
container_cpu_usage_seconds_total{id="/kubepods/burstable/pod1/c1",name="my-app",namespace="default"} 5.0 1678886400000
container_cpu_usage_seconds_total{id="/kubepods/burstable/pod1/c2",name="libops-cache",namespace="default"} 1.0 1678886400000
container_cpu_usage_seconds_total{id="/kubepods/burstable/pod1/c3",name="other-app",namespace="default"} 0.0 1678886400000
container_cpu_usage_seconds_total{id="/system.slice/docker.service"} 2.0 1678886400000
# HELP container_tasks_state The state of the container's tasks.
# TYPE container_tasks_state gauge
container_tasks_state{state="running",name="my-app"} 1.0 1678886400000
Expand All @@ -55,21 +56,25 @@ container_tasks_state{state="running",name="my-app"} 1.0 1678886400000
// Expected results:
// 1. "cap" container (name="cap") -> EXCLUDED
// 2. "my-app" container (name="my-app", metric="container_cpu_usage_seconds_total", val=5.0) -> INCLUDED
// 3. "libops-cache" container (name="libops-cache") -> EXCLUDED (HasPrefix)
// 3. "libops-cache" container (name="libops-cache") -> INCLUDED
// 4. "other-app" container (name="other-app", val=0.0) -> EXCLUDED (val <= 0.0)
// 5. "my-app" tasks_state (metric="container_tasks_state") -> EXCLUDED (Metric name check)
// 5. "/system.slice/docker.service" (no name label) -> EXCLUDED (not a Docker container series)
// 6. "my-app" tasks_state (metric="container_tasks_state") -> EXCLUDED (Metric name check)

if len(batch) != 1 {
t.Fatalf("Expected 1 metric sample after filtering, got %d", len(batch))
if len(batch) != 2 {
t.Fatalf("Expected 2 metric samples after filtering, got %d", len(batch))
}

// Helper to find the original labels from the Ref
labelsByRef := s.GetLabelsByRef(storage.SeriesRef(batch[0].Ref))
if labelsByRef.Get("name") != "my-app" {
t.Errorf("Included metric has wrong container name. Expected 'my-app', got '%s'", labelsByRef.Get("name"))
got := make(map[string]float64, len(batch))
for _, sample := range batch {
labelsByRef := s.GetLabelsByRef(storage.SeriesRef(sample.Ref))
got[labelsByRef.Get("name")] = sample.V
}
if got["my-app"] != 5.0 {
t.Errorf("Expected my-app value 5.0, got %f", got["my-app"])
}
if batch[0].V != 5.0 {
t.Errorf("Included metric has wrong value. Expected 5.0, got %f", batch[0].V)
if got["libops-cache"] != 1.0 {
t.Errorf("Expected libops-cache value 1.0, got %f", got["libops-cache"])
}
}

Expand Down
Loading