-
Notifications
You must be signed in to change notification settings - Fork 660
feat: Add metric to track GitHub app rate limit #4088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c2602d0
feat: Add metric to track GitHub app rate limit
npalm 4c136e2
docs: auto update terraform docs
forest-pr[bot] 0885b8c
refactor: rename gh-auth module
npalm 43a6b9f
fix example termination watcher
npalm 8cdb2b9
Apply suggestions from code review
npalm 93f23cd
typo
npalm c272393
docs: auto update terraform docs
forest-pr[bot] f36b4f2
cleanup before in scale-up test
npalm 9ef6d30
revert example
npalm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/control-plane/src/gh-auth/gh-octokit.ts → ...tions/control-plane/src/github/octokit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
lambdas/functions/control-plane/src/github/rate-limit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ResponseHeaders } from '@octokit/types'; | ||
import { createSingleMetric } from '@terraform-aws-github-runner/aws-powertools-util'; | ||
import { MetricUnit } from '@aws-lambda-powertools/metrics'; | ||
import { metricGitHubAppRateLimit } from './rate-limit'; | ||
|
||
process.env.PARAMETER_GITHUB_APP_ID_NAME = 'test'; | ||
jest.mock('@terraform-aws-github-runner/aws-ssm-util', () => ({ | ||
...jest.requireActual('@terraform-aws-github-runner/aws-ssm-util'), | ||
// get parameter name from process.env.PARAMETER_GITHUB_APP_ID_NAME rerunt 1234 | ||
getParameter: jest.fn((name: string) => { | ||
if (name === process.env.PARAMETER_GITHUB_APP_ID_NAME) { | ||
return '1234'; | ||
} else { | ||
return ''; | ||
} | ||
}), | ||
})); | ||
|
||
jest.mock('@terraform-aws-github-runner/aws-powertools-util', () => ({ | ||
...jest.requireActual('@terraform-aws-github-runner/aws-powertools-util'), | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
createSingleMetric: jest.fn((name: string, unit: string, value: number, dimensions?: Record<string, string>) => { | ||
return { | ||
addMetadata: jest.fn(), | ||
}; | ||
}), | ||
})); | ||
|
||
describe('metricGitHubAppRateLimit', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should update rate limit metric', async () => { | ||
// set process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT to true | ||
process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT = 'true'; | ||
const headers: ResponseHeaders = { | ||
'x-ratelimit-remaining': '10', | ||
'x-ratelimit-limit': '60', | ||
}; | ||
|
||
await metricGitHubAppRateLimit(headers); | ||
|
||
expect(createSingleMetric).toHaveBeenCalledWith('GitHubAppRateLimitRemaining', MetricUnit.Count, 10, { | ||
AppId: '1234', | ||
}); | ||
}); | ||
|
||
it('should not update rate limit metric', async () => { | ||
// set process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT to false | ||
process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT = 'false'; | ||
const headers: ResponseHeaders = { | ||
'x-ratelimit-remaining': '10', | ||
'x-ratelimit-limit': '60', | ||
}; | ||
|
||
await metricGitHubAppRateLimit(headers); | ||
|
||
expect(createSingleMetric).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not update rate limit metric if headers are undefined', async () => { | ||
// set process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT to true | ||
process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT = 'true'; | ||
|
||
await metricGitHubAppRateLimit(undefined as unknown as ResponseHeaders); | ||
|
||
expect(createSingleMetric).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ResponseHeaders } from '@octokit/types'; | ||
import { createSingleMetric, logger } from '@terraform-aws-github-runner/aws-powertools-util'; | ||
import { MetricUnit } from '@aws-lambda-powertools/metrics'; | ||
import yn from 'yn'; | ||
import { getParameter } from '@terraform-aws-github-runner/aws-ssm-util'; | ||
|
||
export async function metricGitHubAppRateLimit(headers: ResponseHeaders): Promise<void> { | ||
try { | ||
const remaining = parseInt(headers['x-ratelimit-remaining'] as string); | ||
const limit = parseInt(headers['x-ratelimit-limit'] as string); | ||
|
||
logger.debug(`Rate limit remaining: ${remaining}, limit: ${limit}`); | ||
|
||
const updateMetric = yn(process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT); | ||
if (updateMetric) { | ||
const appId = await getParameter(process.env.PARAMETER_GITHUB_APP_ID_NAME); | ||
const metric = createSingleMetric('GitHubAppRateLimitRemaining', MetricUnit.Count, remaining, { | ||
AppId: appId, | ||
}); | ||
metric.addMetadata('AppId', appId); | ||
} | ||
} catch (e) { | ||
logger.debug(`Error updating rate limit metric`, { error: e }); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.