From 5e9add8659db038db5ad1c4a8e3977f3a124f18e Mon Sep 17 00:00:00 2001 From: Matt Artist Date: Thu, 4 Jan 2024 12:58:00 -0500 Subject: [PATCH] chore: cleanup --- modules/docker_network/charts.go | 22 +--------------------- modules/docker_network/collect.go | 19 +++++++++++++++++++ modules/docker_network/docker_network.go | 10 ++++++---- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/modules/docker_network/charts.go b/modules/docker_network/charts.go index ed3a9c029..fa590364f 100644 --- a/modules/docker_network/charts.go +++ b/modules/docker_network/charts.go @@ -10,27 +10,7 @@ const ( prioNetworkBytes = module.Priority + iota ) -var summaryCharts = module.Charts{ - // These charts will be collected for all of docker - // Mostly just aggregate stats - networkBytesChart.Copy(), -} - -var ( - networkBytesChart = module.Chart{ - ID: "network_bytes", - Title: "Network bytes", - Units: "bytes/s", - Fam: "network", - Ctx: "docker_net.network_bytes", - Priority: prioNetworkBytes, - Type: module.Stacked, - Dims: module.Dims{ - {ID: "network_bytes_rx", Name: "received"}, - {ID: "network_bytes_tx", Name: "sent"}, - }, - } -) +var summaryCharts = module.Charts{} var ( containerNetworkChartsTmpl = module.Charts{ diff --git a/modules/docker_network/collect.go b/modules/docker_network/collect.go index 6264bdf9a..a82cae7f4 100644 --- a/modules/docker_network/collect.go +++ b/modules/docker_network/collect.go @@ -78,6 +78,12 @@ func (d *DockerNetwork) collectContainers(mx map[string]int64) error { } // We can now get the network stats network := stat.Networks + // If there's no previous stats for this container then we ignore it but save the stats + if _, ok := d.previousStats[container.ID]; !ok { + d.previousStats[container.ID] = stat + cancel() + continue + } // Now we want the tx and rx bytes txBytes := 0 rxBytes := 0 @@ -86,6 +92,17 @@ func (d *DockerNetwork) collectContainers(mx map[string]int64) error { txBytes += int(net.TxBytes) rxBytes += int(net.RxBytes) } + // Add up previous stats + prev := d.previousStats[container.ID] + for _, net := range prev.Networks { + txBytes -= int(net.TxBytes) + rxBytes -= int(net.RxBytes) + } + // Now we have how much traffic has happened in the last "update time" seconds + // So to get this into a bytes/sec we divide by the update time + txBytes /= d.UpdateEvery + rxBytes /= d.UpdateEvery + // Now we have what we wanted name := strings.TrimPrefix(container.Names[0], "/") seen[name] = true @@ -100,6 +117,8 @@ func (d *DockerNetwork) collectContainers(mx map[string]int64) error { px := fmt.Sprintf("container_%s_", name) mx[px+"network_bytes_tx"] = int64(txBytes) mx[px+"network_bytes_rx"] = int64(rxBytes) + // Update the previous stats + d.previousStats[container.ID] = stat // We can now close the context cancel() } diff --git a/modules/docker_network/docker_network.go b/modules/docker_network/docker_network.go index a8f6c3674..17f2ffeee 100644 --- a/modules/docker_network/docker_network.go +++ b/modules/docker_network/docker_network.go @@ -36,13 +36,15 @@ func New() *DockerNetwork { newClient: func(cfg Config) (dockerClient, error) { return docker.NewClientWithOpts(docker.WithHost(cfg.Address)) }, - containers: make(map[string]bool), + containers: make(map[string]bool), + previousStats: make(map[string]types.StatsJSON), } } type Config struct { - Timeout web.Duration `yaml:"timeout"` - Address string `yaml:"address"` + Timeout web.Duration `yaml:"timeout"` + Address string `yaml:"address"` + UpdateEvery int `yaml:"update_every"` } type ( @@ -80,7 +82,7 @@ func (d *DockerNetwork) Init() bool { // Check will check if the module is able to collect metrics. func (d *DockerNetwork) Check() bool { - return len(d.Collect()) > 0 + return true } // Charts returns the charts that we want to expose to the agent.