Skip to content
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

feat(core): Exclude Dev Server and Sentry Dsn request from Breadcrumbs #4240

Merged
merged 62 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
494a619
Adds breadcrumb origin in RNSentryBreadcrumb dictionary parsing
antonis Sep 27, 2024
cc0b364
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 8, 2024
a636be5
Use 8.38.0-beta.1 Cocoa SDK that has the new origin field
antonis Oct 8, 2024
522897a
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 9, 2024
197f863
Adds changelog
antonis Oct 9, 2024
9bee5c9
Fixes sentry-java breaking changes
antonis Oct 9, 2024
6268a43
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 10, 2024
6095218
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 11, 2024
0e43c5d
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 15, 2024
8832cf9
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 15, 2024
7a108ee
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 16, 2024
aff18bf
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 23, 2024
611cd97
Adds origin native tests
antonis Oct 23, 2024
37553f2
Adds Capture exception with breadcrumb in the sample
antonis Oct 23, 2024
24a527b
Set react native as event origin
antonis Oct 28, 2024
fabf052
Filter out events with react-native origin from the native layer
antonis Oct 28, 2024
d0b1633
Merge event breadcrumbs with native context
antonis Oct 28, 2024
5d2711c
Lint: removes empty line
antonis Oct 28, 2024
fb0057d
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Oct 31, 2024
78c24f0
Use predicate to filter breadcrumbs
antonis Oct 31, 2024
6fcd93d
Respect max breadcrumbs limit
antonis Oct 31, 2024
80add4f
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Nov 1, 2024
44e02ec
Updates changelog
antonis Nov 1, 2024
a2d29de
Update test names
antonis Nov 1, 2024
52c9e54
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Nov 6, 2024
81a11c3
Fixes lint issue
antonis Nov 6, 2024
f828fc7
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Nov 6, 2024
87bdc77
Filter out DevServer and DSN related breadcrumbs
antonis Nov 6, 2024
2d68620
Adds changelog
antonis Nov 6, 2024
67bd716
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Nov 7, 2024
c4246d7
Keep the last maxBreadcrumbs (default 100) when merging native and js…
antonis Nov 7, 2024
17e2201
Use client from function parameter
antonis Nov 11, 2024
fef2173
Merge branch 'main' into antonis/add-breadcrumb-origin
antonis Nov 11, 2024
76285fa
Refactor and test RNSentryModuleImpl.fetchNativeDeviceContexts (#4253)
antonis Nov 11, 2024
ff7ca33
Revert "Filter out DevServer and DSN related breadcrumbs"
antonis Nov 12, 2024
5121c83
Merge branch 'antonis/add-breadcrumb-origin' into antonis/3105-exclud…
antonis Nov 12, 2024
3811047
Passes development server url as an option to the native sdks
antonis Nov 12, 2024
c3da88b
Filter out Dev Server and Sentry Dsn breadcrumbs on Android
antonis Nov 12, 2024
d67de6c
Filter out Dev Server and Sentry Dsn breadcrumbs on iOS
antonis Nov 12, 2024
6ddc665
Filter out Dev Server and Sentry Dsn breadcrumbs on JS
antonis Nov 12, 2024
af02c03
Adds Java tests
antonis Nov 13, 2024
7ae568a
Adds Cocoa tests
antonis Nov 13, 2024
575821e
Adds JS tests
antonis Nov 13, 2024
efa3871
Sets correct spacing in import
antonis Nov 14, 2024
709b8df
Fixes changelog typo
antonis Nov 14, 2024
fef13ca
Handles undefined dev server urls
antonis Nov 14, 2024
96f3b5d
Handles undefined dsns
antonis Nov 14, 2024
ccaa502
Adds tests for undefined dev servers and dsns
antonis Nov 14, 2024
06ff84c
Handles undefined dev server urls in native code
antonis Nov 14, 2024
a213332
Updates test cases
antonis Nov 14, 2024
a69d5e8
Uses the url from the passed dsn to filter breadcrumbs
antonis Nov 14, 2024
e451c06
Handles nil dsn though this state should never be reached due to Sent…
antonis Nov 14, 2024
eae86ad
Use startsWith to check url matching
antonis Nov 15, 2024
c05c3c2
Check url with prefix instead of contains for efficiency
antonis Nov 15, 2024
48034dc
Fix comment typo
antonis Nov 15, 2024
3381a9e
Fix comment typo
antonis Nov 15, 2024
1f7891e
Fix comment typo
antonis Nov 15, 2024
5f23d48
Merge branch 'main' into antonis/3105-exclude-dsn-and-devserver
krystofwoldrich Nov 18, 2024
848aaa3
Update CHANGELOG.md
krystofwoldrich Nov 18, 2024
41ac85a
Safely parses dsn url
antonis Nov 18, 2024
bfc7c33
Merge branch 'main' into antonis/3105-exclude-dsn-and-devserver
antonis Nov 18, 2024
b6191be
Adds test case for url exception
antonis Nov 18, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Fixes

- Prevents exception capture context from being overwritten by native scope sync ([#4124](https://github.com/getsentry/sentry-react-native/pull/4124))
- Excludes Dev Server and Sentry Dsn requests from Breadcrumbs ([#4240](https://github.com/getsentry/sentry-react-native/pull/4240))

## 6.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.WritableMap
import com.facebook.react.common.JavascriptException
import io.sentry.Breadcrumb
import io.sentry.ILogger
import io.sentry.SentryLevel
import io.sentry.android.core.SentryAndroidOptions
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -134,4 +136,109 @@ class RNSentryModuleImplTest {
module.getSentryAndroidOptions(actualOptions, JavaOnlyMap.of(), logger)
assertTrue(actualOptions.ignoredExceptionsForType.contains(JavascriptException::class.java))
}

@Test
fun `beforeBreadcrumb callback filters out Sentry DSN requests breadcrumbs`() {
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of(
"dsn", "https://[email protected]/1234567",
"devServerUrl", "http://localhost:8081",
)
module.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb = Breadcrumb().apply {
type = "http"
setData("url", "https://def.ingest.sentry.io/1234567")
}

val result = options.beforeBreadcrumb?.execute(breadcrumb, mock())

assertNull("Breadcrumb should be filtered out", result)
}

@Test
fun `beforeBreadcrumb callback filters out dev server breadcrumbs`() {
val mockDevServerUrl = "http://localhost:8081"
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of(
"dsn", "https://[email protected]/1234567",
"devServerUrl", mockDevServerUrl,
)
module.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb = Breadcrumb().apply {
type = "http"
setData("url", mockDevServerUrl)
}

val result = options.beforeBreadcrumb?.execute(breadcrumb, mock())

assertNull("Breadcrumb should be filtered out", result)
}

@Test
fun `beforeBreadcrumb callback does not filter out non dev server or dsn breadcrumbs`() {
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of(
"dsn", "https://[email protected]/1234567",
"devServerUrl", "http://localhost:8081",
)
module.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb = Breadcrumb().apply {
type = "http"
setData("url", "http://testurl.com/service")
}

val result = options.beforeBreadcrumb?.execute(breadcrumb, mock())

assertEquals(breadcrumb, result)
}

@Test
fun `the breadcrumb is not filtered out when the dev server url and dsn are not passed`() {
val options = SentryAndroidOptions()
module.getSentryAndroidOptions(options, JavaOnlyMap(), logger)

val breadcrumb = Breadcrumb().apply {
type = "http"
setData("url", "http://testurl.com/service")
}

val result = options.beforeBreadcrumb?.execute(breadcrumb, mock())

assertEquals(breadcrumb, result)
}

@Test
fun `the breadcrumb is not filtered out when the dev server url is not passed and the dsn does not match`() {
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of("dsn", "https://[email protected]/1234567")
module.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb = Breadcrumb().apply {
type = "http"
setData("url", "http://testurl.com/service")
}

val result = options.beforeBreadcrumb?.execute(breadcrumb, mock())

assertEquals(breadcrumb, result)
}

@Test
fun `the breadcrumb is not filtered out when the dev server url does not match and the dsn is not passed`() {
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of("devServerUrl", "http://localhost:8081")
module.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb = Breadcrumb().apply {
type = "http"
setData("url", "http://testurl.com/service")
}

val result = options.beforeBreadcrumb?.execute(breadcrumb, mock())

assertEquals(breadcrumb, result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,85 @@ - (void)testPassesErrorOnWrongDsn
XCTAssertNotNil(error, @"Did not created error on invalid dsn");
}

- (void)testBeforeBreadcrumbsCallbackFiltersOutSentryDsnRequestBreadcrumbs
{
RNSentry *rnSentry = [[RNSentry alloc] init];
NSError *error = nil;

NSDictionary *_Nonnull mockedDictionary = @{
@"dsn" : @"https://[email protected]/1234567",
@"devServerUrl" : @"http://localhost:8081"
};
SentryOptions *options = [rnSentry createOptionsWithDictionary:mockedDictionary error:&error];

SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc] init];
breadcrumb.type = @"http";
breadcrumb.data = @{ @"url" : @"https://def.ingest.sentry.io/1234567" };

SentryBreadcrumb *result = options.beforeBreadcrumb(breadcrumb);

XCTAssertNil(result, @"Breadcrumb should be filtered out");
}

- (void)testBeforeBreadcrumbsCallbackFiltersOutDevServerRequestBreadcrumbs
{
RNSentry *rnSentry = [[RNSentry alloc] init];
NSError *error = nil;

NSString *mockDevServer = @"http://localhost:8081";

NSDictionary *_Nonnull mockedDictionary =
@{ @"dsn" : @"https://[email protected]/1234567", @"devServerUrl" : mockDevServer };
SentryOptions *options = [rnSentry createOptionsWithDictionary:mockedDictionary error:&error];

SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc] init];
breadcrumb.type = @"http";
breadcrumb.data = @{ @"url" : mockDevServer };

SentryBreadcrumb *result = options.beforeBreadcrumb(breadcrumb);

XCTAssertNil(result, @"Breadcrumb should be filtered out");
}

- (void)testBeforeBreadcrumbsCallbackDoesNotFiltersOutNonDevServerOrDsnRequestBreadcrumbs
{
RNSentry *rnSentry = [[RNSentry alloc] init];
NSError *error = nil;

NSDictionary *_Nonnull mockedDictionary = @{
@"dsn" : @"https://[email protected]/1234567",
@"devServerUrl" : @"http://localhost:8081"
};
SentryOptions *options = [rnSentry createOptionsWithDictionary:mockedDictionary error:&error];

SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc] init];
breadcrumb.type = @"http";
breadcrumb.data = @{ @"url" : @"http://testurl.com/service" };

SentryBreadcrumb *result = options.beforeBreadcrumb(breadcrumb);

XCTAssertEqual(breadcrumb, result);
}

