Skip to content

Commit

Permalink
Send a list of request ids to monorail
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacroldan committed Dec 17, 2024
1 parent 720ad9c commit dda7c7a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/cli-kit/src/private/node/request-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Manages collection of request IDs during command execution
*/
export class RequestIDCollection {

Check failure on line 4 in packages/cli-kit/src/private/node/request-ids.ts

View workflow job for this annotation

GitHub Actions / knip-reporter-annotations-check

packages/cli-kit/src/private/node/request-ids.ts#L4

'RequestIDCollection' is an unused export
private static instance: RequestIDCollection

static getInstance(): RequestIDCollection {
if (!RequestIDCollection.instance) {
RequestIDCollection.instance = new RequestIDCollection()
}
return RequestIDCollection.instance
}

// We only report the last 1000 request IDs.
private readonly maxRequestIds = 1000
private requestIds: string[] = []

private constructor() {}

/**
* Add a request ID to the collection
*/
addRequestId(requestId: string | undefined | null) {
if (requestId) {
this.requestIds.push(requestId)
}
}

/**
* Get all collected request IDs as a comma-separated string
*/
getRequestIds(): string[] {
return this.requestIds.slice(-this.maxRequestIds)
}

/**
* Clear all stored request IDs
*/
clear() {
this.requestIds = []
}
}

export const requestIdsCollection = RequestIDCollection.getInstance()
2 changes: 2 additions & 0 deletions packages/cli-kit/src/public/node/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {recordMetrics} from '../../private/node/otel-metrics.js'
import {runWithRateLimit} from '../../private/node/conf-store.js'
import {reportingRateLimit} from '../../private/node/constants.js'
import {getLastSeenUserIdAfterAuth} from '../../private/node/session.js'
import {requestIdsCollection} from '../../private/node/request-ids.js'
import {Interfaces} from '@oclif/core'

export type CommandExitMode =
Expand Down Expand Up @@ -154,6 +155,7 @@ async function buildPayload({config, errorMessage, exitMode}: ReportAnalyticsEve
cmd_all_timing_active_ms: totalTimeWithoutSubtimers,
cmd_all_exit: exitMode,
user_id: await getLastSeenUserIdAfterAuth(),
request_ids: requestIdsCollection.getRequestIds(),
},
sensitive: {
args: startArgs.join(' '),
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-kit/src/public/node/api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {buildHeaders, httpsAgent} from '../../../private/node/api/headers.js'
import {debugLogRequestInfo, errorHandler} from '../../../private/node/api/graphql.js'
import {addPublicMetadata, runWithTimer} from '../metadata.js'
import {retryAwareRequest} from '../../../private/node/api.js'
import {requestIdsCollection} from '../../../private/node/request-ids.js'
import {
GraphQLClient,
rawRequest,
Expand Down Expand Up @@ -105,6 +106,7 @@ async function performGraphQLRequest<TResult>(options: PerformGraphQLRequestOpti
async function logLastRequestIdFromResponse(response: GraphQLResponse<unknown>) {
try {
const requestId = response.headers.get('x-request-id')
requestIdsCollection.addRequestId(requestId)
await addPublicMetadata(async () => {
return {
cmd_all_last_graphql_request_id: requestId ?? undefined,
Expand Down
Empty file.

0 comments on commit dda7c7a

Please sign in to comment.