Add step for emitting rate limits for a token (#9354)

* Add step for emitting rate limits for a token

* add step to eng/common sync template

* Move the step out of the per-repo work

* Fix resource label output

* Use propery name instead of nested 'resource' property

* Add percent metric too

* Add divide by zero safety
This commit is contained in:
Patrick Hallisey 2024-11-14 13:10:36 -08:00 коммит произвёл GitHub
Родитель 26ca7fa291
Коммит 91781f0dec
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 46 добавлений и 0 удалений

Просмотреть файл

@ -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}}

Просмотреть файл

@ -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'))

Просмотреть файл

@ -170,3 +170,8 @@ steps:
PushArgs: -f
WorkingDirectory: $(System.DefaultWorkingDirectory)/${{ repo }}
ScriptDirectory: $(System.DefaultWorkingDirectory)/$(Build.Repository.Name)/${{ parameters.ScriptDirectory }}
- template: /eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml
parameters:
GitHubUser: azure-sdk
GitHubToken: $(azuresdk-github-pat)