Skip to content

Commit

Permalink
fix: improving integration
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Oct 15, 2022
1 parent d63d12a commit 7e8be4e
Show file tree
Hide file tree
Showing 13 changed files with 755 additions and 379 deletions.
2 changes: 2 additions & 0 deletions plugin/platforms/ios/src/NSSentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

@property (class, nonatomic, assign, readonly) SentryScreenFrames *currentScreenFrames;
@property (class, nullable, nonatomic, readonly) SentryAppStartMeasurement *appStartMeasurement;
@property (class, nonatomic, assign) BOOL appStartMeasurementHybridSDKMode;
@property (class, nonatomic, assign) BOOL framesTrackingMeasurementHybridSDKMode;

@end
17 changes: 16 additions & 1 deletion plugin/platforms/ios/src/NSSentry.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

@implementation NSSentrySDK

+ (void)captureEnvelope:(SentryEnvelope *)envelope
{
return [PrivateSentrySDKOnly captureEnvelope:envelope];
}
+ (void)storeEnvelope:(SentryEnvelope *)envelope
{
return [PrivateSentrySDKOnly storeEnvelope:envelope];
}
+ (nullable SentryEnvelope *)envelopeWithData:(NSData *)data
{
return [PrivateSentrySDKOnly envelopeWithData:data];
Expand All @@ -27,5 +35,12 @@ + (BOOL)isFramesTrackingRunning
{
return [PrivateSentrySDKOnly isFramesTrackingRunning];
}

+ (void)setAppStartMeasurementHybridSDKMode:(BOOL)appStartMeasurementHybridSDKMode
{
[PrivateSentrySDKOnly setAppStartMeasurementHybridSDKMode:appStartMeasurementHybridSDKMode];
}
+ (void)setFramesTrackingMeasurementHybridSDKMode:(BOOL)framesTrackingMeasurementHybridSDKMode
{
[PrivateSentrySDKOnly setFramesTrackingMeasurementHybridSDKMode:framesTrackingMeasurementHybridSDKMode];
}
@end
13 changes: 13 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
this._sendEnvelope(envelope);
}

/**
* @inheritDoc
*/
// sendEvent(event: Event, hint = {}) {
// if (this._dsn) {
// if (NATIVE.sendEvent) {
// NATIVE.sendEvent(event, hint);
// } else {
// super.sendEvent(event, hint);
// }
// }
// }

/**
* @inheritdoc
*/
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export {
captureException,
captureEvent,
captureMessage,
configureScope,
getHubFromCarrier,
getCurrentHub,
Hub,
Scope,
setContext,
setExtra,
setExtras,
withScope,
configureScope,
setTag,
setTags,
setUser,
startTransaction,
withScope,
startTransaction
} from '@sentry/core';

// We need to import it so we patch the hub with global functions
Expand Down
1 change: 1 addition & 0 deletions src/integrations/devicecontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class DeviceContext implements Integration {
}

