Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mja00 committed Jan 4, 2024
1 parent bd5892f commit 5e9add8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
22 changes: 1 addition & 21 deletions modules/docker_network/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
19 changes: 19 additions & 0 deletions modules/docker_network/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
}
Expand Down
10 changes: 6 additions & 4 deletions modules/docker_network/docker_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5e9add8

Please sign in to comment.