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

implement outcome to Capacitor Client #574

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 10 additions & 11 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eventFromException, eventFromMessage } from '@sentry/browser';
import { createUserFeedbackEnvelope, eventFromException, eventFromMessage } from '@sentry/browser';
import { BaseClient } from '@sentry/core';
import type {
Envelope,
Expand All @@ -12,7 +12,9 @@ import type {
} from '@sentry/types';
import { logger, SentryError } from '@sentry/utils';

import { defaultSdkInfo } from './integrations/sdkinfo';
import type { CapacitorClientOptions } from './options';
import { mergeOutcomes } from './utils/outcome';
import { NATIVE } from './wrapper';

/**
Expand All @@ -30,8 +32,7 @@ export class CapacitorClient extends BaseClient<CapacitorClientOptions> {
*/
public constructor(options: CapacitorClientOptions) {
options._metadata = options._metadata || {};
// TODO: Implement defaultSdkInfo.
// options._metadata.sdk = options._metadata.sdk; || defaultSdkInfo;
options._metadata.sdk = options._metadata.sdk || defaultSdkInfo;
super(options);

this._outcomesBuffer = [];
Expand Down Expand Up @@ -95,8 +96,12 @@ export class CapacitorClient extends BaseClient<CapacitorClientOptions> {
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public captureUserFeedback(feedback: UserFeedback): void {
// TODO: Implement capture user feedback
throw new Error(`${feedback} captureUserFeedback not implemented.`);
const envelope = createUserFeedbackEnvelope(feedback, {
metadata: this._options._metadata,
dsn: this.getDsn(),
tunnel: this._options.tunnel,
});
this._sendEnvelope(envelope);
}

/**
Expand Down Expand Up @@ -165,9 +170,3 @@ export class CapacitorClient extends BaseClient<CapacitorClientOptions> {

// TODO: implement Attaches clients report.
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function mergeOutcomes(_outcomesBuffer: Outcome[], outcomes: Outcome[]): Outcome[] {
// TODO: Implement mergeOutComes.
throw new Error('Function not implemented.');
}

24 changes: 19 additions & 5 deletions src/integrations/sdkinfo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import type { EventProcessor, Integration, Package } from '@sentry/types';
import type { EventProcessor, Integration, Package, SdkInfo as SdkInfoType } from '@sentry/types';
import { logger } from '@sentry/utils';

import { SDK_NAME, SDK_VERSION } from '../version';
import { SDK_NAME, SDK_PACKAGE_NAME, SDK_VERSION } from '../version';
import { NATIVE } from '../wrapper';

type DefaultSdkInfo = Pick<Required<SdkInfoType>, 'name' | 'packages' | 'version'>;

export const defaultSdkInfo: DefaultSdkInfo = {
name: SDK_NAME,
packages: [
{
name: SDK_PACKAGE_NAME,
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};


/** Default SdkInfo instrumentation */
export class SdkInfo implements Integration {
/**
Expand Down Expand Up @@ -38,14 +52,14 @@ export class SdkInfo implements Integration {

event.platform = event.platform || 'javascript';
event.sdk = event.sdk || {};
event.sdk.name = event.sdk.name || SDK_NAME;
event.sdk.version = event.sdk.version || SDK_VERSION;
event.sdk.name = event.sdk.name || defaultSdkInfo.name;;
event.sdk.version = event.sdk.version || defaultSdkInfo.version;
event.sdk.packages = [
// default packages are added by baseclient and should not be added here
...(event.sdk.packages || []),
...((this._nativeSdkPackage && [this._nativeSdkPackage]) || []),
{
name: 'npm:@sentry/capacitor',
name: SDK_PACKAGE_NAME,
version: SDK_VERSION,
},
];
Expand Down
24 changes: 24 additions & 0 deletions src/utils/outcome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Outcome } from '@sentry/types';

/**
* Merges buffer with new outcomes.
*/
export function mergeOutcomes(...merge: Outcome[][]): Outcome[] {
let counter = 0;
const map = new Map<string, number>();
const outcomes: Outcome[] = [];

const process = (outcome: Outcome): void => {
const key = `${outcome.reason}:${outcome.category}`;
const index = map.get(key);
if (typeof(index) !== "undefined") {
outcomes[index].quantity += outcome.quantity;
} else {
map.set(key, counter++);
outcomes.push(outcome);
}
};

merge.forEach(outcomes => outcomes.forEach(process));
return outcomes;
}
13 changes: 5 additions & 8 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Envelope, Transport } from '@sentry/types';
import { CapacitorClient } from '../src/client';
import type { CapacitorClientOptions } from '../src/options';
import { NativeTransport } from '../src/transports/native';
import { SDK_NAME, SDK_PACKAGE_NAME, SDK_VERSION } from '../src/version';
import { NATIVE } from '../src/wrapper';

interface MockedCapacitor {
Expand Down Expand Up @@ -69,7 +70,7 @@ jest.mock('../src/plugin', () => {
});

import * as Plugin from '../src/plugin';

import { envelopeHeader, envelopeItemHeader, envelopeItemPayload, envelopeItems, firstArg, getMockSession, getMockUserFeedback } from './testutils';

const EXAMPLE_DSN = 'https://6890c2f6677340daa4804f8194804ea2@o19635.ingest.sentry.io/148053';

Expand Down Expand Up @@ -209,7 +210,6 @@ describe('Tests CapacitorClient', () => {
});
});

/* TODO: To be implemented
describe('UserFeedback', () => {
test('sends UserFeedback to native Layer', () => {
const mockTransportSend: jest.Mock = jest.fn(() => Promise.resolve());
Expand Down Expand Up @@ -241,10 +241,7 @@ describe('Tests CapacitorClient', () => {
});
});
});
*/

/*
TODO: FIX SdkInfo
describe('envelopeHeader SdkInfo', () => {
let mockTransportSend: jest.Mock;
let client: CapacitorClient;
Expand Down Expand Up @@ -293,8 +290,7 @@ describe('Tests CapacitorClient', () => {
expect(getSdkInfoFrom(mockTransportSend)).toStrictEqual(expectedSdkInfo);
});
});
*/
/* TODO: Fix SDKInfo

describe('event data enhancement', () => {
test('event contains sdk default information', async () => {
const mockedSend = jest.fn<PromiseLike<void>, [Envelope]>().mockResolvedValue(undefined);
Expand All @@ -314,6 +310,7 @@ describe('Tests CapacitorClient', () => {
const actualEvent: Event | undefined = <Event>(
mockedSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload]
);
// @ts-ignore SDK is not inside the event by default.
expect(actualEvent?.sdk?.packages).toEqual([
{
name: SDK_PACKAGE_NAME,
Expand All @@ -323,7 +320,7 @@ describe('Tests CapacitorClient', () => {
});

});
*/

describe('normalizes events', () => {
/* TODO: Fix later
test('handles circular input', async () => {
Expand Down
129 changes: 129 additions & 0 deletions test/utils/outcome.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import type { Outcome } from '@sentry/types';

import { mergeOutcomes } from '../../src/utils/outcome';

describe('mergeOutcomes', () => {
test('merge same outcomes into one incrementing the quantity', () => {
const outcome1: Outcome[] =
[{
reason: 'before_send',
category: 'error',
quantity: 1,
}];
const outcome2: Outcome[] =
[{
reason: 'before_send',
category: 'error',
quantity: 1,
}];
const expectedOutcome: Outcome[] = [{
reason: 'before_send',
category: 'error',
quantity: 2,
}];
const finalOutcomes = mergeOutcomes(outcome1, outcome2);

expect(finalOutcomes).toStrictEqual(expectedOutcome);
});

test('merge different outcomes into separated outcomes', () => {
const outcome1: Outcome[] =
[{
reason: 'before_send',
category: 'error',
quantity: 1,
}];
const outcome2: Outcome[] =
[{
reason: 'event_processor',
category: 'error',
quantity: 1,
}];
const expectedOutcome: Outcome[] = [{
reason: 'before_send',
category: 'error',
quantity: 1,
},
{
reason: 'event_processor',
category: 'error',
quantity: 1,
}];
const finalOutcomes = mergeOutcomes(outcome1, outcome2);

expect(finalOutcomes).toStrictEqual(expectedOutcome);
});

test('merge outcomes when first outcome is empty', () => {
const outcome1: Outcome[] = [];
const outcome2: Outcome[] =
[{
reason: 'before_send',
category: 'error',
quantity: 1,
}];
const expectedOutcome: Outcome[] = [{
reason: 'before_send',
category: 'error',
quantity: 1,
}];
const finalOutcomes = mergeOutcomes(outcome1, outcome2);

expect(finalOutcomes).toStrictEqual(expectedOutcome);
});

test('merge outcomes when second outcome is empty', () => {
const outcome1: Outcome[] =
[{
reason: 'event_processor',
category: 'error',
quantity: 1,
}];
const expectedOutcome: Outcome[] = [{
reason: 'event_processor',
category: 'error',
quantity: 1,
}];
const outcome2: Outcome[] = [];
const finalOutcomes = mergeOutcomes(outcome1, outcome2);

expect(finalOutcomes).toStrictEqual(expectedOutcome);
});

test('empty outcomes return an array of empty outcomes', () => {
const outcome1: Outcome[] = [];
const expectedOutcome: Outcome[] = [];
const outcome2: Outcome[] = [];
const finalOutcomes = mergeOutcomes(outcome1, outcome2);

expect(finalOutcomes).toStrictEqual(expectedOutcome);
});

test('same outocmes but different category into separated outcomes', () => {
const outcome1: Outcome[] =
[{
reason: 'before_send',
category: 'error',
quantity: 1,
}];
const outcome2: Outcome[] =
[{
reason: 'before_send',
category: 'default',
quantity: 1,
}];
const expectedOutcome: Outcome[] = [{
reason: 'before_send',
category: 'error',
quantity: 1,
},
{
reason: 'before_send',
category: 'default',
quantity: 1,
}];
const finalOutcomes = mergeOutcomes(outcome1, outcome2);

expect(finalOutcomes).toStrictEqual(expectedOutcome);
});
});