Skip to content

Commit

Permalink
Make pod metrics sum to aggregate resouce usage graphs (kubernetes#1381)
Browse files Browse the repository at this point in the history
The issue was with different metrics downloaded in the list and in the graphs. The other problem was that "current usage" was off by 15 mins.

Re kubernetes#1346
  • Loading branch information
bryk authored and rf232 committed Oct 31, 2016
1 parent 3fe627b commit dd3ef7d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/app/backend/resource/common/podmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
)

const (
cpuUsage = "cpu-usage"
memoryUsage = "memory-usage"
CpuUsage = "cpu/usage_rate"
MemoryUsage = "memory/usage"
)

// MetricsByPod is a metrics map by pod name.
Expand Down Expand Up @@ -66,8 +66,8 @@ func getPodListMetrics(podNamesByNamespace map[string][]string,
result := &MetricsByPod{MetricsMap: make(map[string]map[string]PodMetrics)}

for namespace, podNames := range podNamesByNamespace {
metricCPUUsagePath := createMetricPath(namespace, podNames, cpuUsage)
metricMemUsagePath := createMetricPath(namespace, podNames, memoryUsage)
metricCPUUsagePath := createMetricPath(namespace, podNames, CpuUsage)
metricMemUsagePath := createMetricPath(namespace, podNames, MemoryUsage)

resultCPUUsageRaw, err := getRawMetrics(heapsterClient, metricCPUUsagePath)
if err != nil {
Expand Down Expand Up @@ -138,11 +138,11 @@ func fillPodMetrics(cpuMetrics []heapster.MetricResult, memMetrics []heapster.Me
cpuMetricsList := cpuMetrics[iterator].Metrics

if len(memMetricsList) > 0 {
memValue = &memMetricsList[0].Value
memValue = &memMetricsList[len(memMetricsList) - 1].Value
}

if len(cpuMetricsList) > 0 {
cpuValue = &cpuMetricsList[0].Value
cpuValue = &cpuMetricsList[len(cpuMetricsList) - 1].Value
}

cpuHistory := make([]MetricResult, len(cpuMetricsList))
Expand Down
3 changes: 2 additions & 1 deletion src/app/backend/resource/dataselect/dataselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func GenericDataSelect(dataList []DataCell, dsQuery *DataSelectQuery) []DataCell
}

// GenericDataSelect takes a list of GenericDataCells and DataSelectQuery and returns selected data as instructed by dsQuery.
func GenericDataSelectWithMetrics(dataList []DataCell, dsQuery *DataSelectQuery, cachedResources *CachedResources, heapsterClient *client.HeapsterClient) ([]DataCell, metric.MetricPromises) {
func GenericDataSelectWithMetrics(dataList []DataCell, dsQuery *DataSelectQuery,
cachedResources *CachedResources, heapsterClient *client.HeapsterClient) ([]DataCell, metric.MetricPromises) {
SelectableData := DataSelector{
GenericDataList: dataList,
DataSelectQuery: dsQuery,
Expand Down
4 changes: 3 additions & 1 deletion src/app/backend/resource/dataselect/dataselectquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package dataselect

import (
"github.com/kubernetes/dashboard/src/app/backend/resource/metric"
"github.com/kubernetes/dashboard/src/app/backend/resource/common"
)

// Options for GenericDataSelect which takes []GenericDataCell and returns selected data.
Expand All @@ -32,7 +33,8 @@ var NoMetrics = NewMetricQuery(nil, nil)

// StandardMetrics query results in a standard metrics being returned.
// standard metrics are: cpu usage, memory usage and aggregation = sum.
var StandardMetrics = NewMetricQuery([]string{"cpu/usage_rate", "memory/usage"}, metric.OnlySumAggregation)
var StandardMetrics = NewMetricQuery([]string{common.CpuUsage, common.MemoryUsage},
metric.OnlySumAggregation)

// MetricQuery holds parameters for metric extraction process.
// It accepts list of metrics to be downloaded and a list of aggregations that should be performed for each metric.
Expand Down
7 changes: 5 additions & 2 deletions src/app/backend/resource/pod/podlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,19 @@ func CreatePodList(pods []api.Pod, dsQuery *dataselect.DataSelectQuery,
ListMeta: common.ListMeta{TotalItems: len(pods)},
}

cache := &dataselect.CachedResources{Pods: pods}

podCells, cumulativeMetricsPromises := dataselect.GenericDataSelectWithMetrics(toCells(pods), dsQuery,
dataselect.NoResourceCache, &heapsterClient)
cache, &heapsterClient)
pods = fromCells(podCells)

for _, pod := range pods {
podDetail := ToPod(&pod, metrics)
podList.Pods = append(podList.Pods, podDetail)
}

}
cumulativeMetrics, err := cumulativeMetricsPromises.GetMetrics()

podList.CumulativeMetrics = cumulativeMetrics
if err != nil {
podList.CumulativeMetrics = make([]metric.Metric, 0)
Expand Down
10 changes: 8 additions & 2 deletions src/test/backend/resource/common/podmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func TestUnmarshalMetrics(t *testing.T) {
func TestCreateResponse(t *testing.T) {
var cpuUsage1 uint64 = 1
var cpuUsage2 uint64 = 2
var cpuUsage3 uint64 = 3
var memoryUsage uint64 = 6131712
var memoryUsage2 uint64 = 6131713
cases := []struct {
cpuMetrics []heapster.MetricResult
memMetrics []heapster.MetricResult
Expand Down Expand Up @@ -89,11 +91,13 @@ func TestCreateResponse(t *testing.T) {
}},
{Metrics: []heapster.MetricPoint{
{Value: cpuUsage2},
{Value: cpuUsage3},
}},
},
[]heapster.MetricResult{
{Metrics: []heapster.MetricPoint{
{Value: memoryUsage},
{Value: memoryUsage2},
}},
{Metrics: []heapster.MetricPoint{
{Value: memoryUsage},
Expand All @@ -106,14 +110,16 @@ func TestCreateResponse(t *testing.T) {
CPUUsageHistory: []MetricResult{
{Value: cpuUsage1},
},
MemoryUsage: &memoryUsage,
MemoryUsage: &memoryUsage2,
MemoryUsageHistory: []MetricResult{
{Value: memoryUsage},
{Value: memoryUsage2},
},
}, "b": {
CPUUsage: &cpuUsage2,
CPUUsage: &cpuUsage3,
CPUUsageHistory: []MetricResult{
{Value: cpuUsage2},
{Value: cpuUsage3},
},
MemoryUsage: &memoryUsage,
MemoryUsageHistory: []MetricResult{
Expand Down

0 comments on commit dd3ef7d

Please sign in to comment.