update env var name to EXCLUDE_CONTAINERS #1352

This commit is contained in:
henrygd
2025-10-30 19:30:01 -04:00
parent af6bd4e505
commit 85ac2e5e9a
2 changed files with 15 additions and 25 deletions

View File

@@ -54,7 +54,7 @@ type dockerManager struct {
buf *bytes.Buffer // Buffer to store and read response bodies buf *bytes.Buffer // Buffer to store and read response bodies
decoder *json.Decoder // Reusable JSON decoder that reads from buf decoder *json.Decoder // Reusable JSON decoder that reads from buf
apiStats *container.ApiStats // Reusable API stats object apiStats *container.ApiStats // Reusable API stats object
containerExclude []string // Patterns to exclude containers by name (supports wildcards) excludeContainers []string // Patterns to exclude containers by name
// Cache-time-aware tracking for CPU stats (similar to cpu.go) // Cache-time-aware tracking for CPU stats (similar to cpu.go)
// Maps cache time intervals to container-specific CPU usage tracking // Maps cache time intervals to container-specific CPU usage tracking
@@ -96,13 +96,12 @@ func (d *dockerManager) dequeue() {
} }
} }
// shouldExcludeContainer checks if a container name matches any exclusion pattern using path.Match // shouldExcludeContainer checks if a container name matches any exclusion pattern
func (dm *dockerManager) shouldExcludeContainer(name string) bool { func (dm *dockerManager) shouldExcludeContainer(name string) bool {
if len(dm.containerExclude) == 0 { if len(dm.excludeContainers) == 0 {
return false return false
} }
for _, pattern := range dm.containerExclude { for _, pattern := range dm.excludeContainers {
// Use path.Match for wildcard support
if match, _ := path.Match(pattern, name); match { if match, _ := path.Match(pattern, name); match {
return true return true
} }
@@ -138,15 +137,9 @@ func (dm *dockerManager) getDockerStats(cacheTimeMs uint16) ([]*container.Stats,
for _, ctr := range dm.apiContainerList { for _, ctr := range dm.apiContainerList {
ctr.IdShort = ctr.Id[:12] ctr.IdShort = ctr.Id[:12]
// Extract container name and check if it should be excluded
name := ctr.Names[0]
if len(name) > 0 && name[0] == '/' {
name = name[1:]
}
// Skip this container if it matches the exclusion pattern // Skip this container if it matches the exclusion pattern
if dm.shouldExcludeContainer(name) { if dm.shouldExcludeContainer(ctr.Names[0][1:]) {
slog.Debug("Excluding container", "name", name, "patterns", dm.containerExclude) slog.Debug("Excluding container", "name", ctr.Names[0][1:])
continue continue
} }
@@ -532,20 +525,17 @@ func newDockerManager(a *Agent) *dockerManager {
userAgent: "Docker-Client/", userAgent: "Docker-Client/",
} }
// Read container exclusion patterns from environment variable (comma-separated, supports wildcards) // Read container exclusion patterns from environment variable
var containerExclude []string var excludeContainers []string
if excludeStr, set := GetEnv("CONTAINER_EXCLUDE"); set && excludeStr != "" { if excludeStr, set := GetEnv("EXCLUDE_CONTAINERS"); set && excludeStr != "" {
// Split by comma and trim whitespace parts := strings.SplitSeq(excludeStr, ",")
parts := strings.Split(excludeStr, ",") for part := range parts {
for _, part := range parts {
trimmed := strings.TrimSpace(part) trimmed := strings.TrimSpace(part)
if trimmed != "" { if trimmed != "" {
containerExclude = append(containerExclude, trimmed) excludeContainers = append(excludeContainers, trimmed)
} }
} }
if len(containerExclude) > 0 { slog.Info("EXCLUDE_CONTAINERS", "patterns", excludeContainers)
slog.Info("Container exclusion patterns set", "patterns", containerExclude)
}
} }
manager := &dockerManager{ manager := &dockerManager{
@@ -557,7 +547,7 @@ func newDockerManager(a *Agent) *dockerManager {
sem: make(chan struct{}, 5), sem: make(chan struct{}, 5),
apiContainerList: []*container.ApiInfo{}, apiContainerList: []*container.ApiInfo{},
apiStats: &container.ApiStats{}, apiStats: &container.ApiStats{},
containerExclude: containerExclude, excludeContainers: excludeContainers,
// Initialize cache-time-aware tracking structures // Initialize cache-time-aware tracking structures
lastCpuContainer: make(map[uint16]map[string]uint64), lastCpuContainer: make(map[uint16]map[string]uint64),

View File

@@ -1196,7 +1196,7 @@ func TestShouldExcludeContainer(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dm := &dockerManager{ dm := &dockerManager{
containerExclude: tt.patterns, excludeContainers: tt.patterns,
} }
result := dm.shouldExcludeContainer(tt.containerName) result := dm.shouldExcludeContainer(tt.containerName)
assert.Equal(t, tt.expected, result) assert.Equal(t, tt.expected, result)