Skip to content

fix(logs): Ensure logs can be flushed correctly #16216

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
May 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Sentry.init({
debug: !!process.env.DEBUG,
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1,
_experiments: {
enableLogs: true,
},
});

import { TRPCError, initTRPC } from '@trpc/server';
Expand All @@ -30,6 +33,11 @@ app.get('/test-success', function (req, res) {
res.send({ version: 'v1' });
});

app.get('/test-log', function (req, res) {
Sentry.logger.debug('Accessed /test-log route');
res.send({ message: 'Log sent' });
});

app.get('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from '@playwright/test';
import { waitForEnvelopeItem } from '@sentry-internal/test-utils';
import type { SerializedLog, SerializedLogContainer } from '@sentry/core';

test('should send logs', async ({ baseURL }) => {
const logEnvelopePromise = waitForEnvelopeItem('node-express', envelope => {
return envelope[0].type === 'log' && (envelope[1] as SerializedLogContainer).items[0]?.level === 'debug';
});

await fetch(`${baseURL}/test-log`);

const logEnvelope = await logEnvelopePromise;
const log = (logEnvelope[1] as SerializedLogContainer).items[0];
expect(log?.level).toBe('debug');
expect(log?.body).toBe('Accessed /test-log route');
});
14 changes: 8 additions & 6 deletions packages/core/src/logs/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { _getSpanForScope } from '../utils/spanOnScope';
import { isParameterizedString } from '../utils-hoist/is';
import { logger } from '../utils-hoist/logger';
import { timestampInSeconds } from '../utils-hoist/time';
import { GLOBAL_OBJ } from '../utils-hoist/worldwide';
import { SEVERITY_TEXT_TO_SEVERITY_NUMBER } from './constants';
import { createLogEnvelope } from './envelope';

const MAX_LOG_BUFFER_SIZE = 100;

const CLIENT_TO_LOG_BUFFER_MAP = new WeakMap<Client, Array<SerializedLog>>();
// 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 @@ -149,11 +151,11 @@ export function _INTERNAL_captureLog(
),
};

const logBuffer = CLIENT_TO_LOG_BUFFER_MAP.get(client);
const logBuffer = _INTERNAL_getLogBuffer(client);
if (logBuffer === undefined) {
CLIENT_TO_LOG_BUFFER_MAP.set(client, [serializedLog]);
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [serializedLog]);
} else {
CLIENT_TO_LOG_BUFFER_MAP.set(client, [...logBuffer, serializedLog]);
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, [...logBuffer, serializedLog]);
if (logBuffer.length >= MAX_LOG_BUFFER_SIZE) {
_INTERNAL_flushLogsBuffer(client, logBuffer);
}
Expand Down Expand Up @@ -181,7 +183,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.
CLIENT_TO_LOG_BUFFER_MAP.set(client, []);
GLOBAL_OBJ._sentryClientToLogBufferMap?.set(client, []);

client.emit('flushLogs');

Expand All @@ -199,5 +201,5 @@ 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 CLIENT_TO_LOG_BUFFER_MAP.get(client);
return GLOBAL_OBJ._sentryClientToLogBufferMap?.get(client);
}
8 changes: 8 additions & 0 deletions packages/core/src/utils-hoist/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/* 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 { SdkSource } from './env';

/** Internal global with common properties and Sentry extensions */
Expand All @@ -35,6 +37,12 @@ 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