Skip to content

Commit 3d34598

Browse files
committed
ref(core): Add weight tracking logic to browser logs/metrics
1 parent d1646c8 commit 3d34598

File tree

9 files changed

+440
-349
lines changed

9 files changed

+440
-349
lines changed

packages/browser/src/client.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import type { BrowserTransportOptions } from './transports/types';
2626
*/
2727
declare const __SENTRY_RELEASE__: string | undefined;
2828

29-
const DEFAULT_FLUSH_INTERVAL = 5000;
30-
3129
type BrowserSpecificOptions = BrowserClientReplayOptions &
3230
BrowserClientProfilingOptions & {
3331
/** If configured, this URL will be used as base URL for lazy loading integration. */
@@ -85,8 +83,6 @@ export type BrowserClientOptions = ClientOptions<BrowserTransportOptions> & Brow
8583
* @see SentryClient for usage documentation.
8684
*/
8785
export class BrowserClient extends Client<BrowserClientOptions> {
88-
private _logFlushIdleTimeout: ReturnType<typeof setTimeout> | undefined;
89-
private _metricFlushIdleTimeout: ReturnType<typeof setTimeout> | undefined;
9086
/**
9187
* Creates a new Browser SDK instance.
9288
*
@@ -110,6 +106,7 @@ export class BrowserClient extends Client<BrowserClientOptions> {
110106

111107
const { sendDefaultPii, sendClientReports, enableLogs, _experiments } = this._options;
112108

109+
// Flush logs and metrics when page becomes hidden (e.g., tab switch, navigation)
113110
if (WINDOW.document && (sendClientReports || enableLogs || _experiments?.enableMetrics)) {
114111
WINDOW.document.addEventListener('visibilitychange', () => {
115112
if (WINDOW.document.visibilityState === 'hidden') {
@@ -126,38 +123,6 @@ export class BrowserClient extends Client<BrowserClientOptions> {
126123
});
127124
}
128125

129-
if (enableLogs) {
130-
this.on('flush', () => {
131-
_INTERNAL_flushLogsBuffer(this);
132-
});
133-
134-
this.on('afterCaptureLog', () => {
135-
if (this._logFlushIdleTimeout) {
136-
clearTimeout(this._logFlushIdleTimeout);
137-
}
138-
139-
this._logFlushIdleTimeout = setTimeout(() => {
140-
_INTERNAL_flushLogsBuffer(this);
141-
}, DEFAULT_FLUSH_INTERVAL);
142-
});
143-
}
144-
145-
if (_experiments?.enableMetrics) {
146-
this.on('flush', () => {
147-
_INTERNAL_flushMetricsBuffer(this);
148-
});
149-
150-
this.on('afterCaptureMetric', () => {
151-
if (this._metricFlushIdleTimeout) {
152-
clearTimeout(this._metricFlushIdleTimeout);
153-
}
154-
155-
this._metricFlushIdleTimeout = setTimeout(() => {
156-
_INTERNAL_flushMetricsBuffer(this);
157-
}, DEFAULT_FLUSH_INTERVAL);
158-
});
159-
}
160-
161126
if (sendDefaultPii) {
162127
this.on('beforeSendSession', addAutoIpAddressToSession);
163128
}

packages/browser/test/client.test.ts

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ vi.mock('@sentry/core', async requireActual => {
1818

1919
describe('BrowserClient', () => {
2020
let client: BrowserClient;
21-
const DEFAULT_FLUSH_INTERVAL = 5000;
2221

2322
afterEach(() => {
2423
vi.useRealTimers();
@@ -78,57 +77,24 @@ describe('BrowserClient', () => {
7877
expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client);
7978
});
8079

81-
it('flushes logs on flush event', () => {
80+
it('inherits log weight-based flushing from base Client', () => {
8281
const scope = new Scope();
8382
scope.setClient(client);
8483

85-
// Add some logs
86-
sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 1' }, scope);
87-
sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 2' }, scope);
84+
// Spy on sendEnvelope to verify flushing happens
85+
const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope');
8886

89-
// Trigger flush event
90-
client.emit('flush');
91-
92-
expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client);
93-
});
94-
95-
it('flushes logs after idle timeout', () => {
96-
const scope = new Scope();
97-
scope.setClient(client);
98-
99-
// Add a log which will trigger afterCaptureLog event
87+
// Add a log and verify the base Client functionality works
10088
sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log' }, scope);
10189

102-
// Fast forward the idle timeout
103-
vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL);
104-
105-
expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client);
106-
});
107-
108-
it('resets idle timeout when new logs are captured', () => {
109-
const scope = new Scope();
110-
scope.setClient(client);
111-
112-
// Add initial log
113-
sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 1' }, scope);
114-
115-
// Fast forward part of the idle timeout
116-
vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL / 2);
90+
// Verify weight tracking is active
91+
expect((client as any)._logWeight).toBeGreaterThan(0);
11792

118-
// Add another log which should reset the timeout
119-
sentryCore._INTERNAL_captureLog({ level: 'info', message: 'test log 2' }, scope);
120-
121-
// Fast forward the remaining time
122-
vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL / 2);
123-
124-
// Should not have flushed yet since timeout was reset
125-
expect(sentryCore._INTERNAL_flushLogsBuffer).not.toHaveBeenCalled();
126-
127-
// Fast forward the full timeout
128-
vi.advanceTimersByTime(DEFAULT_FLUSH_INTERVAL);
93+
// Trigger flush and verify it works
94+
client.emit('flush');
12995

130-
// Now should have flushed both logs
131-
expect(sentryCore._INTERNAL_flushLogsBuffer).toHaveBeenCalledWith(client);
96+
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
97+
expect((client as any)._logWeight).toBe(0);
13298
});
13399
});
134100
});

0 commit comments

Comments
 (0)