-
Notifications
You must be signed in to change notification settings - Fork 33
ref(logging): Decouple async log context #970
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
+504
−221
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab03f5d
refactor(logging): Decouple async log context
sentry-junior[bot] 8aaceb9
test(logging): Update neutral context assertion
sentry-junior[bot] 3ce32f2
refactor(logging): Scope typed context at agent boundary
sentry-junior[bot] 575278d
fix(logging): Preserve scoped context updates
sentry-junior[bot] a754e43
fix(logging): Bind context from current run contract
dcramer cd752ef
ref(logging): Reuse agent run context projection
dcramer 05b869e
fix(logging): Preserve bound Sentry scope context
sentry-junior[bot] 3a0de0c
fix(logging): Preserve inherited typed context
dcramer 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
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,136 @@ | ||
| import { AsyncLocalStorage } from "node:async_hooks"; | ||
|
|
||
| export type LogAttributeValue = string | number | boolean | string[]; | ||
| export type LogAttributes = Record<string, LogAttributeValue>; | ||
|
|
||
| /** Provider-neutral correlation data inherited by logs and spans in an operation. */ | ||
| export interface LogContext { | ||
| conversationId?: string; | ||
| platform?: string; | ||
| requestId?: string; | ||
| messageConversationId?: string; | ||
| destinationName?: string; | ||
| userId?: string; | ||
| userName?: string; | ||
| userEmail?: string; | ||
| runId?: string; | ||
| actorType?: string; | ||
| actorId?: string; | ||
| assistantUserName?: string; | ||
| modelId?: string; | ||
| skillName?: string; | ||
| httpMethod?: string; | ||
| httpPath?: string; | ||
| urlFull?: string; | ||
| userAgent?: string; | ||
| } | ||
|
|
||
| /** Async attribute domain consumed directly by LogTape. */ | ||
| export const logContextStorage = new AsyncLocalStorage<LogAttributes>(); | ||
|
|
||
| /** Typed context domain retained for consumers such as native Sentry scope fields. */ | ||
| const typedLogContextStorage = new AsyncLocalStorage<LogContext>(); | ||
|
|
||
| function definedLogContext(context: LogContext): LogContext { | ||
| return Object.fromEntries( | ||
| Object.entries(context).filter(([, value]) => value !== undefined), | ||
| ) as LogContext; | ||
| } | ||
|
|
||
| function definedAttributes( | ||
| attributes: Record<string, string | undefined>, | ||
| ): LogAttributes { | ||
| return Object.fromEntries( | ||
| Object.entries(attributes).filter( | ||
| (entry): entry is [string, string] => entry[1] !== undefined, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** Convert provider-neutral domain context to stable telemetry attributes. */ | ||
| export function logContextToAttributes(context: LogContext): LogAttributes { | ||
| return definedAttributes({ | ||
| "gen_ai.conversation.id": context.conversationId, | ||
| "app.platform": context.platform, | ||
| "app.request.id": context.requestId, | ||
| "messaging.system": | ||
| context.platform === "slack" ? "slack" : context.platform, | ||
| "messaging.message.conversation_id": context.messageConversationId, | ||
| "messaging.destination.name": context.destinationName, | ||
| "enduser.id": context.userId, | ||
| "enduser.pseudo.id": context.userName, | ||
| "app.run.id": context.runId, | ||
| "app.actor.type": context.actorType, | ||
| "app.actor.id": context.actorId, | ||
| "gen_ai.agent.name": context.assistantUserName, | ||
| "gen_ai.request.model": context.modelId, | ||
| "app.skill.name": context.skillName, | ||
| "http.request.method": context.httpMethod, | ||
| "url.path": context.httpPath, | ||
| "url.full": context.urlFull, | ||
| "user_agent.original": context.userAgent, | ||
| }); | ||
| } | ||
|
|
||
| /** Run an operation with merged context, restoring its parent on completion. */ | ||
| export function runWithLogContext<T>( | ||
| context: LogContext, | ||
| attributes: LogAttributes, | ||
| callback: () => T, | ||
| ): T { | ||
| return typedLogContextStorage.run( | ||
| { | ||
| ...typedLogContextStorage.getStore(), | ||
| ...definedLogContext(context), | ||
| }, | ||
| () => | ||
| logContextStorage.run( | ||
| { ...logContextStorage.getStore(), ...attributes }, | ||
| callback, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** Run an operation with raw attributes, restoring its parent on completion. */ | ||
| export function runWithLogAttributes<T>( | ||
| attributes: LogAttributes, | ||
| callback: () => T, | ||
| ): T { | ||
| return logContextStorage.run( | ||
| { ...logContextStorage.getStore(), ...attributes }, | ||
| callback, | ||
| ); | ||
| } | ||
|
|
||
| /** Merge raw attributes into the current scoped operation. */ | ||
| export function updateLogAttributes(attributes: LogAttributes): void { | ||
| const current = logContextStorage.getStore(); | ||
| if (current) { | ||
| Object.assign(current, attributes); | ||
| } | ||
| } | ||
|
|
||
| /** Merge context and attributes into the current scoped operation. */ | ||
| export function updateLogContext( | ||
| context: LogContext, | ||
| attributes: LogAttributes, | ||
| ): void { | ||
| const currentContext = typedLogContextStorage.getStore(); | ||
| if (currentContext) { | ||
| Object.assign(currentContext, definedLogContext(context)); | ||
| } | ||
| const currentAttributes = logContextStorage.getStore(); | ||
| if (currentAttributes) { | ||
| Object.assign(currentAttributes, attributes); | ||
| } | ||
| } | ||
|
|
||
| /** Read the typed context bound to the current operation. */ | ||
| export function getBoundLogContext(): LogContext { | ||
| return typedLogContextStorage.getStore() ?? {}; | ||
| } | ||
|
|
||
| /** Read the attributes bound to the current operation. */ | ||
| export function getBoundLogAttributes(): LogAttributes { | ||
| return logContextStorage.getStore() ?? {}; | ||
| } | ||
Oops, something went wrong.
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.