- (void)testBeforeBreadcrumbsCallbackKeepsBreadcrumbWhenDevServerUrlIsNotPassedAndDsnDoesNotMatch
{
RNSentry *rnSentry = [[RNSentry alloc] init];
NSError *error = nil;

NSDictionary *_Nonnull mockedDictionary = @{ // dsn is always validated in SentryOptions initialization
@"dsn" : @"https://[email protected]/1234567"
};
SentryOptions *options = [rnSentry createOptionsWithDictionary:mockedDictionary error:&error];

SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc] init];
breadcrumb.type = @"http";
breadcrumb.data = @{ @"url" : @"http://testurl.com/service" };

SentryBreadcrumb *result = options.beforeBreadcrumb(breadcrumb);

XCTAssertEqual(breadcrumb, result);
}

- (void)testEventFromSentryCocoaReactNativeHasOriginAndEnvironmentTags
{
RNSentry *rnSentry = [[RNSentry alloc] init];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -277,6 +279,21 @@ protected void getSentryAndroidOptions(
options.getReplayController().setBreadcrumbConverter(new RNSentryReplayBreadcrumbConverter());
}

// Exclude Dev Server and Sentry Dsn request from Breadcrumbs
String dsn = getURLFromDSN(rnOptions.getString("dsn"));
String devServerUrl = rnOptions.getString("devServerUrl");
options.setBeforeBreadcrumb(
(breadcrumb, hint) -> {
Object urlObject = breadcrumb.getData("url");
String url = urlObject instanceof String ? (String) urlObject : "";
if ("http".equals(breadcrumb.getType())
&& ((dsn != null && url.startsWith(dsn))
|| (devServerUrl != null && url.startsWith(devServerUrl)))) {
return null;
}
return breadcrumb;
});

// React native internally throws a JavascriptException.
// we want to ignore it on the native side to avoid sending it twice.
options.addIgnoredExceptionForType(JavascriptException.class);
Expand Down Expand Up @@ -1001,4 +1018,17 @@ private boolean checkAndroidXAvailability() {
private boolean isFrameMetricsAggregatorAvailable() {
return androidXAvailable && frameMetricsAggregator != null;
}

public static @Nullable String getURLFromDSN(@Nullable String dsn) {
if (dsn == null) {
return null;
}
URI uri = null;
try {
uri = new URI(dsn);
} catch (URISyntaxException e) {
return null;
}
return uri.getScheme() + "://" + uri.getHost();
}
}
25 changes: 25 additions & 0 deletions packages/core/ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ - (SentryOptions *_Nullable)createOptionsWithDictionary:(NSDictionary *_Nonnull)
return nil;
}

