Skip to content

ref(core): Keep client-logger map on carrier & avoid side-effect #16923

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
merged 1 commit into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/carrier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AsyncContextStack } from './asyncContext/stackStrategy';
import type { AsyncContextStrategy } from './asyncContext/types';
import type { Client } from './client';
import type { Scope } from './scope';
import type { SerializedLog } from './types-hoist/log';
import type { Logger } from './utils/logger';
import { SDK_VERSION } from './utils/version';
import { GLOBAL_OBJ } from './utils/worldwide';
Expand All @@ -27,6 +29,11 @@ export interface SentryCarrier {
/** @deprecated Logger is no longer set. Instead, we keep enabled state in loggerSettings. */
logger?: Logger;
loggerSettings?: { enabled: boolean };
/**
* A map of Sentry clients to their log buffers.
* This is used to store logs that are sent to Sentry.
*/
clientToLogBufferMap?: WeakMap<Client, Array<SerializedLog>>;

/** Overwrites TextEncoder used in `@sentry/core`, need for `[email protected]` and older */
encodePolyfill?: (input: string) => Uint8Array;
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/logs/exports.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getGlobalSingleton } from '../carrier';
import type { Client } from '../client';
import { _getTraceInfoFromScope } from '../client';
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from '../currentScopes';
Expand All @@ -9,15 +10,11 @@ import { isParameterizedString } from '../utils/is';
import { debug } from '../utils/logger';
import { _getSpanForScope } from '../utils/spanOnScope';
import { timestampInSeconds } from '../utils/time';
import { GLOBAL_OBJ } from '../utils/worldwide';
import { SEVERITY_TEXT_TO_SEVERITY_NUMBER } from './constants';
import { createLogEnvelope } from './envelope';

const MAX_LOG_BUFFER_SIZE = 100;

// The reference to the Client <> LogBuffer map is stored to ensure it's always the same
GLOBAL_OBJ._sentryClientToLogBufferMap = new WeakMap<Client, Array<SerializedLog>>();

/**
* Converts a log attribute to a serialized log attribute.
*
Expand Down Expand Up @@ -92,11 +89,13 @@ function setLogAttribute(
* the stable Sentry SDK API and can be changed or removed without warning.
*/
export function _INTERNAL_captureSerializedLog(client: Client, serializedLog: SerializedLog): void {
const bufferMap = _getBufferMap();

const logBuffer = _INTERNAL_getLogBuffer(client);
if (logBuffer === undefined) {
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [serializedLog]);
bufferMap.set(client, [serializedLog]);
} else {
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [...logBuffer, serializedLog]);
bufferMap.set(client, [...logBuffer, serializedLog]);
if (logBuffer.length >= MAX_LOG_BUFFER_SIZE) {
_INTERNAL_flushLogsBuffer(client, logBuffer);
}
Expand Down Expand Up @@ -217,7 +216,7 @@ export function _INTERNAL_flushLogsBuffer(client: Client, maybeLogBuffer?: Array
const envelope = createLogEnvelope(logBuffer, clientOptions._metadata, clientOptions.tunnel, client.getDsn());

// Clear the log buffer after envelopes have been constructed.
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, []);
_getBufferMap().set(client, []);

client.emit('flushLogs');

Expand All @@ -235,7 +234,7 @@ export function _INTERNAL_flushLogsBuffer(client: Client, maybeLogBuffer?: Array
* @returns The log buffer for the given client.
*/
export function _INTERNAL_getLogBuffer(client: Client): Array<SerializedLog> | undefined {
return GLOBAL_OBJ._sentryClientToLogBufferMap?.get(client);
return _getBufferMap().get(client);
}

/**
Expand All @@ -251,3 +250,8 @@ function getMergedScopeData(currentScope: Scope): ScopeData {
mergeScopeData(scopeData, currentScope.getScopeData());
return scopeData;
}

function _getBufferMap(): WeakMap<Client, Array<SerializedLog>> {
// The reference to the Client <> LogBuffer map is stored on the carrier to ensure it's always the same
return getGlobalSingleton('clientToLogBufferMap', () => new WeakMap<Client, Array<SerializedLog>>());
}
8 changes: 0 additions & 8 deletions packages/core/src/utils/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Carrier } from '../carrier';
import type { Client } from '../client';
import type { SerializedLog } from '../types-hoist/log';
import type { Span } from '../types-hoist/span';
import type { SdkSource } from './env';

Expand All @@ -38,12 +36,6 @@ export type InternalGlobal = {
id?: string;
};
SENTRY_SDK_SOURCE?: SdkSource;
/**
* A map of Sentry clients to their log buffers.
*
* This is used to store logs that are sent to Sentry.
*/
_sentryClientToLogBufferMap?: WeakMap<Client, Array<SerializedLog>>;
/**
* Debug IDs are indirectly injected by Sentry CLI or bundler plugins to directly reference a particular source map
* for resolving of a source file. The injected code will place an entry into the record for each loaded bundle/JS
Expand Down
Loading