-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add execution id support (#592)
When set LOG_EXECUTION_ID=True in environment variables or set --log-execution-id, execution id will be added to log along with trace and span id. Nodejs version 13.0.0 is required for this feature.
- Loading branch information
Showing
18 changed files
with
700 additions
and
20 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as semver from 'semver'; | ||
import {Request, Response} from './functions'; | ||
import {NextFunction} from 'express'; | ||
import {requiredNodeJsVersionForLogExecutionID} from './options'; | ||
import type {AsyncLocalStorage} from 'node:async_hooks'; | ||
|
||
export interface ExecutionContext { | ||
executionId?: string; | ||
traceId?: string; | ||
spanId?: string; | ||
} | ||
|
||
let asyncLocalStorage: AsyncLocalStorage<ExecutionContext> | undefined; | ||
|
||
export async function asyncLocalStorageMiddleware( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) { | ||
if ( | ||
semver.lt(process.versions.node, requiredNodeJsVersionForLogExecutionID) | ||
) { | ||
// Skip for unsupported Node.js version. | ||
next(); | ||
return; | ||
} | ||
if (!asyncLocalStorage) { | ||
const asyncHooks = await import('node:async_hooks'); | ||
asyncLocalStorage = new asyncHooks.AsyncLocalStorage(); | ||
} | ||
|
||
asyncLocalStorage.run( | ||
{ | ||
executionId: req.executionId, | ||
traceId: req.traceId, | ||
spanId: req.spanId, | ||
}, | ||
() => { | ||
next(); | ||
} | ||
); | ||
} | ||
|
||
export function getCurrentContext(): ExecutionContext | undefined { | ||
if (!asyncLocalStorage) { | ||
return undefined; | ||
} | ||
return asyncLocalStorage.getStore(); | ||
} |
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,37 @@ | ||
import {Request, Response} from './functions'; | ||
import {NextFunction} from 'express'; | ||
|
||
const FUNCTION_EXECUTION_ID_HEADER_KEY = 'function-execution-id'; | ||
const TRACE_CONTEXT_HEADER_KEY = 'X-Cloud-Trace-Context'; | ||
|
||
const TRACE_CONTEXT_PATTERN = | ||
/^(?<traceId>\w+)\/(?<spanId>\d+);o=(?<options>.+)$/; | ||
|
||
function generateExecutionId() { | ||
const timestampPart = Date.now().toString(36).slice(-6); | ||
const randomPart = Math.random().toString(36).slice(-6); | ||
return timestampPart + randomPart; | ||
} | ||
|
||
export const executionContextMiddleware = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
let executionId = req.header(FUNCTION_EXECUTION_ID_HEADER_KEY); | ||
if (!executionId) { | ||
executionId = generateExecutionId(); | ||
} | ||
req.executionId = executionId; | ||
|
||
const cloudTraceContext = req.header(TRACE_CONTEXT_HEADER_KEY); | ||
if (cloudTraceContext) { | ||
const match = cloudTraceContext.match(TRACE_CONTEXT_PATTERN); | ||
if (match?.groups) { | ||
const {traceId, spanId} = match.groups; | ||
req.traceId = traceId; | ||
req.spanId = spanId; | ||
} | ||
} | ||
next(); | ||
}; |
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.