Skip to content

Commit 90ba859

Browse files
committed
ref: Use addBreadcrumb directly & allow to pass hint
Instead of using `hub.addBreadcrumb()`
1 parent db4bef1 commit 90ba859

File tree

12 files changed

+39
-38
lines changed

12 files changed

+39
-38
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-lines */
2-
import { getClient, getCurrentHub } from '@sentry/core';
2+
import { addBreadcrumb, getClient } from '@sentry/core';
33
import type {
44
Event as SentryEvent,
55
HandlerDataConsole,
@@ -123,7 +123,7 @@ export class Breadcrumbs implements Integration {
123123
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
124124
*/
125125
function addSentryBreadcrumb(event: SentryEvent): void {
126-
getCurrentHub().addBreadcrumb(
126+
addBreadcrumb(
127127
{
128128
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
129129
event_id: event.event_id,
@@ -173,7 +173,7 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: HandlerDa
173173
return;
174174
}
175175

176-
getCurrentHub().addBreadcrumb(
176+
addBreadcrumb(
177177
{
178178
category: `ui.${handlerData.name}`,
179179
message: target,
@@ -213,7 +213,7 @@ function _consoleBreadcrumb(handlerData: HandlerDataConsole): void {
213213
}
214214
}
215215

216-
getCurrentHub().addBreadcrumb(breadcrumb, {
216+
addBreadcrumb(breadcrumb, {
217217
input: handlerData.args,
218218
level: handlerData.level,
219219
});
@@ -247,7 +247,7 @@ function _xhrBreadcrumb(handlerData: HandlerDataXhr): void {
247247
endTimestamp,
248248
};
249249

250-
getCurrentHub().addBreadcrumb(
250+
addBreadcrumb(
251251
{
252252
category: 'xhr',
253253
data,
@@ -282,7 +282,7 @@ function _fetchBreadcrumb(handlerData: HandlerDataFetch): void {
282282
endTimestamp,
283283
};
284284

285-
getCurrentHub().addBreadcrumb(
285+
addBreadcrumb(
286286
{
287287
category: 'fetch',
288288
data,
@@ -303,7 +303,7 @@ function _fetchBreadcrumb(handlerData: HandlerDataFetch): void {
303303
startTimestamp,
304304
endTimestamp,
305305
};
306-
getCurrentHub().addBreadcrumb(
306+
addBreadcrumb(
307307
{
308308
category: 'fetch',
309309
data,
@@ -338,7 +338,7 @@ function _historyBreadcrumb(handlerData: HandlerDataHistory): void {
338338
from = parsedFrom.relative;
339339
}
340340

341-
getCurrentHub().addBreadcrumb({
341+
addBreadcrumb({
342342
category: 'navigation',
343343
data: {
344344
from,

packages/browser/test/unit/integrations/breadcrumbs.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import * as SentryCore from '@sentry/core';
22
import type { Client } from '@sentry/types';
33

44
import { Breadcrumbs, BrowserClient, Hub, flush } from '../../../src';
@@ -18,21 +18,20 @@ jest.mock('@sentry/core', () => {
1818

1919
describe('Breadcrumbs', () => {
2020
it('Should add sentry breadcrumb', async () => {
21-
const addBreadcrumb = jest.fn();
22-
hub.addBreadcrumb = addBreadcrumb;
23-
2421
client = new BrowserClient({
2522
...getDefaultBrowserClientOptions(),
2623
dsn: 'https://username@domain/123',
2724
integrations: [new Breadcrumbs()],
2825
});
2926

30-
getCurrentHub().bindClient(client);
27+
SentryCore.getCurrentHub().bindClient(client);
28+
29+
const addBreadcrumbSpy = jest.spyOn(SentryCore, 'addBreadcrumb').mockImplementation(() => {});
3130

3231
client.captureMessage('test');
3332
await flush(2000);
3433

35-
expect(addBreadcrumb.mock.calls[0][0].category).toEqual('sentry.event');
36-
expect(addBreadcrumb.mock.calls[0][0].message).toEqual('test');
34+
expect(addBreadcrumbSpy.mock.calls[0][0].category).toEqual('sentry.event');
35+
expect(addBreadcrumbSpy.mock.calls[0][0].message).toEqual('test');
3736
});
3837
});

packages/core/src/exports.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
Breadcrumb,
3+
BreadcrumbHint,
34
CaptureContext,
45
CheckIn,
56
Client,
@@ -90,8 +91,8 @@ export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub
9091
*
9192
* @param breadcrumb The breadcrumb to record.
9293
*/
93-
export function addBreadcrumb(breadcrumb: Breadcrumb): ReturnType<Hub['addBreadcrumb']> {
94-
getCurrentHub().addBreadcrumb(breadcrumb);
94+
export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): ReturnType<Hub['addBreadcrumb']> {
95+
getCurrentHub().addBreadcrumb(breadcrumb, hint);
9596
}
9697

9798
/**

packages/node-experimental/src/integrations/http.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Span } from '@opentelemetry/api';
33
import { SpanKind } from '@opentelemetry/api';
44
import { registerInstrumentations } from '@opentelemetry/instrumentation';
55
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
6-
import { hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
6+
import { addBreadcrumb, hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
77
import { _INTERNAL, getClient, getCurrentHub, getSpanKind, setSpanMetadata } from '@sentry/opentelemetry';
88
import type { EventProcessor, Hub, Integration } from '@sentry/types';
99
import { stringMatchesSomePattern } from '@sentry/utils';
@@ -159,7 +159,7 @@ export class Http implements Integration {
159159
}
160160

161161
const data = _INTERNAL.getRequestSpanData(span);
162-
getCurrentHub().addBreadcrumb(
162+
addBreadcrumb(
163163
{
164164
category: 'http',
165165
data: {

packages/node-experimental/src/integrations/node-fetch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Span } from '@opentelemetry/api';
22
import { SpanKind } from '@opentelemetry/api';
33
import type { Instrumentation } from '@opentelemetry/instrumentation';
4-
import { hasTracingEnabled } from '@sentry/core';
5-
import { _INTERNAL, getClient, getCurrentHub, getSpanKind } from '@sentry/opentelemetry';
4+
import { addBreadcrumb, hasTracingEnabled } from '@sentry/core';
5+
import { _INTERNAL, getClient, getSpanKind } from '@sentry/opentelemetry';
66
import type { Integration } from '@sentry/types';
77

88
import type { NodeExperimentalClient } from '../types';
@@ -114,7 +114,7 @@ export class NodeFetch extends NodePerformanceIntegration<NodeFetchOptions> impl
114114
}
115115

116116
const data = _INTERNAL.getRequestSpanData(span);
117-
getCurrentHub().addBreadcrumb({
117+
addBreadcrumb({
118118
category: 'http',
119119
data: {
120120
...data,

packages/node/src/integrations/console.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as util from 'util';
2-
import { getCurrentHub } from '@sentry/core';
2+
import { addBreadcrumb, getCurrentHub } from '@sentry/core';
33
import type { Integration } from '@sentry/types';
44
import { addConsoleInstrumentationHandler, severityLevelFromString } from '@sentry/utils';
55

@@ -26,7 +26,7 @@ export class Console implements Integration {
2626
return;
2727
}
2828

29-
hub.addBreadcrumb(
29+
addBreadcrumb(
3030
{
3131
category: 'console',
3232
level: severityLevelFromString(level),

packages/node/src/integrations/http.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as http from 'http';
22
import type * as https from 'https';
33
import type { Hub } from '@sentry/core';
4-
import { getClient, getCurrentScope } from '@sentry/core';
4+
import { addBreadcrumb, getClient, getCurrentScope } from '@sentry/core';
55
import { getCurrentHub, getDynamicSamplingContextFromClient, isSentryRequestUrl } from '@sentry/core';
66
import type {
77
DynamicSamplingContext,
@@ -214,7 +214,7 @@ function _createWrappedRequestMethodFactory(
214214
return;
215215
}
216216

217-
getCurrentHub().addBreadcrumb(
217+
addBreadcrumb(
218218
{
219219
category: 'http',
220220
data: {

packages/node/src/integrations/undici/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
addBreadcrumb,
23
getClient,
34
getCurrentHub,
45
getCurrentScope,
@@ -214,7 +215,7 @@ export class Undici implements Integration {
214215
}
215216

216217
if (this._options.breadcrumbs) {
217-
hub.addBreadcrumb(
218+
addBreadcrumb(
218219
{
219220
category: 'http',
220221
data: {
@@ -254,7 +255,7 @@ export class Undici implements Integration {
254255
}
255256

256257
if (this._options.breadcrumbs) {
257-
hub.addBreadcrumb(
258+
addBreadcrumb(
258259
{
259260
category: 'http',
260261
data: {

packages/node/test/integrations/http.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ describe('default protocols', () => {
592592
function captureBreadcrumb(key: string): Promise<Breadcrumb> {
593593
const hub = new Hub();
594594
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
595+
jest.spyOn(sentryCore, 'addBreadcrumb').mockImplementation((...rest) => hub.addBreadcrumb(...rest));
595596

596597
let resolve: (value: Breadcrumb | PromiseLike<Breadcrumb>) => void;
597598
const p = new Promise<Breadcrumb>(r => {

packages/replay/src/util/log.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { addBreadcrumb } from '@sentry/core';
22
import { logger } from '@sentry/utils';
33

44
import { DEBUG_BUILD } from '../debug-build';
@@ -14,7 +14,7 @@ export function logInfo(message: string, shouldAddBreadcrumb?: boolean): void {
1414
logger.info(message);
1515

1616
if (shouldAddBreadcrumb) {
17-
addBreadcrumb(message);
17+
addLogBreadcrumb(message);
1818
}
1919
}
2020

@@ -33,14 +33,13 @@ export function logInfoNextTick(message: string, shouldAddBreadcrumb?: boolean):
3333
// Wait a tick here to avoid race conditions for some initial logs
3434
// which may be added before replay is initialized
3535
setTimeout(() => {
36-
addBreadcrumb(message);
36+
addLogBreadcrumb(message);
3737
}, 0);
3838
}
3939
}
4040

41-
function addBreadcrumb(message: string): void {
42-
const hub = getCurrentHub();
43-
hub.addBreadcrumb(
41+
function addLogBreadcrumb(message: string): void {
42+
addBreadcrumb(
4443
{
4544
category: 'console',
4645
data: {

packages/vercel-edge/src/integrations/wintercg-fetch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { instrumentFetchRequest } from '@sentry-internal/tracing';
2-
import { getClient, getCurrentHub, isSentryRequestUrl } from '@sentry/core';
2+
import { addBreadcrumb, getClient, getCurrentHub, isSentryRequestUrl } from '@sentry/core';
33
import type { FetchBreadcrumbData, FetchBreadcrumbHint, HandlerDataFetch, Integration, Span } from '@sentry/types';
44
import { LRUMap, addFetchInstrumentationHandler, stringMatchesSomePattern } from '@sentry/utils';
55

@@ -130,7 +130,7 @@ function createBreadcrumb(handlerData: HandlerDataFetch): void {
130130
endTimestamp,
131131
};
132132

133-
getCurrentHub().addBreadcrumb(
133+
addBreadcrumb(
134134
{
135135
category: 'fetch',
136136
data,
@@ -150,7 +150,7 @@ function createBreadcrumb(handlerData: HandlerDataFetch): void {
150150
startTimestamp,
151151
endTimestamp,
152152
};
153-
getCurrentHub().addBreadcrumb(
153+
addBreadcrumb(
154154
{
155155
category: 'fetch',
156156
data,

packages/vercel-edge/test/wintercg-fetch.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jest.spyOn(sentryCore, 'getClient').mockImplementation(() => fakeHubInstance.get
3434

3535
const addFetchInstrumentationHandlerSpy = jest.spyOn(sentryUtils, 'addFetchInstrumentationHandler');
3636
const instrumentFetchRequestSpy = jest.spyOn(internalTracing, 'instrumentFetchRequest');
37-
const addBreadcrumbSpy = jest.spyOn(fakeHubInstance, 'addBreadcrumb');
37+
const addBreadcrumbSpy = jest.spyOn(sentryCore, 'addBreadcrumb');
3838

3939
beforeEach(() => {
4040
jest.clearAllMocks();

0 commit comments

Comments
 (0)