Skip to content

Commit

Permalink
Set metrics value between 0 and 1 to 1 (cloudfoundry#564)
Browse files Browse the repository at this point in the history
* change metrics value (0,1) to 1

* add test cases for metrics values update
  • Loading branch information
aqan213 authored May 7, 2020
1 parent 8aef743 commit f1d64d9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/autoscaler/eventgenerator/aggregator/metricPoller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strconv"
"time"
"math"

"code.cloudfoundry.org/lager"
)
Expand Down Expand Up @@ -122,7 +123,7 @@ func (m *MetricPoller) aggregate(appId string, metricType string, metrics []*mod
}
}

avgValue := int64(float64(sum)/float64(count) + 0.5)
avgValue := int64(math.Ceil(float64(sum)/float64(count)))
return &models.AppMetric{
AppId: appId,
MetricType: metricType,
Expand All @@ -131,3 +132,4 @@ func (m *MetricPoller) aggregate(appId string, metricType string, metrics []*mod
Timestamp: timestamp,
}
}

5 changes: 3 additions & 2 deletions src/autoscaler/eventgenerator/aggregator/metricPoller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var _ = Describe("MetricPoller", func() {
CollectedAt: 222222,
Name: testMetricType,
Unit: testMetricUnit,
Value: "400",
Value: "401",
Timestamp: 220000,
},
}
Expand Down Expand Up @@ -147,7 +147,7 @@ var _ = Describe("MetricPoller", func() {
Expect(appMetric).To(Equal(&models.AppMetric{
AppId: testAppId,
MetricType: testMetricType,
Value: "250",
Value: "251",
Unit: testMetricUnit,
Timestamp: timestamp}))
})
Expand Down Expand Up @@ -224,3 +224,4 @@ var _ = Describe("MetricPoller", func() {
})
})
})

14 changes: 8 additions & 6 deletions src/autoscaler/metricsserver/collector/envelope_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strconv"
"time"
"math"

"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (ep *envelopeProcessor) processContainerMetrics(appID string, instanceIndex
CollectedAt: ep.clock.Now().UnixNano(),
Name: models.MetricNameMemoryUsed,
Unit: models.UnitMegaBytes,
Value: fmt.Sprintf("%d", int(memory.GetValue()/(1024*1024)+0.5)),
Value: fmt.Sprintf("%d", int(math.Ceil(memory.GetValue()/(1024*1024)))),
Timestamp: timestamp,
}
ep.metricChan <- memoryUsedMetric
Expand All @@ -116,7 +117,7 @@ func (ep *envelopeProcessor) processContainerMetrics(appID string, instanceIndex
CollectedAt: ep.clock.Now().UnixNano(),
Name: models.MetricNameMemoryUtil,
Unit: models.UnitPercentage,
Value: fmt.Sprintf("%d", int(memory.GetValue()/memoryQuota.GetValue()*100+0.5)),
Value: fmt.Sprintf("%d", int(math.Ceil(memory.GetValue()/memoryQuota.GetValue()*100))),
Timestamp: timestamp,
}
ep.metricChan <- memoryUtilMetric
Expand All @@ -131,7 +132,7 @@ func (ep *envelopeProcessor) processContainerMetrics(appID string, instanceIndex
CollectedAt: ep.clock.Now().UnixNano(),
Name: models.MetricNameCPUUtil,
Unit: models.UnitPercentage,
Value: fmt.Sprintf("%d", int64(cpu.GetValue()+0.5)),
Value: fmt.Sprintf("%d", int64(math.Ceil(cpu.GetValue()))),
Timestamp: timestamp,
}
ep.metricChan <- cpuMetric
Expand Down Expand Up @@ -159,7 +160,7 @@ func (ep *envelopeProcessor) processCustomMetrics(appID string, instanceIndex ui
CollectedAt: ep.clock.Now().UnixNano(),
Name: n,
Unit: v.Unit,
Value: fmt.Sprintf("%d", int64(v.Value+0.5)),
Value: fmt.Sprintf("%d", int64(math.Ceil(v.Value))),
Timestamp: timestamp,
}
ep.metricChan <- customMetric
Expand Down Expand Up @@ -204,7 +205,7 @@ func (ep *envelopeProcessor) computeAndSaveMetrics() {
CollectedAt: ep.clock.Now().UnixNano(),
Name: models.MetricNameThroughput,
Unit: models.UnitRPS,
Value: fmt.Sprintf("%d", int(float64(numReq)/ep.collectInterval.Seconds()+0.5)),
Value: fmt.Sprintf("%d", int(math.Ceil(float64(numReq)/ep.collectInterval.Seconds()))),
Timestamp: ep.clock.Now().UnixNano(),
}
ep.metricChan <- throughputMetric
Expand All @@ -215,7 +216,7 @@ func (ep *envelopeProcessor) computeAndSaveMetrics() {
CollectedAt: ep.clock.Now().UnixNano(),
Name: models.MetricNameResponseTime,
Unit: models.UnitMilliseconds,
Value: fmt.Sprintf("%d", ep.sumReponseTimes[appID][instanceIdx]/(numReq*1000*1000)),
Value: fmt.Sprintf("%d", int64(math.Ceil(float64(ep.sumReponseTimes[appID][instanceIdx])/float64((numReq*1000*1000))))),
Timestamp: ep.clock.Now().UnixNano(),
}
ep.metricChan <- responseTimeMetric
Expand All @@ -225,3 +226,4 @@ func (ep *envelopeProcessor) computeAndSaveMetrics() {
ep.sumReponseTimes = map[string]map[uint32]int64{}

}

