Skip to content

Commit e2e3b07

Browse files
author
farfromrefuge
committed
fix: update plugin for latest sentry
1 parent 95f7754 commit e2e3b07

File tree

12 files changed

+165
-235
lines changed

12 files changed

+165
-235
lines changed

plugin/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
"url": "https://github.com/nativescript-community/sentry"
3333
},
3434
"dependencies": {
35-
"@sentry/browser": "7.15.0",
36-
"@sentry/core": "7.15.0",
37-
"@sentry/hub": "7.15.0",
38-
"@sentry/integrations": "7.15.0",
39-
"@sentry/tracing": "7.15.0",
40-
"@sentry/types": "7.15.0",
41-
"@sentry/utils": "7.15.0",
35+
"@sentry/browser": "^7.88.0",
36+
"@sentry/core": "^7.88.0",
37+
"@sentry/hub": "^7.88.0",
38+
"@sentry/integrations": "^7.88.0",
39+
"@sentry/tracing": "^7.88.0",
40+
"@sentry/types": "^7.88.0",
41+
"@sentry/utils": "^7.88.0",
4242
"stacktrace-parser": "^0.1.10"
4343
},
4444
"gitHead": "1e799d9f3a3eeb19c0fc4188be82a6cded4ea727"

plugin/platforms/android/include.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ android {
77
}
88
}
99
dependencies {
10-
def sentryVersion = project.hasProperty("sentryVersion") ? project.sentryVersion : "6.9.1"
10+
def sentryVersion = project.hasProperty("sentryVersion") ? project.sentryVersion : "7.0.0"
1111
implementation "io.sentry:sentry-android:$sentryVersion"
1212
}

src/client.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { BrowserClient, makeFetchTransport } from '@sentry/browser';
2-
import { BrowserTransportOptions } from '@sentry/browser/types/transports/types';
1+
import { eventFromException, eventFromMessage,makeFetchTransport } from '@sentry/browser';import { BrowserTransportOptions } from '@sentry/browser/types/transports/types';
32
import { FetchImpl } from '@sentry/browser/types/transports/utils';
43
import { BaseClient } from '@sentry/core';
54

6-
import { ClientReportEnvelope, ClientReportItem, Envelope, Event, EventHint, Outcome, SeverityLevel, Transport, UserFeedback } from '@sentry/types';
5+
import { ClientReportEnvelope, ClientReportItem, Envelope, Event, EventHint, Exception, Outcome, SeverityLevel, Thread, Transport, UserFeedback } from '@sentry/types';
76
import { SentryError, dateTimestampInSeconds, logger } from '@sentry/utils';
87

98
import { alert } from '@nativescript/core';
@@ -14,6 +13,7 @@ import { createUserFeedbackEnvelope, items } from './utils/envelope';
1413
import { mergeOutcomes } from './utils/outcome';
1514
import { NATIVE } from './wrapper';
1615
import { Screenshot } from './integrations/screenshot';
16+
import { rewriteFrameIntegration } from './sdk';
1717

1818

1919
/**
@@ -24,7 +24,7 @@ import { Screenshot } from './integrations/screenshot';
2424
*/
2525
export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
2626
private _outcomesBuffer: Outcome[];
27-
private readonly _browserClient: BrowserClient;
27+
// private readonly _browserClient: BrowserClient;
2828

