-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add metric to track GitHub app rate limit (#4088)
## Description This PR adds an optional metric to keep track of the remaining rate limit for teh GItHub app. ## Notes - Refactored the metric configuration to align the metric configuration usages in all submodules. All changed are only impacting experimental features. Which means non breaking. - Refactored nameing gh-auth package, see separate commit. --------- Co-authored-by: forest-pr|bot <forest-pr[bot]@users.noreply.github.com> Co-authored-by: Stuart Pearson <1926002+stuartp44@users.noreply.github.com>
- Loading branch information
1 parent
9fc5dbc
commit d7cdaed
Showing
44 changed files
with
441 additions
and
182 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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.