-
Notifications
You must be signed in to change notification settings - Fork 167
feat: add execution id support #592
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
liuyunnnn
merged 30 commits into
GoogleCloudPlatform:main
from
liuyunnnn:add-execution-id-support
Mar 6, 2024
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
3d279d7
feat: add execution id support.
liuyunnnn f7f3a49
add --enable-execution-id.
liuyunnnn 29fc127
Force execution id support on nodejs version to be at least 12.17.0.
liuyunnnn c2a204b
import async_hooks dynamically.
liuyunnnn bf56546
revert package-lock.
liuyunnnn 106b0f9
Fix test and dependency.
liuyunnnn dfdecb2
fix test.
liuyunnnn da044b2
Fix latest support node js version.
liuyunnnn 15e2384
get rid of text-encoding.
liuyunnnn 998e393
Addressed commets.
liuyunnnn a898052
Fixed format.
liuyunnnn a212010
Add method getExecutionId() to allow users get execution id.
liuyunnnn 37eebd7
Control enabling or not by env var LOG_EXECUTION_ID.
liuyunnnn 79270d5
Error handler to catch all unhandled exceptions and log with executio…
liuyunnnn 58871f0
Update doc
liuyunnnn 19b0de7
Addressed comments
liuyunnnn a01bd34
fixed format.
liuyunnnn ad41e7b
Addressed comments.
liuyunnnn 2ae9bf8
Updated dependencies to use node:crypto
liuyunnnn 5fc4025
Update dependencies to use node:crypto
liuyunnnn c72c7b6
fixed tsconfig
liuyunnnn 12d1c5b
fixed dependencies.
liuyunnnn 1ab7659
Seperate execution context middleware into two and add execution id, …
liuyunnnn 859a7fe
Update docs
liuyunnnn d18088d
delete getExecutionId()
liuyunnnn 19a3a4c
cleanup getExecutionId().
liuyunnnn 363b4d4
add getExecutionId() back as req.executionId is not accessible to eve…
liuyunnnn 409c5c9
Update documents.
liuyunnnn ea14efc
addressed comments and remove getExecutionId().
liuyunnnn fcb6a40
import type AsyncLocalStorage
liuyunnnn 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 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,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 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.