2929
/**
3030
* Creates a new React Native SDK instance.
@@ -45,14 +45,14 @@ export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
4545

4646
this._outcomesBuffer = [];
4747

48-
this._browserClient = new BrowserClient({
49-
dsn: options.dsn,
50-
transport: options.transport,
51-
transportOptions: options.transportOptions,
52-
stackParser: options.stackParser,
53-
integrations: [],
54-
_metadata: options._metadata,
55-
});
48+
// this._browserClient = new BrowserClient({
49+
// dsn: options.dsn,
50+
// transport: options.transport,
51+
// transportOptions: options.transportOptions,
52+
// stackParser: options.stackParser,
53+
// integrations: [],
54+
// _metadata: options._metadata,
55+
// });
5656

5757
this._initNativeSdk();
5858
}
@@ -65,15 +65,43 @@ export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
6565
if (exception['stackTrace']) {
6666
exception['stacktrace'] = exception['stackTrace'];
6767
}
68-
Screenshot.attachScreenshotToEventHint(hint, this._options);
69-
return this._browserClient.eventFromException(exception, hint);
68+
const hintWithScreenshot = Screenshot.attachScreenshotToEventHint(hint, this._options);
69+
return eventFromException(
70+
this._options.stackParser,
71+
exception,
72+
hintWithScreenshot,
73+
this._options.attachStacktrace,
74+
);
75+
// return this._browserClient.eventFromException(exception, hint);
7076
}
7177

7278
/**
7379
* @inheritDoc
7480
*/
75-
public eventFromMessage(_message: string, _level?: SeverityLevel, _hint?: EventHint): PromiseLike<Event> {
76-
return this._browserClient.eventFromMessage(_message, _level, _hint);
81+
public eventFromMessage(message: string, level?: SeverityLevel, hint?: EventHint): PromiseLike<Event> {
82+
return eventFromMessage(
83+
this._options.stackParser,
84+
message,
85+
level,
86+
hint,
87+
this._options.attachStacktrace,
88+
).then((event: Event) => {
89+
// TMP! Remove this function once JS SDK uses threads for messages
90+
if (!event.exception?.values || event.exception.values.length <= 0) {
91+
return event;
92+
}
93+
const values = event.exception.values.map((exception: Exception): Thread => {
94+
if (exception.stacktrace) {
95+
exception.stacktrace.frames.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
96+
}
97+
return {
98+
stacktrace: exception.stacktrace,
99+
};
100+
});
101+
(event as { threads?: { values: Thread[] } }).threads = { values };
102+
delete event.exception;
103+
return event;
104+
});
77105
}
78106

79107
/**

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ _addTracingExtensions();
5151

5252
import * as Integrations from './integrations';
5353
import { SDK_NAME, SDK_VERSION } from './version';
54+
import { Trace } from '@nativescript/core';
5455
export { NativescriptOptions } from './options';
5556
export { NativescriptClient } from './client';
5657

@@ -76,3 +77,16 @@ export {
7677
// } from './tracing';
7778

7879
export { Integrations, SDK_NAME, SDK_VERSION };
80+
81+
export const SentryTraceCategory = 'Sentry';
82+
83+
export enum CLogTypes {
84+
log = Trace.messageType.log,
85+
info = Trace.messageType.info,
86+
warning = Trace.messageType.warn,
87+
error = Trace.messageType.error
88+
}
89+
90+
export const CLog = (type: CLogTypes, ...args) => {
91+
Trace.write(args.map((a) => (a && typeof a === 'object' ? JSON.stringify(a) : a)).join(' '), SentryTraceCategory, type);
92+
};

src/integrations/debugsymbolicator.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
1+
import { addEventProcessor, addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
22
import { Event, EventHint, Integration, StackFrame, StackLineParser } from '@sentry/types';
33
import { logger, stackParserFromStackParserOptions } from '@sentry/utils';
44

@@ -122,17 +122,20 @@ export class DebugSymbolicator implements Integration {
122122
* @inheritDoc
123123
*/
124124
public setupOnce(): void {
125+
console.log('setupOnce');
125126
addGlobalEventProcessor(async (event: Event, hint?: EventHint) => {
126127
const self = getCurrentHub().getIntegration(DebugSymbolicator);
128+
console.log('addGlobalEventProcessor', hint.originalException);
127129
if (!self || hint === undefined || hint.originalException === undefined) {
128130
return event;
129131
}
130132
// @ts-ignore
131133
const error: NativescriptError = hint.originalException;
132134
// const parseErrorStack = require('react-native/Libraries/Core/Devtools/parseErrorStack');
133135
const stack = parseErrorStack(error);
134-
console.log('addGlobalEventProcessor', error);
135136
console.log('stack', stack);
137+
console.log('event.exception?.values?.[0].stacktrace', event.exception?.values?.[0].stacktrace);
138+
// console.log('stack', stack);
136139

137140

138141
// Ideally this should go into contexts but android sdk doesn't support it
@@ -174,7 +177,8 @@ export class DebugSymbolicator implements Integration {
174177
* @param frames StackFrame[]
175178
*/
176179
private _replaceFramesInEvent(event: Event, frames: StackFrame[]): void {
177-
if (event.exception && event.exception.values && event.exception.values[0] && event.exception.values[0].stacktrace) {
180+
console.log('_replaceFramesInEvent');
181+
if (event.exception?.values?.[0].stacktrace) {
178182
event.exception.values[0].stacktrace.frames = frames.reverse();
179183
}
180184
}

src/integrations/devicecontext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
1+
import { addEventProcessor, addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
22
import { Contexts, Event, Integration } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44
import { NATIVE } from '../wrapper';
@@ -18,7 +18,7 @@ export class DeviceContext implements Integration {
1818
* @inheritDoc
1919
*/
2020
public setupOnce(): void {
21-
addGlobalEventProcessor(async (event: Event) => {
21+
addEventProcessor(async (event: Event) => {
2222
const self = getCurrentHub().getIntegration(DeviceContext);
2323
if (!self) {
2424
return event;

src/options.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ export interface BaseNativescriptOptions {
4545
/** The interval to end a session if the App goes to the background. */
4646
sessionTrackingIntervalMillis?: number;
4747

48-
/** Enable scope sync from Java to NDK on Android */
48+
/** Enable NDK on Android
49+
*
50+
* @default true
51+
*/
52+
enableNdk?: boolean;
53+
54+
/** Enable scope sync from Java to NDK on Android
55+
* Only has an effect if `enableNdk` is `true`.
56+
*/
4957
enableNdkScopeSync?: boolean;
5058

5159
/** When enabled, all the threads are automatically attached to all logged events on Android */
@@ -85,12 +93,6 @@ export interface BaseNativescriptOptions {
8593
*/
8694
enableOutOfMemoryTracking?: boolean;
8795

88-
/**
89-
* Set data to the inital scope
90-
* @deprecated Use `Sentry.configureScope(...)`
91-
*/
92-
initialScope?: CaptureContext;
93-
9496

9597
/**
9698
* The max cache items for capping the number of envelopes.
@@ -120,6 +122,18 @@ export interface BaseNativescriptOptions {
120122
* @default 2
121123
*/
122124
appHangsTimeoutInterval?: number;
125+
126+
/**
127+
* The max queue size for capping the number of envelopes waiting to be sent by Transport.
128+
*/
129+
maxQueueSize?: number;
130+
}
131+
132+
export interface ReactNativeTransportOptions extends BrowserTransportOptions {
133+
/**
134+
* @deprecated use `maxQueueSize` in the root of the SDK options.
135+
*/
136+
bufferSize?: number;
123137
}
124138

125139
/**

src/sdk.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,47 @@ import { NativescriptErrorHandlersOptions } from './integrations/nativescripterr
1313
import { SdkInfo } from './integrations/sdkinfo';
1414
// import { NativescriptScope } from './scope';
1515
import { NativescriptTracing } from './tracing';
16-
import { makeNativescriptTransport } from './transports/native';
16+
import { DEFAULT_BUFFER_SIZE, makeNativescriptTransport } from './transports/native';
1717
import { makeUtf8TextEncoder } from './transports/TextEncoder';
1818
import { safeFactory, safeTracesSampler } from './utils/safe';
1919
import { NATIVE } from './wrapper';
2020
import { parseErrorStack } from './integrations/debugsymbolicator';
2121
import { Screenshot } from './integrations/screenshot';
22+
import { getDefaultEnvironment } from './utils/environment';
2223

2324

24-
const STACKTRACE_LIMIT = 50;
25-
function stripSentryFramesAndReverse(stack) {
26-
if (!stack.length) {
27-
return [];
28-
}
25+
// const STACKTRACE_LIMIT = 50;
26+
// function stripSentryFramesAndReverse(stack) {
27+
// if (!stack.length) {
28+
// return [];
29+
// }
2930

30-
let localStack = stack;
31+
// let localStack = stack;
3132

32-
const firstFrameFunction = localStack[0].function || '';
33-
const lastFrameFunction = localStack[localStack.length - 1].function || '';
33+
// const firstFrameFunction = localStack[0].function || '';
34+
// const lastFrameFunction = localStack[localStack.length - 1].function || '';
3435

35-
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
36-
if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
37-
localStack = localStack.slice(1);
38-
}
36+
// // If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
37+
// if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
38+
// localStack = localStack.slice(1);
39+
// }
3940

40-
// If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
41-
if (lastFrameFunction.indexOf('sentryWrapped') !== -1) {
42-
localStack = localStack.slice(0, -1);
43-
}
41+
// // If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
42+
// if (lastFrameFunction.indexOf('sentryWrapped') !== -1) {
43+
// localStack = localStack.slice(0, -1);
44+
// }
4445

45-
// The frame where the crash happened, should be the last entry in the array
46-
return localStack
47-
.slice(0, STACKTRACE_LIMIT)
48-
.map(frame => ({
49-
...frame,
50-
filename: frame.filename || localStack[0].filename,
51-
function: frame.function || undefined,
52-
}));
53-
}
46+
// // The frame where the crash happened, should be the last entry in the array
47+
// return localStack
48+
// .slice(0, STACKTRACE_LIMIT)
49+
// .map(frame => ({
50+
// ...frame,
51+
// filename: frame.filename || localStack[0].filename,
52+
// function: frame.function || undefined,
53+
// }));
54+
// }
5455
const defaultStackParser = (stack, skipFirst = 0) => {
5556
let frames = parseErrorStack({ stack } as any);
56-
5757
if (skipFirst) {
5858
frames = frames.slice(skipFirst);
5959
}
@@ -78,6 +78,8 @@ const DEFAULT_OPTIONS: NativescriptOptions & NativescriptErrorHandlersOptions =
7878
textEncoder: makeUtf8TextEncoder(),
7979
},
8080
sendClientReports: true,
81+
maxQueueSize: DEFAULT_BUFFER_SIZE,
82+
attachStacktrace: true
8183
};
8284

8385
export let rewriteFrameIntegration: {
@@ -91,6 +93,10 @@ export function init(passedOptions: NativescriptOptions): void {
9193
const NativescriptHub = new Hub(undefined, new Scope());
9294
// const NativescriptHub = new Hub(undefined, new NativescriptScope());
9395
makeMain(NativescriptHub);
96+
97+
const maxQueueSize = passedOptions.maxQueueSize
98+
?? passedOptions.transportOptions?.bufferSize
99+
?? DEFAULT_OPTIONS.maxQueueSize;
94100
const options: NativescriptClientOptions & NativescriptOptions = {
95101
...DEFAULT_OPTIONS,
96102
...passedOptions,
@@ -99,14 +105,20 @@ export function init(passedOptions: NativescriptOptions): void {
99105
transportOptions: {
100106
...DEFAULT_OPTIONS.transportOptions,
101107
...(passedOptions.transportOptions ?? {}),
108+
bufferSize: maxQueueSize,
102109
},
110+
maxQueueSize ,
103111
integrations: [],
104112
// integrations: getIntegrationsToSetup(passedOptions),
105113
stackParser: stackParserFromStackParserOptions(passedOptions.stackParser || defaultStackParser),
106114
beforeBreadcrumb: safeFactory(passedOptions.beforeBreadcrumb, { loggerMessage: 'The beforeBreadcrumb threw an error' }),
107115
initialScope: safeFactory(passedOptions.initialScope, { loggerMessage: 'The initialScope threw an error' }),
108116
tracesSampler: safeTracesSampler(passedOptions.tracesSampler),
109117
};
118+
119+
if (!('environment' in options)) {
120+
options.environment = getDefaultEnvironment();
121+
}
110122
// As long as tracing is opt in with either one of these options, then this is how we determine tracing is enabled.
111123
const tracingEnabled =
112124
typeof options.tracesSampler !== 'undefined' ||
@@ -202,7 +214,7 @@ export function nativeCrash(): void {
202214
* Use this before applying any realtime updates such as code-push or expo updates.
203215
* Not yet working on Android
204216
*/
205-
export async function flush(timeout: number): Promise<boolean> {
217+
export async function flush(timeout: number = 0): Promise<boolean> {
206218
try {
207219
const client = getCurrentHub().getClient<NativescriptClient>();
208220

src/tracing/nstracing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class NativescriptTracing implements Integration {
147147
} = this.options;
148148

149149
this._getCurrentHub = getCurrentHub;
150-
console.log('NativescriptTracing', this.options);
150+
// console.log('NativescriptTracing', this.options);
151151
if (enableAppStartTracking) {
152152
void this._instrumentAppStart();
153153
}

0 commit comments

Comments
 (0)