Skip to content

Commit

Permalink
Merge branch 'main' into antonis/remove-deprecated-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-zimerman authored Nov 18, 2024
2 parents 3ebf513 + c686700 commit c30dae5
Show file tree
Hide file tree
Showing 28 changed files with 342 additions and 63 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
## Unreleased

### Fixes

- Prevents exception capture context from being overwritten by native scope sync ([#4124](https://github.com/getsentry/sentry-react-native/pull/4124))

## 6.2.0

### Features

- Enables Spotlight in Android and iOS SDKs ([#4211](https://github.com/getsentry/sentry-react-native/pull/4211))
Expand Down
26 changes: 15 additions & 11 deletions dev-packages/e2e-tests/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,19 @@ if (actions.includes('test')) {
execFileSync('adb', ['install', '-r', '-d', testApp]);
}

execSync(
`maestro test maestro \
--env=APP_ID="${appId}" \
--env=SENTRY_AUTH_TOKEN="${sentryAuthToken}" \
--debug-output maestro-logs \
--flatten-debug-output`,
{
stdio: 'inherit',
cwd: e2eDir,
},
);
if (sentryAuthToken === undefined) {
console.log('Skipping maestro test due to unavailable SENTRY_AUTH_TOKEN');
} else {
execSync(
`maestro test maestro \
--env=APP_ID="${appId}" \
--env=SENTRY_AUTH_TOKEN="${sentryAuthToken}" \
--debug-output maestro-logs \
--flatten-debug-output`,
{
stdio: 'inherit',
cwd: e2eDir,
},
);
}
}
4 changes: 2 additions & 2 deletions dev-packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sentry-react-native-e2e-tests",
"version": "6.1.0",
"version": "6.2.0",
"private": true,
"description": "Sentry React Native End to End Tests Library",
"main": "dist/index.js",
Expand All @@ -13,7 +13,7 @@
"devDependencies": {
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@sentry/react-native": "6.1.0",
"@sentry/react-native": "6.2.0",
"@sentry/utils": "8.37.1",
"@types/node": "^20.9.3",
"@types/react": "^18.2.64",
Expand Down
2 changes: 1 addition & 1 deletion dev-packages/type-check/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sentry-react-native-type-check",
"private": true,
"version": "6.1.0",
"version": "6.2.0",
"scripts": {
"type-check": "./run-type-check.sh"
}
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "6.1.0",
"version": "6.2.0",
"packages": [
"packages/*",
"dev-packages/*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.sentry.react

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import com.facebook.react.bridge.PromiseImpl
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.WritableMap
import com.facebook.soloader.SoLoader
import io.sentry.Breadcrumb
import io.sentry.Scope
import io.sentry.SentryOptions
import io.sentry.android.core.SentryAndroidOptions
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class RNSentryModuleImplTest {

private lateinit var module: RNSentryModuleImpl
private lateinit var context: Context

@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
SoLoader.init(context, false)
val reactContext = ReactApplicationContext(context)
module = RNSentryModuleImpl(reactContext)
}

@Test
fun fetchNativeDeviceContextsWithNullContext() {
val options = SentryAndroidOptions()
val scope = Scope(options)
val promise = PromiseImpl({
assertEquals(1, it.size)
assertEquals(null, it[0])
}, {
fail("Promise was rejected unexpectedly")
})
module.fetchNativeDeviceContexts(promise, options, null, scope)
}

@Test
fun fetchNativeDeviceContextsWithInvalidSentryOptions() {
class NotAndroidSentryOptions : SentryOptions()

val options = NotAndroidSentryOptions()
val scope = Scope(options)
val promise = PromiseImpl({
assertEquals(1, it.size)
assertEquals(null, it[0])
}, {
fail("Promise was rejected unexpectedly")
})
module.fetchNativeDeviceContexts(promise, options, context, scope)
}

@Test
fun fetchNativeDeviceContextsFiltersBreadcrumbs() {
val options = SentryAndroidOptions().apply { maxBreadcrumbs = 5 }
val scope = Scope(options)
scope.addBreadcrumb(Breadcrumb("Breadcrumb1-RN").apply { origin = "react-native" })
scope.addBreadcrumb(Breadcrumb("Breadcrumb2-Native"))
scope.addBreadcrumb(Breadcrumb("Breadcrumb3-Native").apply { origin = "java" })
scope.addBreadcrumb(Breadcrumb("Breadcrumb2-RN").apply { origin = "react-native" })
scope.addBreadcrumb(Breadcrumb("Breadcrumb2-RN").apply { origin = "react-native" })

val promise = PromiseImpl({
assertEquals(1, it.size)
assertEquals(true, it[0] is WritableMap)
val actual = it[0] as WritableMap
val breadcrumbs = actual.getArray("breadcrumbs")
assertEquals(2, breadcrumbs?.size())
assertEquals("Breadcrumb2-Native", breadcrumbs?.getMap(0)?.getString("message"))
assertEquals("Breadcrumb3-Native", breadcrumbs?.getMap(1)?.getString("message"))
}, {
fail("Promise was rejected unexpectedly")
})

module.fetchNativeDeviceContexts(promise, options, context, scope)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.rnsentryandroidtester

import com.facebook.react.bridge.JavaOnlyMap
import io.sentry.SentryLevel
import io.sentry.react.RNSentryBreadcrumb
import junit.framework.TestCase.assertEquals
import org.junit.Test
Expand All @@ -10,6 +11,38 @@ import org.junit.runners.JUnit4
@RunWith(JUnit4::class)
class RNSentryBreadcrumbTest {

@Test
fun generatesSentryBreadcrumbFromMap() {
val testData = JavaOnlyMap.of(
"test", "data",
)
val map = JavaOnlyMap.of(
"level", "error",
"category", "testCategory",
"origin", "testOrigin",
"type", "testType",
"message", "testMessage",
"data", testData,
)
val actual = RNSentryBreadcrumb.fromMap(map)
assertEquals(SentryLevel.ERROR, actual.level)
assertEquals("testCategory", actual.category)
assertEquals("testOrigin", actual.origin)
assertEquals("testType", actual.type)
assertEquals("testMessage", actual.message)
assertEquals(testData.toHashMap(), actual.data)
}

@Test
fun reactNativeForMissingOrigin() {
val map = JavaOnlyMap.of(
"message", "testMessage",
)
val actual = RNSentryBreadcrumb.fromMap(map)
assertEquals("testMessage", actual.message)
assertEquals("react-native", actual.origin)
}

@Test
fun nullForMissingCategory() {
val map = JavaOnlyMap.of()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
33AFDFED2B8D14B300AAB120 /* RNSentryFramesTrackerListenerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 33AFDFEC2B8D14B300AAB120 /* RNSentryFramesTrackerListenerTests.m */; };
33AFDFF12B8D15E500AAB120 /* RNSentryDependencyContainerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 33AFDFF02B8D15E500AAB120 /* RNSentryDependencyContainerTests.m */; };
33F58AD02977037D008F60EA /* RNSentryTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 33F58ACF2977037D008F60EA /* RNSentryTests.mm */; };
AEFB00422CC90C4B00EC8A9A /* RNSentryBreadcrumbTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3360843C2C340C76008CC412 /* RNSentryBreadcrumbTests.swift */; };
B5859A50A3E865EF5E61465A /* libPods-RNSentryCocoaTesterTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 650CB718ACFBD05609BF2126 /* libPods-RNSentryCocoaTesterTests.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -221,6 +222,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AEFB00422CC90C4B00EC8A9A /* RNSentryBreadcrumbTests.swift in Sources */,
332D33472CDBDBB600547D76 /* RNSentryReplayOptionsTests.swift in Sources */,
33AFDFF12B8D15E500AAB120 /* RNSentryDependencyContainerTests.m in Sources */,
336084392C32E382008CC412 /* RNSentryReplayBreadcrumbConverterTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Sentry
import XCTest

class RNSentryBreadcrumbTests: XCTestCase {
final class RNSentryBreadcrumbTests: XCTestCase {

func testGeneratesSentryBreadcrumbFromNSDictionary() {
let actualCrumb = RNSentryBreadcrumb.from([
"level": "error",
"category": "testCategory",
"origin": "testOrigin",
"type": "testType",
"message": "testMessage",
"data": [
Expand All @@ -16,11 +17,29 @@ class RNSentryBreadcrumbTests: XCTestCase {

XCTAssertEqual(actualCrumb!.level, SentryLevel.error)
XCTAssertEqual(actualCrumb!.category, "testCategory")
XCTAssertEqual(actualCrumb!.origin, "testOrigin")
XCTAssertEqual(actualCrumb!.type, "testType")
XCTAssertEqual(actualCrumb!.message, "testMessage")
XCTAssertEqual((actualCrumb!.data)!["test"] as! String, "data")
}

func testUsesReactNativeAsDefaultOrigin() {
let actualCrumb = RNSentryBreadcrumb.from([
"message": "testMessage"
])

XCTAssertEqual(actualCrumb!.origin, "react-native")
}

func testKeepsOriginIfSet() {
let actualCrumb = RNSentryBreadcrumb.from([
"message": "testMessage",
"origin": "someOrigin"
])

XCTAssertEqual(actualCrumb!.origin, "someOrigin")
}

func testUsesInfoAsDefaultSentryLevel() {
let actualCrumb = RNSentryBreadcrumb.from([
"message": "testMessage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public static Breadcrumb fromMap(ReadableMap from) {
breadcrumb.setCategory(from.getString("category"));
}

if (from.hasKey("origin")) {
breadcrumb.setOrigin(from.getString("origin"));
} else {
breadcrumb.setOrigin("react-native");
}

if (from.hasKey("level")) {
switch (from.getString("level")) {
case "fatal":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.JavascriptException;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import io.sentry.Breadcrumb;
import io.sentry.HubAdapter;
import io.sentry.ILogger;
import io.sentry.IScope;
Expand Down Expand Up @@ -76,6 +77,7 @@
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -886,18 +888,35 @@ private String readStringFromFile(File path) throws IOException {

public void fetchNativeDeviceContexts(Promise promise) {
final @NotNull SentryOptions options = HubAdapter.getInstance().getOptions();
final @Nullable Context context = this.getReactApplicationContext().getApplicationContext();
final @Nullable IScope currentScope = InternalSentrySdk.getCurrentScope();
fetchNativeDeviceContexts(promise, options, context, currentScope);
}

protected void fetchNativeDeviceContexts(
Promise promise,
final @NotNull SentryOptions options,
final @Nullable Context context,
final @Nullable IScope currentScope) {
if (!(options instanceof SentryAndroidOptions)) {
promise.resolve(null);
return;
}

final @Nullable Context context = this.getReactApplicationContext().getApplicationContext();
if (context == null) {
promise.resolve(null);
return;
}
if (currentScope != null) {
// Remove react-native breadcrumbs
Iterator<Breadcrumb> breadcrumbsIterator = currentScope.getBreadcrumbs().iterator();
while (breadcrumbsIterator.hasNext()) {
Breadcrumb breadcrumb = breadcrumbsIterator.next();
if ("react-native".equals(breadcrumb.getOrigin())) {
breadcrumbsIterator.remove();
}
}
}

final @Nullable IScope currentScope = InternalSentrySdk.getCurrentScope();
final @NotNull Map<String, Object> serialized =
InternalSentrySdk.serializeScope(context, (SentryAndroidOptions) options, currentScope);
final @Nullable Object deviceContext = RNSentryMapConverter.convertToWritable(serialized);
Expand Down
10 changes: 10 additions & 0 deletions packages/core/ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ - (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAd

[serializedScope setValue:contexts forKey:@"contexts"];
[serializedScope removeObjectForKey:@"context"];

// Remove react-native breadcrumbs
NSPredicate *removeRNBreadcrumbsPredicate =
[NSPredicate predicateWithBlock:^BOOL(NSDictionary *breadcrumb, NSDictionary *bindings) {
return ![breadcrumb[@"origin"] isEqualToString:@"react-native"];
}];
NSArray *breadcrumbs = [[serializedScope[@"breadcrumbs"] mutableCopy]
filteredArrayUsingPredicate:removeRNBreadcrumbsPredicate];
[serializedScope setValue:breadcrumbs forKey:@"breadcrumbs"];

resolve(serializedScope);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/ios/RNSentryBreadcrumb.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ + (SentryBreadcrumb *)from:(NSDictionary *)dict

[crumb setLevel:sentryLevel];
[crumb setCategory:dict[@"category"]];
id origin = dict[@"origin"];
if (origin != nil) {
[crumb setOrigin:origin];
} else {
[crumb setOrigin:@"react-native"];
}
[crumb setType:dict[@"type"]];
[crumb setMessage:dict[@"message"]];
[crumb setData:dict[@"data"]];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@sentry/react-native",
"homepage": "https://github.com/getsentry/sentry-react-native",
"repository": "https://github.com/getsentry/sentry-react-native",
"version": "6.1.0",
"version": "6.2.0",
"description": "Official Sentry SDK for react-native",
"typings": "dist/js/index.d.ts",
"types": "dist/js/index.d.ts",
Expand Down
Loading

0 comments on commit c30dae5

Please sign in to comment.