Skip to content

Commit

Permalink
[Docker] Stop returning errors when there are no metric values (#39807)
Browse files Browse the repository at this point in the history
* Return nil instead or an error if no metrics

The docker metricset returns an error when there are no memory metric
values available.

This condition can happen when there are no running containers on
Docker.

When no containers are running, the metricset returns an error
at every collection, creating noise.

* Add a "docker.memory" logger to write a debug message on no-metrics
  • Loading branch information
zmoog authored Jun 7, 2024
1 parent 3102b49 commit 4c1d3f2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions metricbeat/module/docker/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package memory
import (
"fmt"

"github.com/elastic/elastic-agent-libs/logp"

"github.com/docker/docker/client"

"github.com/elastic/beats/v7/metricbeat/mb"
Expand All @@ -41,25 +43,28 @@ type MetricSet struct {
memoryService *MemoryService
dockerClient *client.Client
dedot bool
logger *logp.Logger
}

// New creates a new instance of the docker memory MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logger := logp.NewLogger("docker.memory")
config := docker.DefaultConfig()
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}

client, err := docker.NewDockerClient(base.HostData().URI, config)
dockerClient, err := docker.NewDockerClient(base.HostData().URI, config)
if err != nil {
return nil, err
}

return &MetricSet{
BaseMetricSet: base,
memoryService: &MemoryService{},
dockerClient: client,
dockerClient: dockerClient,
dedot: config.DeDot,
logger: logger,
}, nil
}

Expand All @@ -72,7 +77,10 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error {

memoryStats := m.memoryService.getMemoryStatsList(stats, m.dedot)
if len(memoryStats) == 0 {
return fmt.Errorf("No memory stats data available")
// No memory stats available, probably
// because no containers are running.
m.logger.Debug("No memory stats data available")
return nil
}
eventsMapping(r, memoryStats)

Expand Down

0 comments on commit 4c1d3f2

Please sign in to comment.