Skip to content

Commit

Permalink
allow neighbour app to send custom metrics without policy
Browse files Browse the repository at this point in the history
  • Loading branch information
asalan316 committed Sep 27, 2024
1 parent fcfb8af commit a4de8af
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/autoscaler/metricsforwarder/server/custom_metrics_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ var (
type CustomMetricsHandler struct {
metricForwarder forwarder.MetricForwarder
policyDB db.PolicyDB
bindingDB db.BindingDB
allowedMetricCache cache.Cache
cacheTTL time.Duration
logger lager.Logger
}

func NewCustomMetricsHandler(logger lager.Logger, metricForwarder forwarder.MetricForwarder, policyDB db.PolicyDB, allowedMetricCache cache.Cache) *CustomMetricsHandler {
func NewCustomMetricsHandler(logger lager.Logger, metricForwarder forwarder.MetricForwarder, policyDB db.PolicyDB, bindingDB db.BindingDB, allowedMetricCache cache.Cache) *CustomMetricsHandler {
return &CustomMetricsHandler{
metricForwarder: metricForwarder,
policyDB: policyDB,
bindingDB: bindingDB,
allowedMetricCache: allowedMetricCache,
logger: logger,
}
Expand Down Expand Up @@ -138,7 +140,17 @@ func (mh *CustomMetricsHandler) validateCustomMetricTypes(appGUID string, metric
// AllowedMetrics found in cache
allowedMetricTypeSet = res.(map[string]struct{})
} else {
// AllowedMetrics not found in cache, find AllowedMetrics from Database
// allow app with strategy as bound_app to submit metrics without policy
isAppWithBoundStrategy, err := mh.isAppWithBoundStrategy(appGUID)
if err != nil {
mh.logger.Error("error-finding-app-submission-strategy", err, lager.Data{"appId": appGUID})
return err
}
if isAppWithBoundStrategy {
mh.logger.Info("app-with-bound-strategy-found", lager.Data{"appId": appGUID})
return nil
}

scalingPolicy, err := mh.policyDB.GetAppPolicy(context.TODO(), appGUID)
if err != nil {
mh.logger.Error("error-getting-policy", err, lager.Data{"appId": appGUID})
Expand Down Expand Up @@ -172,6 +184,20 @@ func (mh *CustomMetricsHandler) validateCustomMetricTypes(appGUID string, metric
return nil
}

func (mh *CustomMetricsHandler) isAppWithBoundStrategy(appGUID string) (bool, error) {
// allow app with submission_strategy as bound_app to submit custom metrics even without policy
submissionStrategy, err := mh.bindingDB.GetCustomMetricStrategyByAppId(context.TODO(), appGUID)
if err != nil {
mh.logger.Error("error-getting-custom-metrics-strategy", err, lager.Data{"appId": appGUID})
return false, err
}
if submissionStrategy == "bound_app" {
mh.logger.Info("bounded-metrics-submission-strategy", lager.Data{"appId": appGUID, "submission_strategy": submissionStrategy})
return true, nil
}
return false, nil
}

func (mh *CustomMetricsHandler) getMetrics(appID string, metricsConsumer *models.MetricsConsumer) []*models.CustomMetric {
var metrics []*models.CustomMetric
for _, metric := range metricsConsumer.CustomMetrics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = Describe("MetricHandler", func() {
allowedMetricTypeSet map[string]struct{}

policyDB *fakes.FakePolicyDB
fakeBindingDB *fakes.FakeBindingDB
metricsforwarder *fakes.FakeMetricForwarder

resp *httptest.ResponseRecorder
Expand All @@ -46,12 +47,13 @@ var _ = Describe("MetricHandler", func() {
BeforeEach(func() {
logger := lager.NewLogger("metrichandler-test")
policyDB = &fakes.FakePolicyDB{}
fakeBindingDB = &fakes.FakeBindingDB{}
metricsforwarder = &fakes.FakeMetricForwarder{}
allowedMetricCache = *cache.New(10*time.Minute, -1)
allowedMetricTypeSet = make(map[string]struct{})
vars = make(map[string]string)
resp = httptest.NewRecorder()
handler = NewCustomMetricsHandler(logger, metricsforwarder, policyDB, allowedMetricCache)
handler = NewCustomMetricsHandler(logger, metricsforwarder, policyDB, fakeBindingDB, allowedMetricCache)
allowedMetricCache.Flush()
})

Expand Down Expand Up @@ -295,6 +297,24 @@ var _ = Describe("MetricHandler", func() {

})
})
When("neighbour app is bound to same autoscaler instance without policy", func() {
BeforeEach(func() {
fakeBindingDB.GetCustomMetricStrategyByAppIdReturns("bound_app", nil)
customMetrics := []*models.CustomMetric{
{
Name: "queuelength", Value: 12, Unit: "unit", InstanceIndex: 1, AppGUID: "an-app-id",
},
}
body, err = json.Marshal(models.MetricsConsumer{InstanceIndex: 0, CustomMetrics: customMetrics})
Expect(err).NotTo(HaveOccurred())
})

It("should returns status code 200", func() {
Expect(resp.Code).To(Equal(http.StatusOK))
Expect(fakeBindingDB.GetCustomMetricStrategyByAppIdCallCount()).To(Equal(1))

})
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler/metricsforwarder/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewServer(logger lager.Logger, conf *config.Config, policyDb db.PolicyDB, b
return nil, fmt.Errorf("failed to create metric forwarder: %w", err)
}

mh := NewCustomMetricsHandler(logger, *metricForwarder, policyDb, allowedMetricCache)
mh := NewCustomMetricsHandler(logger, *metricForwarder, policyDb, bindingDB, allowedMetricCache)
authenticator, err := auth.New(logger, credentials, bindingDB)
if err != nil {
return nil, fmt.Errorf("failed to create auth middleware: %w", err)
Expand Down

0 comments on commit a4de8af

Please sign in to comment.