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

Add docker diskio stats on Windows #8126

Merged
merged 5 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -66,6 +66,7 @@ https://github.com/elastic/beats/compare/v6.4.0...master[Check the HEAD diff]
- Recover metrics for old apache versions removed by mistake on #6450. {pull}7871[7871]
- Add missing namespace field in http server metricset {pull}7890[7890]
- Fixed the RPM by designating the modules.d config files as configuration data in the RPM spec. {issue}8075[8075]
- Add docker diskio stats on Windows. {issue}6815[6815] {pull}8126[8126]

*Packetbeat*

Expand Down
46 changes: 43 additions & 3 deletions metricbeat/module/docker/diskio/diskio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func setTime(index int) {
newBlkioRaw[index].Time = oldBlkioRaw[index].Time.Add(time.Duration(2000000000))
}

func TestGetBlkioStats(t *testing.T) {
func TestGetBlkioStatsList(t *testing.T) {
start := time.Now()
later := start.Add(10 * time.Second)

Expand All @@ -232,7 +232,7 @@ func TestGetBlkioStats(t *testing.T) {
},
}

dockerStats := &docker.Stat{
dockerStats := []docker.Stat{{
Container: &types.Container{
ID: "cebada",
Names: []string{"test"},
Expand All @@ -258,9 +258,49 @@ func TestGetBlkioStats(t *testing.T) {
},
},
}},
}}

statsList := blkioService.getBlkioStatsList(dockerStats, true)
stats := statsList[0]
assert.Equal(t, float64(5), stats.reads)
assert.Equal(t, float64(10), stats.writes)
assert.Equal(t, float64(15), stats.totals)
assert.Equal(t,
BlkioRaw{Time: later, reads: 150, writes: 300, totals: 450},
stats.serviced)
assert.Equal(t,
BlkioRaw{Time: later, reads: 1500, writes: 3000, totals: 4500},
stats.servicedBytes)
}

func TestGetBlkioStatsListWindows(t *testing.T) {
start := time.Now()
later := start.Add(10 * time.Second)

blkioService := BlkioService{
map[string]BlkioRaw{
"cebada": {Time: start, reads: 100, writes: 200, totals: 300},
},
}

stats := blkioService.getBlkioStats(dockerStats, true)
dockerStats := []docker.Stat{{
Container: &types.Container{
ID: "cebada",
Names: []string{"test"},
},
Stats: types.StatsJSON{Stats: types.Stats{
Read: later,
StorageStats: types.StorageStats{
ReadCountNormalized: 150,
WriteCountNormalized: 300,
ReadSizeBytes: 1500,
WriteSizeBytes: 3000,
},
}},
}}

statsList := blkioService.getBlkioStatsList(dockerStats, true)
stats := statsList[0]
assert.Equal(t, float64(5), stats.reads)
assert.Equal(t, float64(10), stats.writes)
assert.Equal(t, float64(15), stats.totals)
Expand Down
68 changes: 52 additions & 16 deletions metricbeat/module/docker/diskio/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ type BlkioStats struct {
servicedBytes BlkioRaw
}

func (s *BlkioStats) Add(o *BlkioStats) {

Choose a reason for hiding this comment

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

exported method BlkioStats.Add should have comment or be unexported

s.reads += o.reads
s.writes += o.writes
s.totals += o.totals

s.serviced.Add(&o.serviced)
s.servicedBytes.Add(&o.servicedBytes)
}

type BlkioRaw struct {
Time time.Time
reads uint64
writes uint64
totals uint64
}

func (s *BlkioRaw) Add(o *BlkioRaw) {

Choose a reason for hiding this comment

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

exported method BlkioRaw.Add should have comment or be unexported

s.reads += o.reads
s.writes += o.writes
s.totals += o.totals
}

// BlkioService is a helper to collect and calculate disk I/O metrics
type BlkioService struct {
lastStatsPerContainer map[string]BlkioRaw
Expand All @@ -61,34 +76,55 @@ func (io *BlkioService) getBlkioStatsList(rawStats []docker.Stat, dedot bool) []
statsPerContainer := make(map[string]BlkioRaw)
for _, myRawStats := range rawStats {
stats := io.getBlkioStats(&myRawStats, dedot)
statsPerContainer[myRawStats.Container.ID] = stats.serviced
storageStats := io.getStorageStats(&myRawStats, dedot)
stats.Add(&storageStats)

oldStats, exist := io.lastStatsPerContainer[stats.Container.ID]
if exist {
stats.reads = io.getReadPs(&oldStats, &stats.serviced)
stats.writes = io.getWritePs(&oldStats, &stats.serviced)
stats.totals = io.getTotalPs(&oldStats, &stats.serviced)
}

statsPerContainer[stats.Container.ID] = stats.serviced
formattedStats = append(formattedStats, stats)
}

io.lastStatsPerContainer = statsPerContainer
return formattedStats
}

func (io *BlkioService) getBlkioStats(myRawStat *docker.Stat, dedot bool) BlkioStats {
newBlkioStats := io.getNewStats(myRawStat.Stats.Read, myRawStat.Stats.BlkioStats.IoServicedRecursive)
bytesBlkioStats := io.getNewStats(myRawStat.Stats.Read, myRawStat.Stats.BlkioStats.IoServiceBytesRecursive)
func (io *BlkioService) getStorageStats(myRawStats *docker.Stat, dedot bool) BlkioStats {
return BlkioStats{
Time: myRawStats.Stats.Read,
Container: docker.NewContainer(myRawStats.Container, dedot),

serviced: BlkioRaw{
reads: myRawStats.Stats.StorageStats.ReadCountNormalized,
writes: myRawStats.Stats.StorageStats.WriteCountNormalized,
totals: myRawStats.Stats.StorageStats.ReadCountNormalized + myRawStats.Stats.StorageStats.WriteCountNormalized,
},

servicedBytes: BlkioRaw{
reads: myRawStats.Stats.StorageStats.ReadSizeBytes,
writes: myRawStats.Stats.StorageStats.WriteSizeBytes,
totals: myRawStats.Stats.StorageStats.ReadSizeBytes + myRawStats.Stats.StorageStats.WriteSizeBytes,
},
}
}

myBlkioStats := BlkioStats{
func (io *BlkioService) getBlkioStats(myRawStat *docker.Stat, dedot bool) BlkioStats {
return BlkioStats{
Time: myRawStat.Stats.Read,
Container: docker.NewContainer(myRawStat.Container, dedot),

serviced: newBlkioStats,
servicedBytes: bytesBlkioStats,
serviced: io.getNewStats(
myRawStat.Stats.Read,
myRawStat.Stats.BlkioStats.IoServicedRecursive),
servicedBytes: io.getNewStats(
myRawStat.Stats.Read,
myRawStat.Stats.BlkioStats.IoServiceBytesRecursive),
}

oldBlkioStats, exist := io.lastStatsPerContainer[myRawStat.Container.ID]
if exist {
myBlkioStats.reads = io.getReadPs(&oldBlkioStats, &newBlkioStats)
myBlkioStats.writes = io.getWritePs(&oldBlkioStats, &newBlkioStats)
myBlkioStats.totals = io.getTotalPs(&oldBlkioStats, &newBlkioStats)
}

return myBlkioStats
}

func (io *BlkioService) getNewStats(time time.Time, blkioEntry []types.BlkioStatEntry) BlkioRaw {
Expand Down