Skip to content

Commit 817434d

Browse files
azure-sdkhallipr
andauthored
Sync eng/common directory with azure-sdk-tools for PR 9354 (Azure#1905)
* 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 <[email protected]>
1 parent 8accf95 commit 817434d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
parameters:
2+
- name: GitHubUser
3+
type: string
4+
- name: GitHubToken
5+
type: string
6+
7+
steps:
8+
- pwsh: |
9+
$headers = @{
10+
"Authorization" = "Bearer $env:GITHUB_TOKEN"
11+
"X-GitHub-Api-Version" = "2022-11-28"
12+
}
13+
14+
$response = Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' -Headers $headers -Method Get
15+
$timestamp = Get-Date
16+
foreach ($property in $response.resources.PSObject.Properties)
17+
{
18+
$labels = @{ user= $env:GITHUB_USER; resource= $property.Name }
19+
20+
$remaining = $property.Value.remaining
21+
$limit = $property.Value.limit
22+
$used = $property.Value.used
23+
24+
Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_remaining_total"; value= $remaining; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)"
25+
Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_limit_total"; value= $limit; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)"
26+
Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_used_total"; value= $used; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)"
27+
28+
if ($limit -ne 0) {
29+
$percent = $used / $limit * 100
30+
Write-Host "logmetric: $( [ordered]@{ name= "github_ratelimit_used_percent"; value= $percent; timestamp= $timestamp; labels= $labels } | ConvertTo-Json -Compress)"
31+
}
32+
}
33+
displayName: Check GitHub Rate Limit
34+
env:
35+
GITHUB_TOKEN: ${{ parameters.GitHubToken}}
36+
GITHUB_USER: ${{ parameters.GitHubUser}}

eng/common/pipelines/templates/steps/git-push-changes.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ steps:
2828
condition: succeeded()
2929
workingDirectory: ${{ parameters.WorkingDirectory }}
3030

31+
- template: /eng/common/pipelines/templates/steps/emit-rate-limit-metrics.yml
32+
parameters:
33+
GitHubUser: azure-sdk
34+
GitHubToken: $(azuresdk-github-pat)
35+
3136
- task: PowerShell@2
3237
displayName: Push changes
3338
condition: and(succeeded(), eq(variables['HasChanges'], 'true'))

0 commit comments

Comments
 (0)