Skip to content

Commit

Permalink
Write log entry when Azure DevOps API Rate Limit is (nearly) reached (#…
Browse files Browse the repository at this point in the history
…6285)

Signed-off-by: Ruben Herrebrugh <[email protected]>
  • Loading branch information
raherrebrugh authored Nov 3, 2024
1 parent c5b35bd commit ea6adc4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

- **General**: Add the generateEmbeddedObjectMeta flag to generate meta properties of JobTargetRef in ScaledJob ([#5908](https://github.com/kedacore/keda/issues/5908))
- **General**: Cache miss fallback in validating webhook for ScaledObjects with direct kubernetes client ([#5973](https://github.com/kedacore/keda/issues/5973))
- **Azure Pipelines Scalar**: Print warning to log when Azure DevOps API Rate Limits are (nearly) reached ([#6284](https://github.com/kedacore/keda/issues/6284))
- **CloudEventSource**: Introduce ClusterCloudEventSource ([#3533](https://github.com/kedacore/keda/issues/3533))
- **CloudEventSource**: Provide ClusterCloudEventSource around the management of ScaledJobs resources ([#3523](https://github.com/kedacore/keda/issues/3523))
- **CloudEventSource**: Provide ClusterCloudEventSource around the management of TriggerAuthentication/ClusterTriggerAuthentication resources ([#3524](https://github.com/kedacore/keda/issues/3524))
Expand Down
10 changes: 10 additions & 0 deletions pkg/scalers/azure_pipelines_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ func getAzurePipelineRequest(ctx context.Context, logger logr.Logger, urlString
return []byte{}, fmt.Errorf("the Azure DevOps REST API returned error. urlString: %s status: %d response: %s", urlString, r.StatusCode, string(b))
}

// Log when API Rate Limits are reached
rateLimitRemaining := r.Header[http.CanonicalHeaderKey("X-RateLimit-Remaining")]
if rateLimitRemaining != nil {
logger.V(1).Info(fmt.Sprintf("Warning: ADO TSTUs Left %s. When reaching zero requests are delayed, lower the polling interval. See https://learn.microsoft.com/en-us/azure/devops/integrate/concepts/rate-limits?view=azure-devops", rateLimitRemaining))
}
rateLimitDelay := r.Header[http.CanonicalHeaderKey("X-RateLimit-Delay")]
if rateLimitDelay != nil {
logger.V(1).Info(fmt.Sprintf("Warning: Request to ADO API is delayed by %s seconds. Sending additional requests will increase delay until results are being blocked entirely", rateLimitDelay))
}

return b, nil
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/scalers/azure_pipelines_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,34 @@ func TestAzurePipelinesMatchedAgent(t *testing.T) {
}
}

func TestAzurePipelinesDelayed(t *testing.T) {
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// nosemgrep: no-direct-write-to-responsewriter
w.Header().Add("X-RateLimit-Limit", "0")
// nosemgrep: no-direct-write-to-responsewriter
w.Header().Add("X-RateLimit-Delay", "42")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(buildLoadJSON())
}))

meta := getMatchedAgentMetaData(apiStub.URL)

mockAzurePipelinesScaler := azurePipelinesScaler{
metadata: meta,
httpClient: http.DefaultClient,
}

queueLen, err := mockAzurePipelinesScaler.GetAzurePipelinesQueueLength(context.Background())

if err != nil {
t.Fail()
}

if queueLen < 1 {
t.Fail()
}
}

func getDemandJobMetaData(url string) *azurePipelinesMetadata {
meta := getMatchedAgentMetaData(url)
meta.parent = ""
Expand Down

0 comments on commit ea6adc4

Please sign in to comment.