51 changes: 48 additions & 3 deletions src/autoscaler/metricsserver/collector/envelope_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var _ = Describe("EnvelopeProcessor", func() {
Context("processing container metrics", func() {
BeforeEach(func() {
Expect(envelopeChan).Should(BeSent(GenerateContainerMetrics("test-app-id", "0", 10.2, 10*1024*1024, 20*1024*1024, 1111)))
Expect(envelopeChan).Should(BeSent(GenerateContainerMetrics("test-app-id", "1", 10.6, 10.2*1024*1024, 20*1024*1024, 1111)))

})
It("sends standard app instance metrics to channel", func() {
Eventually(metricChan).Should(Receive(Equal(&models.AppInstanceMetric{
Expand Down Expand Up @@ -81,15 +83,47 @@ var _ = Describe("EnvelopeProcessor", func() {
CollectedAt: fclock.Now().UnixNano(),
Name: models.MetricNameCPUUtil,
Unit: models.UnitPercentage,
Value: "10",
Value: "11",
Timestamp: 1111,
})))

Eventually(metricChan).Should(Receive(Equal(&models.AppInstanceMetric{
AppId: "test-app-id",
InstanceIndex: 1,
CollectedAt: fclock.Now().UnixNano(),
Name: models.MetricNameMemoryUsed,
Unit: models.UnitMegaBytes,
Value: "11",
Timestamp: 1111,
})))

Eventually(metricChan).Should(Receive(Equal(&models.AppInstanceMetric{
AppId: "test-app-id",
InstanceIndex: 1,
CollectedAt: fclock.Now().UnixNano(),
Name: models.MetricNameMemoryUtil,
Unit: models.UnitPercentage,
Value: "51",
Timestamp: 1111,
})))

Eventually(metricChan).Should(Receive(Equal(&models.AppInstanceMetric{
AppId: "test-app-id",
InstanceIndex: 1,
CollectedAt: fclock.Now().UnixNano(),
Name: models.MetricNameCPUUtil,
Unit: models.UnitPercentage,
Value: "11",
Timestamp: 1111,
})))

})
})

Context("processing custom metrics", func() {
BeforeEach(func() {
Expect(envelopeChan).Should(BeSent(GenerateCustomMetrics("test-app-id", "1", "custom_name", "custom_unit", 11.88, 1111)))
Expect(envelopeChan).Should(BeSent(GenerateCustomMetrics("test-app-id", "0", "custom_name", "custom_unit", 11.08, 1111)))
})
It("sends standard app instance metrics to channel", func() {
Eventually(metricChan).Should(Receive(Equal(&models.AppInstanceMetric{
Expand All @@ -102,6 +136,16 @@ var _ = Describe("EnvelopeProcessor", func() {
Timestamp: 1111,
})))

Eventually(metricChan).Should(Receive(Equal(&models.AppInstanceMetric{
AppId: "test-app-id",
InstanceIndex: 0,
CollectedAt: fclock.Now().UnixNano(),
Name: "custom_name",
Unit: "custom_unit",
Value: "12",
Timestamp: 1111,
})))

})
})

Expand All @@ -110,7 +154,7 @@ var _ = Describe("EnvelopeProcessor", func() {
getAppIDs = func() map[string]bool {
return map[string]bool{"test-app-id": true}
}
Expect(envelopeChan).Should(BeSent(GenerateHttpStartStopEnvelope("test-app-id", "0", 10*1000*1000, 20*1000*1000, 1111)))
Expect(envelopeChan).Should(BeSent(GenerateHttpStartStopEnvelope("test-app-id", "0", 10*1000*1000, 25*1000*1000, 1111)))
Expect(envelopeChan).Should(BeSent(GenerateHttpStartStopEnvelope("test-app-id", "1", 10*1000*1000, 30*1000*1000, 1111)))
Expect(envelopeChan).Should(BeSent(GenerateHttpStartStopEnvelope("test-app-id", "0", 20*1000*1000, 30*1000*1000, 1111)))
Expect(envelopeChan).Should(BeSent(GenerateHttpStartStopEnvelope("test-app-id", "1", 20*1000*1000, 50*1000*1000, 1111)))
Expand Down Expand Up @@ -143,7 +187,7 @@ var _ = Describe("EnvelopeProcessor", func() {
CollectedAt: fclock.Now().UnixNano(),
Name: models.MetricNameResponseTime,
Unit: models.UnitMilliseconds,
Value: "10",
Value: "13",
Timestamp: fclock.Now().UnixNano(),
}))

Expand Down Expand Up @@ -315,3 +359,4 @@ func GenerateHttpStartStopEnvelope(sourceID, instance string, start, stop, times
}
return e
}

0 comments on commit f1d64d9

Please sign in to comment.