Skip to content

Commit

Permalink
Fixed docker diskio bug due to reseting of map. (elastic#5582)
Browse files Browse the repository at this point in the history
* Fixed docker diskio bug due to reseting of map.

Fixes elastic#5568
  • Loading branch information
kwojcicki committed Nov 23, 2017
1 parent 16a6d1f commit 25b6dda
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ https://github.com/elastic/beats/compare/v5.6.4...5.6[Check the HEAD diff]
*Metricbeat*

- Clarify meaning of percentages reported by system core metricset. {pull}5565[5565]
- Change field type of http header from nested to object {pull}5258[5258]
- 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) {
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
8 changes: 4 additions & 4 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 @@ -55,9 +57,7 @@ func (io *BLkioService) getBlkioStats(myRawStat *docker.Stat) BlkioStats {
if exist {
myBlkioStats.reads = io.getReadPs(&oldBlkioStats, &newBlkioStats)
myBlkioStats.writes = io.getWritePs(&oldBlkioStats, &newBlkioStats)
myBlkioStats.totals = io.getReadPs(&oldBlkioStats, &newBlkioStats)
} else {
io.BlkioSTatsPerContainer = make(map[string]BlkioRaw)
myBlkioStats.totals = io.getTotalPs(&oldBlkioStats, &newBlkioStats)
}

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

0 comments on commit 25b6dda

Please sign in to comment.