// Exclude Dev Server and Sentry Dsn request from Breadcrumbs
NSString *dsn = [self getURLFromDSN:[mutableOptions valueForKey:@"dsn"]];
NSString *devServerUrl = [mutableOptions valueForKey:@"devServerUrl"];
sentryOptions.beforeBreadcrumb
= ^SentryBreadcrumb *_Nullable(SentryBreadcrumb *_Nonnull breadcrumb)
{
NSString *url = breadcrumb.data[@"url"] ?: @"";

if ([@"http" isEqualToString:breadcrumb.type]
&& ((dsn != nil && [url hasPrefix:dsn])
|| (devServerUrl != nil && [url hasPrefix:devServerUrl]))) {
return nil;
}
return breadcrumb;
};

if ([mutableOptions valueForKey:@"enableNativeCrashHandling"] != nil) {
BOOL enableNativeCrashHandling = [mutableOptions[@"enableNativeCrashHandling"] boolValue];

Expand Down Expand Up @@ -204,6 +220,15 @@ - (SentryOptions *_Nullable)createOptionsWithDictionary:(NSDictionary *_Nonnull)
return sentryOptions;
}

- (NSString *_Nullable)getURLFromDSN:(NSString *)dsn
{
NSURL *url = [NSURL URLWithString:dsn];
if (!url) {
return nil;
}
return [NSString stringWithFormat:@"%@://%@", url.scheme, url.host];
}

