Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed docker diskio bug due to reseting of map. #5582

Merged
merged 5 commits into from
Nov 21, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
- Fix the loading of 5.x dashboards. {issue}5277[5277]
- Fix the fetching of process information when some data is missing under MacOS X. {issue}5337[5337]
- Change `MySQL active connections` visualization title to `MySQL total connections`. {issue}4812[4812]
- Fix map overwrite in docker diskio module. {issue}5582[5582]

*Packetbeat*

Expand Down
115 changes: 115 additions & 0 deletions metricbeat/module/docker/diskio/diskio_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,129 @@
package diskio

import (
"encoding/json"
"testing"
"time"

"github.com/elastic/beats/metricbeat/module/docker"

dc "github.com/fsouza/go-dockerclient"
)

var blkioService BLkioService
var oldBlkioRaw = make([]BlkioRaw, 3)
var newBlkioRaw = make([]BlkioRaw, 3)

func TestDeltaMultipleContainers(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these tests

var apiContainer1 docker.Stat
var apiContainer2 docker.Stat
metrics := dc.BlkioStatsEntry{
Major: 123,
Minor: 123,
Op: "Total",
Value: 123,
}
jsonContainers := `[
{
"Id": "8dfafdbc3a40",
"Names": ["container"]
},{
"Id": "8dfafdbc3a41",
"Names": ["container1"]
}]`
var containers []dc.APIContainers
err := json.Unmarshal([]byte(jsonContainers), &containers)
if err != nil {
t.Fatal(err)
}

apiContainer1.Stats.Read = time.Now()
apiContainer1.Container = containers[0]
apiContainer1.Stats.BlkioStats.IOServicedRecursive = append(apiContainer1.Stats.BlkioStats.IOServicedRecursive, metrics)
apiContainer2.Stats.Read = time.Now()
apiContainer2.Container = containers[1]
apiContainer2.Stats.BlkioStats.IOServicedRecursive = append(apiContainer2.Stats.BlkioStats.IOServicedRecursive, metrics)
dockerStats := []docker.Stat{apiContainer1, apiContainer2}
stats := blkioService.getBlkioStatsList(dockerStats)
totals := make([]float64, 2)
for _, stat := range stats {
totals[0] = stat.totals
}

dockerStats[0].Stats.BlkioStats.IOServicedRecursive[0].Value = 1000
dockerStats[0].Stats.Read = dockerStats[0].Stats.Read.Add(time.Second * 10)
dockerStats[1].Stats.BlkioStats.IOServicedRecursive[0].Value = 1000
dockerStats[1].Stats.Read = dockerStats[0].Stats.Read.Add(time.Second * 10)
stats = blkioService.getBlkioStatsList(dockerStats)
for _, stat := range stats {
totals[1] = stat.totals
if stat.totals < totals[0] {
t.Errorf("getBlkioStatsList(%v) => %v, want value bigger than %v", dockerStats, stat.totals, totals[0])
}
}

dockerStats[0].Stats.Read = dockerStats[0].Stats.Read.Add(time.Second * 15)
dockerStats[0].Stats.BlkioStats.IOServicedRecursive[0].Value = 2000
dockerStats[1].Stats.BlkioStats.IOServicedRecursive[0].Value = 2000
dockerStats[1].Stats.Read = dockerStats[0].Stats.Read.Add(time.Second * 15)
stats = blkioService.getBlkioStatsList(dockerStats)
for _, stat := range stats {
if stat.totals < totals[1] || stat.totals < totals[0] {
t.Errorf("getBlkioStatsList(%v) => %v, want value bigger than %v", dockerStats, stat.totals, totals[1])
}
}

}

func TestDeltaOneContainer(t *testing.T) {
var apiContainer docker.Stat
metrics := dc.BlkioStatsEntry{
Major: 123,
Minor: 123,
Op: "Total",
Value: 123,
}
jsonContainers := `
{
"Id": "8dfafdbc3a40",
"Names": ["container"]
}`
var containers dc.APIContainers
err := json.Unmarshal([]byte(jsonContainers), &containers)
if err != nil {
t.Fatal(err)
}

apiContainer.Stats.Read = time.Now()
apiContainer.Container = containers
apiContainer.Stats.BlkioStats.IOServicedRecursive = append(apiContainer.Stats.BlkioStats.IOServicedRecursive, metrics)
dockerStats := []docker.Stat{apiContainer}
stats := blkioService.getBlkioStatsList(dockerStats)
totals := make([]float64, 2)
for _, stat := range stats {
totals[0] = stat.totals
}

dockerStats[0].Stats.BlkioStats.IOServicedRecursive[0].Value = 1000
dockerStats[0].Stats.Read = dockerStats[0].Stats.Read.Add(time.Second * 10)
stats = blkioService.getBlkioStatsList(dockerStats)
for _, stat := range stats {
if stat.totals < totals[0] {
t.Errorf("getBlkioStatsList(%v) => %v, want value bigger than %v", dockerStats, stat.totals, totals[0])
}
}

dockerStats[0].Stats.BlkioStats.IOServicedRecursive[0].Value = 2000
dockerStats[0].Stats.Read = dockerStats[0].Stats.Read.Add(time.Second * 15)
stats = blkioService.getBlkioStatsList(dockerStats)
for _, stat := range stats {
if stat.totals < totals[1] || stat.totals < totals[0] {
t.Errorf("getBlkioStatsList(%v) => %v, want value bigger than %v", dockerStats, stat.totals, totals[1])
}
}

}

func TestWritePs(t *testing.T) {
oldWritePs := []uint64{220, 951, 0}
newWritePs := []uint64{120, 2951, 0}
Expand Down
6 changes: 3 additions & 3 deletions metricbeat/module/docker/diskio/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ type BLkioService struct {

func (io *BLkioService) getBlkioStatsList(rawStats []docker.Stat) []BlkioStats {
formattedStats := []BlkioStats{}

if io.BlkioSTatsPerContainer == nil {
io.BlkioSTatsPerContainer = make(map[string]BlkioRaw)
}
for _, myRawStats := range rawStats {
formattedStats = append(formattedStats, io.getBlkioStats(&myRawStats))
}
Expand All @@ -56,8 +58,6 @@ func (io *BLkioService) getBlkioStats(myRawStat *docker.Stat) BlkioStats {
myBlkioStats.reads = io.getReadPs(&oldBlkioStats, &newBlkioStats)
myBlkioStats.writes = io.getWritePs(&oldBlkioStats, &newBlkioStats)
myBlkioStats.totals = io.getTotalPs(&oldBlkioStats, &newBlkioStats)
} else {
io.BlkioSTatsPerContainer = make(map[string]BlkioRaw)
}

io.BlkioSTatsPerContainer[myRawStat.Container.ID] = newBlkioStats
Expand Down