Skip to content

Commit d3bd054

Browse files
committed
Capture githubRateLimitRemaining metric in CloudWatch
1 parent 90f44b4 commit d3bd054

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

package-lock.json

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"dependencies": {
4242
"@aws-lambda-powertools/logger": "2.x",
43+
"@aws-lambda-powertools/metrics": "2.x",
4344
"@aws-lambda-powertools/parameters": "2.x",
4445
"@aws-lambda-powertools/tracer": "2.x",
4546
"@aws-sdk/client-athena": "3.x",

src/app/domain/github/crawler/crawlInstallation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { crawlRepositories } from './crawlRepositories'
55
import { crawlPushes } from './crawlPushes'
66
import { crawlWorkflowRunEvents } from './crawlRunEvents'
77
import { logger } from '../../../util/logging'
8+
import { publishGithubInstallationClientMetrics } from '../../../outboundInterfaces/githubInstallationClient'
89

910
export async function crawlInstallation(
1011
appState: AppState,
@@ -14,7 +15,7 @@ export async function crawlInstallation(
1415
logger.info(`Crawling Installation for ${installation.accountLogin}`)
1516
const githubInstallationClient = appState.githubClient.clientForInstallation(installation.installationId)
1617
await crawlUsers(appState, installation, githubInstallationClient)
17-
logger.info('Github Metadata after crawling users', { ...githubInstallationClient.meta() })
18+
publishGithubInstallationClientMetrics(installation, githubInstallationClient)
1819
const repos = await crawlRepositories(appState, installation, githubInstallationClient)
1920
// Eventually consider doing some parallelization here (or move back to step function) but
2021
// need to be careful since GitHub gets twitchy about concurrent requests to the API
@@ -24,6 +25,6 @@ export async function crawlInstallation(
2425
await crawlPushes(appState, installation, repo, githubInstallationClient)
2526
await crawlWorkflowRunEvents(appState, installation, repo, lookbackDays, githubInstallationClient)
2627
}
27-
28+
publishGithubInstallationClientMetrics(installation, githubInstallationClient)
2829
logger.info('Github Metadata after crawls', { ...githubInstallationClient.meta() })
2930
}

src/app/middleware/standardMiddleware.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware'
22
import { logger } from '../util/logging'
33
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware'
44
import { tracer } from '../util/tracing'
5+
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware'
6+
import { metrics } from '../util/metrics'
57

68
const loggingMiddleware = injectLambdaContext(logger)
9+
const metricsMiddleware = logMetrics(metrics, { captureColdStartMetric: false, throwOnEmptyMetrics: false })
710
const tracingMiddleware = captureLambdaHandler(tracer)
811

9-
export const powertoolsMiddlewares = [loggingMiddleware, tracingMiddleware]
12+
export const powertoolsMiddlewares = [loggingMiddleware, metricsMiddleware, tracingMiddleware]

src/app/outboundInterfaces/githubInstallationClient.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { RawGithubWorkflowRunEvent } from '../domain/types/rawGithub/RawGithubWo
44
import { RawGithubRepository } from '../domain/types/rawGithub/RawGithubRepository'
55
import { RawGithubUser } from '../domain/types/rawGithub/RawGithubUser'
66
import { RawGithubEvent } from '../domain/types/rawGithub/RawGithubEvent'
7+
import { GithubInstallation } from '../domain/types/GithubInstallation'
8+
import { metrics } from '../util/metrics'
9+
import { MetricUnit } from '@aws-lambda-powertools/metrics'
710

811
export interface GithubInstallationClient {
912
listWorkflowRunsForRepo(owner: string, repo: string, created?: string): Promise<RawGithubWorkflowRunEvent[]>
@@ -127,3 +130,17 @@ export type OctokitResponseHeaders = {
127130
'x-ratelimit-reset'?: string
128131
'x-ratelimit-used'?: string
129132
}
133+
134+
// ToEventually - move this into the actual GithubInstallationClient object
135+
export function publishGithubInstallationClientMetrics(
136+
installation: GithubInstallation,
137+
githubInstallationClient: GithubInstallationClient
138+
) {
139+
const rateLimitMetric = metrics.singleMetric()
140+
rateLimitMetric.addDimension('installationAccount', installation.accountLogin)
141+
rateLimitMetric.addMetric(
142+
'githubRateLimitRemaining',
143+
MetricUnit.Count,
144+
githubInstallationClient.meta().ratelimitRemaining
145+
)
146+
}

src/app/util/metrics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Metrics } from '@aws-lambda-powertools/metrics'
2+
3+
// Configuration in Lambda env is defined in CDK for when this is used in a Lambda environment
4+
// E.g. look for POWERTOOLS_ ... env vars
5+
export const metrics = new Metrics({})

0 commit comments

Comments
 (0)