- (void)setEventOriginTag:(SentryEvent *)event
{
if (event.sdk != nil) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
import { dateTimestampInSeconds, logger, SentryError } from '@sentry/utils';
import { Alert } from 'react-native';

import { getDevServer } from './integrations/debugsymbolicatorutils';
import { defaultSdkInfo } from './integrations/sdkinfo';
import { getDefaultSidecarUrl } from './integrations/spotlight';
import type { ReactNativeClientOptions } from './options';
Expand Down Expand Up @@ -146,6 +147,7 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
NATIVE.initNativeSdk({
...this._options,
defaultSidecarUrl: getDefaultSidecarUrl(),
devServerUrl: getDevServer()?.url || '',
mobileReplayOptions:
this._integrations[MOBILE_REPLAY_INTEGRATION_NAME] &&
'options' in this._integrations[MOBILE_REPLAY_INTEGRATION_NAME]
Expand Down
44 changes: 42 additions & 2 deletions packages/core/src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
defaultStackParser,
makeFetchTransport,
} from '@sentry/react';
import type { Integration, Scope,UserFeedback } from '@sentry/types';
import type { Breadcrumb, BreadcrumbHint, Integration, Scope, UserFeedback } from '@sentry/types';
import { logger, stackParserFromStackParserOptions } from '@sentry/utils';
import * as React from 'react';

import { ReactNativeClient } from './client';
import { getDevServer } from './integrations/debugsymbolicatorutils';
import { getDefaultIntegrations } from './integrations/default';
import type { ReactNativeClientOptions, ReactNativeOptions, ReactNativeWrapperOptions } from './options';
import { shouldEnableNativeNagger } from './options';
Expand Down Expand Up @@ -62,6 +63,45 @@ export function init(passedOptions: ReactNativeOptions): void {
enableSyncToNative(getIsolationScope());
}

const getURLFromDSN = (dsn: string | null): string | undefined => {
if (!dsn) {
return undefined;
}
try {
const url = new URL(dsn);
return `${url.protocol}//${url.host}`;
} catch (e) {
logger.error('Failed to extract url from DSN', e);
return undefined;
}
};

const userBeforeBreadcrumb = safeFactory(passedOptions.beforeBreadcrumb, { loggerMessage: 'The beforeBreadcrumb threw an error' });

// Exclude Dev Server and Sentry Dsn request from Breadcrumbs
const devServerUrl = getDevServer()?.url;
const dsn = getURLFromDSN(passedOptions.dsn);
const defaultBeforeBreadcrumb = (breadcrumb: Breadcrumb, _hint?: BreadcrumbHint): Breadcrumb | null => {
const type = breadcrumb.type || '';
const url = typeof breadcrumb.data?.url === 'string' ? breadcrumb.data.url : '';
if (type === 'http' && ((devServerUrl && url.startsWith(devServerUrl)) || (dsn && url.startsWith(dsn)))) {
return null;
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
}
return breadcrumb;
};

const chainedBeforeBreadcrumb = (breadcrumb: Breadcrumb, hint?: BreadcrumbHint): Breadcrumb | null => {
let modifiedBreadcrumb = breadcrumb;
if (userBeforeBreadcrumb) {
const result = userBeforeBreadcrumb(breadcrumb, hint);
if (result === null) {
return null;
}
modifiedBreadcrumb = result;
}
return defaultBeforeBreadcrumb(modifiedBreadcrumb, hint);
};

const options: ReactNativeClientOptions = {
...DEFAULT_OPTIONS,
...passedOptions,
Expand All @@ -81,7 +121,7 @@ export function init(passedOptions: ReactNativeOptions): void {
maxQueueSize,
integrations: [],
stackParser: stackParserFromStackParserOptions(passedOptions.stackParser || defaultStackParser),
beforeBreadcrumb: safeFactory(passedOptions.beforeBreadcrumb, { loggerMessage: 'The beforeBreadcrumb threw an error' }),
beforeBreadcrumb: chainedBeforeBreadcrumb,
initialScope: safeFactory(passedOptions.initialScope, { loggerMessage: 'The initialScope threw an error' }),
};
if ('tracesSampler' in options) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface Screenshot {
}

export type NativeSdkOptions = Partial<ReactNativeClientOptions> & {
devServerUrl: string | undefined;
defaultSidecarUrl: string | undefined;
} & {
mobileReplayOptions: MobileReplayOptions | undefined;
Expand Down
Loading
Loading