Skip to content

Commit

Permalink
🕵️
Browse files Browse the repository at this point in the history
  • Loading branch information
geigerj0 committed Apr 23, 2024
1 parent 1042352 commit 8295cd0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/acceptance/app/dynamic_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var _ = Describe("AutoScaler dynamic policy", func() {
initialInstanceCount = 1
})

// todo: break this test so that it fails when reading only 100 envelopes form log-cache via rest-api
JustBeforeEach(func() {
ticker = time.NewTicker(10 * time.Second)
go func(chan bool) {
Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler/eventgenerator/aggregator/metric_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (m *MetricPoller) retrieveMetric(appMonitor *models.AppMonitor) error {
metrics, err := m.metricClient.GetMetrics(appId, metricType, startTime, endTime)
m.logger.Debug("received metrics from metricClient", lager.Data{"retrievedMetrics": metrics})
if err != nil {
return fmt.Errorf("retriveMetric Failed: %w", err)
return fmt.Errorf("retrieveMetric Failed: %w", err)
}
avgMetric := m.aggregate(appId, metricType, metrics)
m.logger.Debug("save-aggregated-appmetric", lager.Data{"appMetric": avgMetric})
Expand Down
59 changes: 26 additions & 33 deletions src/autoscaler/eventgenerator/client/log_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,52 +138,45 @@ func (c *LogCacheClient) GetMetrics(appId string, metricType string, startTime t
return []models.AppInstanceMetric{}, fmt.Errorf("result does not contain a vector")
}

// safeguard: the query ensures that we get a sample but let's double-check
if len(vector.GetSamples()) > 1 {
return []models.AppInstanceMetric{}, fmt.Errorf("vector contains more than one sample")
}

// return empty metrics if there are no samples
if len(vector.GetSamples()) <= 0 {
return c.emptyAppInstanceMetrics(appId, models.MetricNameThroughput, models.UnitRPS, now)
}

sample := vector.GetSamples()[0]

// safeguard: metric label instance_id should be always there but let's double-check
instanceIdStr, ok := sample.GetMetric()["instance_id"]
if !ok {
return []models.AppInstanceMetric{}, fmt.Errorf("sample does not contain instance_id: %w", err)
}

// convert result into autoscaler metric model
instanceIdUInt, err := strconv.ParseUint(instanceIdStr, 10, 32)
if err != nil {
return []models.AppInstanceMetric{}, fmt.Errorf("could not convert instance_id to uint32: %w", err)
}

// safeguard: the query ensures that we get a point
point := sample.GetPoint()
if point == nil {
return []models.AppInstanceMetric{}, fmt.Errorf("sample does not contain a point")
}

c.logger.Info("get-metrics-promql-result", lager.Data{"value": fmt.Sprintf("%f", point.GetValue())})

instanceId := uint32(instanceIdUInt)
valueWithoutDecimalsRoundedToCeiling := fmt.Sprintf("%.0f", math.Ceil(point.GetValue()))

return []models.AppInstanceMetric{
{
var metrics []models.AppInstanceMetric
for _, sample := range vector.GetSamples() {
// safeguard: metric label instance_id should be always there but let's double-check
instanceIdStr, ok := sample.GetMetric()["instance_id"]
if !ok {
return []models.AppInstanceMetric{}, fmt.Errorf("sample does not contain instance_id: %w", err)
}

instanceIdUInt, err := strconv.ParseUint(instanceIdStr, 10, 32)
if err != nil {
return []models.AppInstanceMetric{}, fmt.Errorf("could not convert instance_id to uint32: %w", err)
}

// safeguard: the query ensures that we get a point but let's double-check
point := sample.GetPoint()
if point == nil {
return []models.AppInstanceMetric{}, fmt.Errorf("sample does not contain a point")
}

instanceId := uint32(instanceIdUInt)
valueWithoutDecimalsRoundedToCeiling := fmt.Sprintf("%.0f", math.Ceil(point.GetValue()))

metrics = append(metrics, models.AppInstanceMetric{
AppId: appId,
InstanceIndex: instanceId,
Name: metricType,
Unit: metricTypeUnit,
Value: valueWithoutDecimalsRoundedToCeiling,
CollectedAt: now.UnixNano(),
Timestamp: now.UnixNano(),
},
}, nil
})
}
return metrics, nil
}

filters := logCacheFiltersFor(endTime, metricType)
Expand Down
32 changes: 21 additions & 11 deletions src/autoscaler/eventgenerator/client/log_cache_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ var _ = Describe("LogCacheClient", func() {

JustBeforeEach(func() {
fakeGoLogCacheReader.ReadReturns(envelopes, logCacheClientReadError)
fakeEnvelopeProcessor.GetTimerMetricsReturns(metrics)
fakeEnvelopeProcessor.GetGaugeMetricsReturnsOnCall(0, metrics, nil)
fakeEnvelopeProcessor.GetGaugeMetricsReturnsOnCall(1, nil, errors.New("some error"))

Expand Down Expand Up @@ -248,6 +247,14 @@ var _ = Describe("LogCacheClient", func() {
Value: 123,
},
},
{
Metric: map[string]string{
"instance_id": "1",
},
Point: &logcache_v1.PromQL_Point{
Value: 321,
},
},
},
},
},
Expand All @@ -256,14 +263,19 @@ var _ = Describe("LogCacheClient", func() {
metrics, err := logCacheClient.GetMetrics("app-id", "throughput", startTime, endTime)

Expect(err).To(Not(HaveOccurred()))
Expect(metrics).To(HaveLen(1))

metric := metrics[0]
Expect(metric.AppId).To(Equal("app-id"))
Expect(metric.InstanceIndex).To(Equal(uint32(0)))
Expect(metric.Name).To(Equal("throughput"))
Expect(metric.Unit).To(Equal("rps"))
Expect(metric.Value).To(Equal("123"))
Expect(metrics).To(HaveLen(2))

Expect(metrics[0].AppId).To(Equal("app-id"))
Expect(metrics[0].InstanceIndex).To(Equal(uint32(0)))
Expect(metrics[0].Name).To(Equal("throughput"))
Expect(metrics[0].Unit).To(Equal("rps"))
Expect(metrics[0].Value).To(Equal("123"))

Expect(metrics[1].AppId).To(Equal("app-id"))
Expect(metrics[1].InstanceIndex).To(Equal(uint32(1)))
Expect(metrics[1].Name).To(Equal("throughput"))
Expect(metrics[1].Unit).To(Equal("rps"))
Expect(metrics[1].Value).To(Equal("321"))
})
})

Expand Down Expand Up @@ -294,8 +306,6 @@ var _ = Describe("LogCacheClient", func() {
// after starTime and envelopeType we filter the metric names
Expect(valuesFrom(readOptions[2])["name_filter"][0]).To(Equal(requiredFilters[2]))

Expect(fakeEnvelopeProcessor.GetTimerMetricsCallCount()).To(Equal(0))

By("Sends the right arguments to the gauge processor")
actualEnvelopes, actualCurrentTimestamp := fakeEnvelopeProcessor.GetGaugeMetricsArgsForCall(0)
Expect(actualEnvelopes).To(Equal(envelopes))
Expand Down

0 comments on commit 8295cd0

Please sign in to comment.