Skip to content

ref: Use addBreadcrumb directly & allow to pass hint #9867

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
Dec 18, 2023
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
16 changes: 8 additions & 8 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import { getClient, getCurrentHub } from '@sentry/core';
import { addBreadcrumb, getClient } from '@sentry/core';
import type {
Event as SentryEvent,
HandlerDataConsole,
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Breadcrumbs implements Integration {
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
*/
function addSentryBreadcrumb(event: SentryEvent): void {
getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
event_id: event.event_id,
Expand Down Expand Up @@ -173,7 +173,7 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: HandlerDa
return;
}

getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: `ui.${handlerData.name}`,
message: target,
Expand Down Expand Up @@ -213,7 +213,7 @@ function _consoleBreadcrumb(handlerData: HandlerDataConsole): void {
}
}

getCurrentHub().addBreadcrumb(breadcrumb, {
addBreadcrumb(breadcrumb, {
input: handlerData.args,
level: handlerData.level,
});
Expand Down Expand Up @@ -247,7 +247,7 @@ function _xhrBreadcrumb(handlerData: HandlerDataXhr): void {
endTimestamp,
};

getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'xhr',
data,
Expand Down Expand Up @@ -282,7 +282,7 @@ function _fetchBreadcrumb(handlerData: HandlerDataFetch): void {
endTimestamp,
};

getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'fetch',
data,
Expand All @@ -303,7 +303,7 @@ function _fetchBreadcrumb(handlerData: HandlerDataFetch): void {
startTimestamp,
endTimestamp,
};
getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'fetch',
data,
Expand Down Expand Up @@ -338,7 +338,7 @@ function _historyBreadcrumb(handlerData: HandlerDataHistory): void {
from = parsedFrom.relative;
}

getCurrentHub().addBreadcrumb({
addBreadcrumb({
category: 'navigation',
data: {
from,
Expand Down
13 changes: 6 additions & 7 deletions packages/browser/test/unit/integrations/breadcrumbs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import * as SentryCore from '@sentry/core';
import type { Client } from '@sentry/types';

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

describe('Breadcrumbs', () => {
it('Should add sentry breadcrumb', async () => {
const addBreadcrumb = jest.fn();
hub.addBreadcrumb = addBreadcrumb;

client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [new Breadcrumbs()],
});

getCurrentHub().bindClient(client);
SentryCore.getCurrentHub().bindClient(client);

const addBreadcrumbSpy = jest.spyOn(SentryCore, 'addBreadcrumb').mockImplementation(() => {});

client.captureMessage('test');
await flush(2000);

expect(addBreadcrumb.mock.calls[0][0].category).toEqual('sentry.event');
expect(addBreadcrumb.mock.calls[0][0].message).toEqual('test');
expect(addBreadcrumbSpy.mock.calls[0][0].category).toEqual('sentry.event');
expect(addBreadcrumbSpy.mock.calls[0][0].message).toEqual('test');
});
});
5 changes: 3 additions & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
Breadcrumb,
BreadcrumbHint,
CaptureContext,
CheckIn,
Client,
Expand Down Expand Up @@ -90,8 +91,8 @@ export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub
*
* @param breadcrumb The breadcrumb to record.
*/
export function addBreadcrumb(breadcrumb: Breadcrumb): ReturnType<Hub['addBreadcrumb']> {
getCurrentHub().addBreadcrumb(breadcrumb);
export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): ReturnType<Hub['addBreadcrumb']> {
getCurrentHub().addBreadcrumb(breadcrumb, hint);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/node-experimental/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
import { addBreadcrumb, hasTracingEnabled, isSentryRequestUrl } from '@sentry/core';
import { _INTERNAL, getClient, getCurrentHub, getSpanKind, setSpanMetadata } from '@sentry/opentelemetry';
import type { EventProcessor, Hub, Integration } from '@sentry/types';
import { stringMatchesSomePattern } from '@sentry/utils';
Expand Down Expand Up @@ -159,7 +159,7 @@ export class Http implements Integration {
}

const data = _INTERNAL.getRequestSpanData(span);
getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'http',
data: {
Expand Down
6 changes: 3 additions & 3 deletions packages/node-experimental/src/integrations/node-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import type { Instrumentation } from '@opentelemetry/instrumentation';
import { hasTracingEnabled } from '@sentry/core';
import { _INTERNAL, getClient, getCurrentHub, getSpanKind } from '@sentry/opentelemetry';
import { addBreadcrumb, hasTracingEnabled } from '@sentry/core';
import { _INTERNAL, getClient, getSpanKind } from '@sentry/opentelemetry';
import type { Integration } from '@sentry/types';

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

const data = _INTERNAL.getRequestSpanData(span);
getCurrentHub().addBreadcrumb({
addBreadcrumb({
category: 'http',
data: {
...data,
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/integrations/console.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as util from 'util';
import { getCurrentHub } from '@sentry/core';
import { addBreadcrumb, getCurrentHub } from '@sentry/core';
import type { Integration } from '@sentry/types';
import { addConsoleInstrumentationHandler, severityLevelFromString } from '@sentry/utils';

Expand All @@ -26,7 +26,7 @@ export class Console implements Integration {
return;
}

hub.addBreadcrumb(
addBreadcrumb(
{
category: 'console',
level: severityLevelFromString(level),
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as http from 'http';
import type * as https from 'https';
import type { Hub } from '@sentry/core';
import { getClient, getCurrentScope } from '@sentry/core';
import { addBreadcrumb, getClient, getCurrentScope } from '@sentry/core';
import { getCurrentHub, getDynamicSamplingContextFromClient, isSentryRequestUrl } from '@sentry/core';
import type {
DynamicSamplingContext,
Expand Down Expand Up @@ -214,7 +214,7 @@ function _createWrappedRequestMethodFactory(
return;
}

getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'http',
data: {
Expand Down
5 changes: 3 additions & 2 deletions packages/node/src/integrations/undici/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
addBreadcrumb,
getClient,
getCurrentHub,
getCurrentScope,
Expand Down Expand Up @@ -214,7 +215,7 @@ export class Undici implements Integration {
}

if (this._options.breadcrumbs) {
hub.addBreadcrumb(
addBreadcrumb(
{
category: 'http',
data: {
Expand Down Expand Up @@ -254,7 +255,7 @@ export class Undici implements Integration {
}

if (this._options.breadcrumbs) {
hub.addBreadcrumb(
addBreadcrumb(
{
category: 'http',
data: {
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ describe('default protocols', () => {
function captureBreadcrumb(key: string): Promise<Breadcrumb> {
const hub = new Hub();
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
jest.spyOn(sentryCore, 'addBreadcrumb').mockImplementation((...rest) => hub.addBreadcrumb(...rest));

let resolve: (value: Breadcrumb | PromiseLike<Breadcrumb>) => void;
const p = new Promise<Breadcrumb>(r => {
Expand Down
11 changes: 5 additions & 6 deletions packages/replay/src/util/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { addBreadcrumb } from '@sentry/core';
import { logger } from '@sentry/utils';

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

if (shouldAddBreadcrumb) {
addBreadcrumb(message);
addLogBreadcrumb(message);
}
}

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

function addBreadcrumb(message: string): void {
const hub = getCurrentHub();
hub.addBreadcrumb(
function addLogBreadcrumb(message: string): void {
addBreadcrumb(
{
category: 'console',
data: {
Expand Down
6 changes: 3 additions & 3 deletions packages/vercel-edge/src/integrations/wintercg-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { instrumentFetchRequest } from '@sentry-internal/tracing';
import { getClient, getCurrentHub, isSentryRequestUrl } from '@sentry/core';
import { addBreadcrumb, getClient, getCurrentHub, isSentryRequestUrl } from '@sentry/core';
import type { FetchBreadcrumbData, FetchBreadcrumbHint, HandlerDataFetch, Integration, Span } from '@sentry/types';
import { LRUMap, addFetchInstrumentationHandler, stringMatchesSomePattern } from '@sentry/utils';

Expand Down Expand Up @@ -130,7 +130,7 @@ function createBreadcrumb(handlerData: HandlerDataFetch): void {
endTimestamp,
};

getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'fetch',
data,
Expand All @@ -150,7 +150,7 @@ function createBreadcrumb(handlerData: HandlerDataFetch): void {
startTimestamp,
endTimestamp,
};
getCurrentHub().addBreadcrumb(
addBreadcrumb(
{
category: 'fetch',
data,
Expand Down
2 changes: 1 addition & 1 deletion packages/vercel-edge/test/wintercg-fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jest.spyOn(sentryCore, 'getClient').mockImplementation(() => fakeHubInstance.get

const addFetchInstrumentationHandlerSpy = jest.spyOn(sentryUtils, 'addFetchInstrumentationHandler');
const instrumentFetchRequestSpy = jest.spyOn(internalTracing, 'instrumentFetchRequest');
const addBreadcrumbSpy = jest.spyOn(fakeHubInstance, 'addBreadcrumb');
const addBreadcrumbSpy = jest.spyOn(sentryCore, 'addBreadcrumb');

beforeEach(() => {
jest.clearAllMocks();
Expand Down