Skip to content

Commit 8d41f8e

Browse files
author
Joe Bowers
committed
API endpoint for CPU and Memory usage history
1 parent a58945d commit 8d41f8e

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

src/app/backend/replicationcontrollerpodsmetrics.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"strings"
23+
"time"
2324

2425
heapster "k8s.io/heapster/api/v1/types"
2526
"k8s.io/kubernetes/pkg/api"
@@ -36,12 +37,23 @@ type ReplicationControllerMetricsByPod struct {
3637
MetricsMap map[string]PodMetrics `json:"metricsMap"`
3738
}
3839

40+
// Some sample measurement of a non-negative, integer quantity
41+
// (for example, memory usage in bytes observed at some moment)
42+
type MetricResult struct {
43+
Timestamp time.Time `json:"timestamp"`
44+
Value uint64 `json:"value"`
45+
}
46+
3947
// Pod metrics structure.
4048
type PodMetrics struct {
41-
// Cumulative CPU usage on all cores in nanoseconds.
49+
// Most recent measure of CPU usage on all cores in nanoseconds.
4250
CpuUsage *uint64 `json:"cpuUsage"`
4351
// Pod memory usage in bytes.
4452
MemoryUsage *uint64 `json:"memoryUsage"`
53+
// Timestamped samples of CpuUsage over some short period of history
54+
CpuUsageHistory []MetricResult `json:"cpuUsageHistory"`
55+
// Timestamped samples of pod memory usage over some short period of history
56+
MemoryUsageHistory []MetricResult `json:"memoryUsageHistory"`
4557
}
4658

4759
// Return Pods metrics for Replication Controller or error when occurred.
@@ -118,15 +130,33 @@ func createResponse(cpuMetrics []heapster.MetricResult, memMetrics []heapster.Me
118130
var cpuValue *uint64
119131
memMetricsList := memMetrics[iterator].Metrics
120132
cpuMetricsList := cpuMetrics[iterator].Metrics
133+
121134
if len(memMetricsList) > 0 {
122135
memValue = &memMetricsList[0].Value
123136
}
137+
124138
if len(cpuMetricsList) > 0 {
125139
cpuValue = &cpuMetricsList[0].Value
126140
}
141+
142+
cpuHistory := make([]MetricResult, len(cpuMetricsList))
143+
memHistory := make([]MetricResult, len(memMetricsList))
144+
145+
for i, cpuMeasure := range cpuMetricsList {
146+
cpuHistory[i].Value = cpuMeasure.Value
147+
cpuHistory[i].Timestamp = cpuMeasure.Timestamp
148+
}
149+
150+
for i, memMeasure := range memMetricsList {
151+
memHistory[i].Value = memMeasure.Value
152+
memHistory[i].Timestamp = memMeasure.Timestamp
153+
}
154+
127155
podResources := PodMetrics{
128-
CpuUsage: cpuValue,
129-
MemoryUsage: memValue,
156+
CpuUsage: cpuValue,
157+
MemoryUsage: memValue,
158+
CpuUsageHistory: cpuHistory,
159+
MemoryUsageHistory: memHistory,
130160
}
131161
replicationControllerPodsResources[podName] = podResources
132162
}

src/app/externs/backendapi.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ backendApi.Event;
106106
*/
107107
backendApi.ReplicationControllerList;
108108

109+
/**
110+
* @typedef {{
111+
* timestamp: string,
112+
* value: number
113+
* }}
114+
*/
115+
backendApi.MetricResult;
116+
117+
/**
118+
* @typedef {{
119+
* reason: string,
120+
* message: string
121+
* }}
122+
*/
123+
backendApi.PodEvent;
124+
125+
/**
126+
* @typedef {{
127+
* cpuUsage: ?number,
128+
* memoryUsage: ?number,
129+
* cpuUsageHistory: !Array<!backendApi.MetricResult>,
130+
* memoryUsageHistory: !Array<!backendApi.MetricResult>
131+
* }}
132+
*/
133+
backendApi.PodMetrics;
134+
109135
/**
110136
* @typedef {{
111137
* current: number,
@@ -170,7 +196,7 @@ backendApi.DeleteReplicationControllerSpec;
170196
* podIP: string,
171197
* nodeName: string,
172198
* restartCount: number,
173-
* metrics: {cpuUsage: ?number, memoryUsage: ?number}
199+
* metrics: backendApi.PodMetrics
174200
* }}
175201
*/
176202
backendApi.ReplicationControllerPod;

src/test/backend/replicationcontrollerpodsmetrics_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,23 @@ func TestCreateResponse(t *testing.T) {
107107
&ReplicationControllerMetricsByPod{
108108
MetricsMap: map[string]PodMetrics{
109109
"a": {
110-
CpuUsage: &cpuUsage1,
110+
CpuUsage: &cpuUsage1,
111+
CpuUsageHistory: []MetricResult{
112+
{Value: cpuUsage1},
113+
},
111114
MemoryUsage: &memoryUsage,
115+
MemoryUsageHistory: []MetricResult{
116+
{Value: memoryUsage},
117+
},
112118
}, "b": {
113-
CpuUsage: &cpuUsage2,
119+
CpuUsage: &cpuUsage2,
120+
CpuUsageHistory: []MetricResult{
121+
{Value: cpuUsage2},
122+
},
114123
MemoryUsage: &memoryUsage,
124+
MemoryUsageHistory: []MetricResult{
125+
{Value: memoryUsage},
126+
},
115127
},
116128
},
117129
},

0 commit comments

Comments
 (0)