try {
console.log('about to fetchNativeDeviceContexts');
const contexts = await NATIVE.fetchNativeDeviceContexts();

const context = contexts['context'] as Contexts ?? {};
Expand Down
1 change: 1 addition & 0 deletions src/integrations/nativescripterrorhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class NativescriptErrorHandlers implements Integration {
handlingFatal = false;

private async globalHander(error: any, isFatal?: boolean) {
console.log('globalHander', error, isFatal);
// We want to handle fatals, but only in production mode.
const shouldHandleFatal = isFatal && !__DEV__;
if (shouldHandleFatal) {
Expand Down
172 changes: 93 additions & 79 deletions src/scope.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,109 @@
import { Scope } from '@sentry/hub';
import { Scope } from '@sentry/core';
import { Attachment, Breadcrumb, User } from '@sentry/types';

import { NATIVE } from './wrapper';

/**
* Extends the scope methods to set scope on the Native SDKs
*/
export class NativescriptScope extends Scope {
/**
* @inheritDoc
*/
public setUser(user: User | null): this {
NATIVE.setUser(user);
return super.setUser(user);
}

/**
* @inheritDoc
*/
public setTag(key: string, value: string): this {
NATIVE.setTag(key, value);
return super.setTag(key, value);
}
const methods = ['addBreadcrumb', 'addAttachment'];

/**
* @inheritDoc
*/
public setTags(tags: { [key: string]: string }): this {
// As native only has setTag, we just loop through each tag key.
Object.keys(tags).forEach((key) => {
NATIVE.setTag(key, tags[key]);
});
return super.setTags(tags);
}
// methods.forEach(m=>{
// const originalMethod =Scope.prototype[m] as Function;
// Scope.prototype[m] = function(...args) {
// NATIVE[m](...args);
// return originalMethod.call(this, ...args);
// };
// });

/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setExtras(extras: { [key: string]: any }): this {
Object.keys(extras).forEach((key) => {
NATIVE.setExtra(key, extras[key]);
});
return super.setExtras(extras);
}
// export class NativescriptScope extends Scope {
// /**
// * @inheritDoc
// */
// public setUser(user: User | null): this {
// NATIVE.setUser(user);
// return super.setUser(user);
// }

/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any
public setExtra(key: string, extra: any): this {
NATIVE.setExtra(key, extra);
return super.setExtra(key, extra);
}
// /**
// * @inheritDoc
// */
// public setTag(key: string, value: string): this {
// NATIVE.setTag(key, value);
// return super.setTag(key, value);
// }

/**
* @inheritDoc
*/
public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this {
NATIVE.addBreadcrumb(breadcrumb);
return super.addBreadcrumb(breadcrumb, maxBreadcrumbs);
}
// /**
// * @inheritDoc
// */
// public setTags(tags: { [key: string]: string }): this {
// // As native only has setTag, we just loop through each tag key.
// Object.keys(tags).forEach((key) => {
// NATIVE.setTag(key, tags[key]);
// });
// return super.setTags(tags);
// }

/**
* @inheritDoc
*/
public clearBreadcrumbs(): this {
NATIVE.clearBreadcrumbs();
return super.clearBreadcrumbs();
}
// /**
// * @inheritDoc
// */
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
// public setExtras(extras: { [key: string]: any }): this {
// Object.keys(extras).forEach((key) => {
// NATIVE.setExtra(key, extras[key]);
// });
// return super.setExtras(extras);
// }

/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setContext(key: string, context: { [key: string]: any } | null): this {
NATIVE.setContext(key, context);
return super.setContext(key, context);
}
// /**
// * @inheritDoc
// */
// // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any
// public setExtra(key: string, extra: any): this {
// NATIVE.setExtra(key, extra);
// return super.setExtra(key, extra);
// }

/**
* @inheritDoc
*/
public addAttachment(attachment: Attachment): this {
return super.addAttachment(attachment);
}
// /**
// * @inheritDoc
// */
// public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this {
// NATIVE.addBreadcrumb(breadcrumb);
// return super.addBreadcrumb(breadcrumb, maxBreadcrumbs);
// }

/**
* @inheritDoc
*/
public clearAttachments(): this {
return super.clearAttachments();
}
}
// /**
// * @inheritDoc
// */
// public clearBreadcrumbs(): this {
// NATIVE.clearBreadcrumbs();
// return super.clearBreadcrumbs();
// }

// /**
// * @inheritDoc
// */
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
// public setContext(key: string, context: { [key: string]: any } | null): this {
// NATIVE.setContext(key, context);
// return this;
// // return super.setContext(key, context);
// }

// /**
// * @inheritDoc
// */
// public addAttachment(attachment: Attachment): this {
// console.log('test addAttachment', attachment);
// NATIVE.addAttachment(attachment);
// return super.addAttachment(attachment);
// }

// /**
// * @inheritDoc
// */
// public clearAttachments(): this {
// return super.clearAttachments();
// }
// }
27 changes: 15 additions & 12 deletions src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Integrations, defaultStackParser, getCurrentHub, defaultIntegrations as sentryDefaultIntegrations } from '@sentry/browser';
import { getIntegrationsToSetup, initAndBind, setExtra } from '@sentry/core';
import { Hub, makeMain } from '@sentry/hub';
import { Hub, Scope, getIntegrationsToSetup, initAndBind, makeMain, setExtra } from '@sentry/core';
import { RewriteFrames } from '@sentry/integrations';
import { Integration, Scope, StackFrame, UserFeedback } from '@sentry/types';
import { getGlobalObject, logger, stackParserFromStackParserOptions } from '@sentry/utils';
import { Integration, StackFrame, UserFeedback } from '@sentry/types';
import { logger, stackParserFromStackParserOptions } from '@sentry/utils';

import { NativescriptClientOptions, NativescriptOptions, NativescriptWrapperOptions } from './options';
import { NativescriptClientOptions, NativescriptOptions } from './options';

import { NativescriptClient } from './client';
import { NativescriptScope } from './scope';
import { DebugSymbolicator, DeviceContext, NativescriptErrorHandlers, Release } from './integrations';
import { DeviceContext, NativescriptErrorHandlers, Release } from './integrations';
import { EventOrigin } from './integrations/eventorigin';
import { NativescriptErrorHandlersOptions } from './integrations/nativescripterrorhandlers';
import { SdkInfo } from './integrations/sdkinfo';
// import { NativescriptScope } from './scope';
import { NativescriptTracing } from './tracing';
import { makeNativescriptTransport } from './transports/native';
import { NativescriptErrorHandlersOptions } from './integrations/nativescripterrorhandlers';
import { makeUtf8TextEncoder } from './transports/TextEncoder';
import { safeFactory, safeTracesSampler } from './utils/safe';
import { NativescriptTracing } from './tracing';
import { NATIVE } from './wrapper';

const IGNORED_DEFAULT_INTEGRATIONS = [
'GlobalHandlers', // We will use the react-native internal handlers
Expand Down Expand Up @@ -46,9 +46,9 @@ export let rewriteFrameIntegration: {
*/
export function init(passedOptions: NativescriptOptions): void {

const NativescriptHub = new Hub(undefined, new NativescriptScope());
const NativescriptHub = new Hub(undefined, new Scope());
// const NativescriptHub = new Hub(undefined, new NativescriptScope());
makeMain(NativescriptHub);

const options: NativescriptClientOptions & NativescriptOptions = {
...DEFAULT_OPTIONS,
...passedOptions,
Expand Down Expand Up @@ -111,6 +111,7 @@ export function init(passedOptions: NativescriptOptions): void {
new EventOrigin(),
new SdkInfo()
]);
console.log('test', options.enableNative);
if (!!options.enableNative) {
defaultIntegrations.push(new DeviceContext());
}
Expand Down Expand Up @@ -211,7 +212,9 @@ export function captureUserFeedback(feedback: UserFeedback): void {
export function withScope(callback: (scope: Scope) => void): ReturnType<Hub['withScope']> {
const safeCallback = (scope: Scope): void => {
try {
callback(scope);
NATIVE.withScope(nscope=>{
callback(scope);
});
} catch (e) {
logger.error('Error while running withScope callback', e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tracing/nstracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class NativescriptTracing implements Integration {
} = this.options;

this._getCurrentHub = getCurrentHub;

console.log('NativescriptTracing', this.options);
if (enableAppStartTracking) {
void this._instrumentAppStart();
}
Expand Down
2 changes: 2 additions & 0 deletions src/typings/ns.ios.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ declare class NSSentrySDK extends SentrySDK {
static readonly installationID: string;

static readonly isFramesTrackingRunning: boolean;
static appStartMeasurementHybridSDKMode: boolean;
static framesTrackingMeasurementHybridSDKMode: boolean;
}
Loading

0 comments on commit 7e8be4e

Please sign in to comment.