From 4a2f743325dda03eee5066574c98e88d0c18fae1 Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Thu, 7 Nov 2024 16:31:07 -0800 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 9354 (#23714) * Add step for emitting rate limits for a token * add step to eng/common sync template * Fix resource label output * Use propery name instead of nested 'resource' property * Add percent metric too * Add divide by zero safety --------- Co-authored-by: Patrick Hallisey --- .../steps/emit-rate-limit-metrics.yml | 36 +++++++++++++++++++ .../templates/steps/git-push-changes.yml | 5 +++ 2 files changed, 41 insertions(+) create mode 100644 eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml diff --git a/eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml b/eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml new file mode 100644 index 000000000000..1dd34a8da3cc --- /dev/null +++ b/eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml @@ -0,0 +1,36 @@ +parameters: + - name: GitHubUser + type: string + - name: GitHubToken + type: string + +steps: +- pwsh: | + $headers = @{ + "Authorization" = "Bearer $env:GITHUB_TOKEN" + "X-GitHub-Api-Version" = "2022-11-28" + } + + $response = Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' -Headers $headers -Method Get + $timestamp = Get-Date + foreach ($property in $response.resources.PSObject.Properties) + { + $labels = @{ user= $env:GITHUB_USER; resource= $property.Name } + + $remaining = $property.Value.remaining + $limit = $property.Value.limit + $used = $property.Value.used + + Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_remaining_total"; value= $remaining; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)" + Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_limit_total"; value= $limit; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)" + Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_used_total"; value= $used; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)" + + if ($limit -ne 0) { + $percent = $used / $limit * 100 + Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_used_percent"; value= $percent; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)" + } + } + displayName: Check GitHub Rate Limit + env: + GITHUB_TOKEN: ${{ parameters.GitHubToken}} + GITHUB_USER: ${{ parameters.GitHubUser}} \ No newline at end of file diff --git a/eng/common/pipelines/templates/steps/git-push-changes.yml b/eng/common/pipelines/templates/steps/git-push-changes.yml index c8fbeaa9769c..53d70fac45be 100644 --- a/eng/common/pipelines/templates/steps/git-push-changes.yml +++ b/eng/common/pipelines/templates/steps/git-push-changes.yml @@ -28,6 +28,11 @@ steps: condition: succeeded() workingDirectory: ${{ parameters.WorkingDirectory }} +- template: /eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml + parameters: + GitHubUser: azure-sdk + GitHubToken: $(azuresdk-github-pat) + - task: PowerShell@2 displayName: Push changes condition: and(succeeded(), eq(variables['HasChanges'], 'true'))