Skip to content

Commit 1f0b463

Browse files
feat(summary): add count in percentiles
To measure the quality of the derived metric, it is necessary to also store the count of the samples from which the percentile calculation is being done. This commit aims to add this feature in cadvisor.
1 parent 5adb1c3 commit 1f0b463

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

info/v2/container.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ type Percentiles struct {
200200
Ninety uint64 `json:"ninety"`
201201
// 95th percentile over the collected sample.
202202
NinetyFive uint64 `json:"ninetyfive"`
203+
// Number of samples used to calculate these percentiles.
204+
Count uint64 `json:"count"`
203205
}
204206

205207
type Usage struct {

summary/percentiles.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (r *resource) AddSample(val uint64) {
109109
Fifty: val,
110110
Ninety: val,
111111
NinetyFive: val,
112+
Count: 1,
112113
}
113114
r.Add(sample)
114115
}
@@ -121,6 +122,7 @@ func (r *resource) GetAllPercentiles() info.Percentiles {
121122
p.Fifty = r.samples.GetPercentile(0.5)
122123
p.Ninety = r.samples.GetPercentile(0.9)
123124
p.NinetyFive = r.samples.GetPercentile(0.95)
125+
p.Count = r.mean.count
124126
p.Present = true
125127
return p
126128
}

summary/percentiles_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func TestAggregates(t *testing.T) {
8484
Fifty: 1000,
8585
Ninety: 1000,
8686
NinetyFive: 1000,
87+
// Since cpu is calculated between samples, we lose 1 sample.
88+
Count: N - 2,
8789
}
8890
if usage.Cpu != cpuExpected {
8991
t.Errorf("cpu stats are %+v. Expected %+v", usage.Cpu, cpuExpected)
@@ -95,6 +97,7 @@ func TestAggregates(t *testing.T) {
9597
Fifty: 50 * 1024,
9698
Ninety: 90 * 1024,
9799
NinetyFive: 95 * 1024,
100+
Count: N - 1,
98101
}
99102
if usage.Memory != memExpected {
100103
t.Errorf("memory stats are mean %+v. Expected %+v", usage.Memory, memExpected)
@@ -133,6 +136,8 @@ func TestSamplesCloseInTimeIgnored(t *testing.T) {
133136
Fifty: 1000,
134137
Ninety: 1000,
135138
NinetyFive: 1000,
139+
// Since cpu is calculated between samples, we lose 1 sample.
140+
Count: N - 2,
136141
}
137142
if usage.Cpu != cpuExpected {
138143
t.Errorf("cpu stats are %+v. Expected %+v", usage.Cpu, cpuExpected)
@@ -144,6 +149,7 @@ func TestSamplesCloseInTimeIgnored(t *testing.T) {
144149
Fifty: 50 * 1024,
145150
Ninety: 90 * 1024,
146151
NinetyFive: 95 * 1024,
152+
Count: N - 1,
147153
}
148154
if usage.Memory != memExpected {
149155
t.Errorf("memory stats are mean %+v. Expected %+v", usage.Memory, memExpected)
@@ -184,6 +190,8 @@ func TestDerivedStats(t *testing.T) {
184190
Fifty: 50 * Nanosecond,
185191
Ninety: 90 * Nanosecond,
186192
NinetyFive: 95 * Nanosecond,
193+
// Since cpu is calculated directly from samples, we don't lose any samples.
194+
Count: N - 1,
187195
}
188196
if usage.Cpu != cpuExpected {
189197
t.Errorf("cpu stats are %+v. Expected %+v", usage.Cpu, cpuExpected)
@@ -195,6 +203,7 @@ func TestDerivedStats(t *testing.T) {
195203
Fifty: 50 * 1024,
196204
Ninety: 90 * 1024,
197205
NinetyFive: 95 * 1024,
206+
Count: N - 1,
198207
}
199208
if usage.Memory != memExpected {
200209
t.Errorf("memory stats are mean %+v. Expected %+v", usage.Memory, memExpected)

0 commit comments

Comments
 (0)