Skip to content

[IGNORE WIP] Use FP to decrease bundle size #4381

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

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7bce4cc
IGNORE
yordis Jan 11, 2022
f553fef
asd
yordis Jan 11, 2022
d7a49d7
feat: move functions out of the class
yordis Jan 11, 2022
2fbffc4
chore: move cloneScope to file context instead of being a static func…
yordis Jan 11, 2022
ac90228
chore: move more functions from the object out
yordis Jan 11, 2022
4ac12dd
chore: move more hub functions out of the prototype
yordis Jan 11, 2022
3feec27
chore: move fns outside class
yordis Jan 11, 2022
cef8a95
chore: fix compiler issues
yordis Jan 11, 2022
e2da41a
chore: move all methods from Hub class
yordis Jan 11, 2022
8558cad
chore: rename field
yordis Jan 11, 2022
482696f
chore: move declaration to body
yordis Jan 11, 2022
c25359c
chore: move functions out
yordis Jan 11, 2022
98ebe31
fix: compiler issues
yordis Jan 11, 2022
06042d0
fix: compilation issues
yordis Jan 11, 2022
f88b7fd
chore: remove Hub interface funcs
yordis Jan 11, 2022
9fc028c
Update packages/integrations/src/angular.ts
yordis Jan 11, 2022
712ebb8
chore: make scope functional
yordis Jan 12, 2022
798210a
chore: reorder code
yordis Jan 12, 2022
db6dde6
chore: fix compiler
yordis Jan 12, 2022
c417d9a
Merge branch 'testing-fp-things' of github.com:yordis/sentry-javascri…
yordis Jan 12, 2022
6c59dad
chore: fixing test cases
yordis Jan 12, 2022
0bb6089
chore: fixing tests
yordis Jan 12, 2022
648ca06
chore: make scope to pass tests
yordis Jan 13, 2022
7b72534
chore: increase test coverage
yordis Jan 13, 2022
434acc1
chore: fix tests
yordis Jan 13, 2022
581e9e7
chore: fix test cases
yordis Jan 13, 2022
07abdbe
chore: fix issues
yordis Jan 13, 2022
eff9fd4
chore: fix test
yordis Jan 13, 2022
51976cb
fix node client
yordis Jan 13, 2022
7c7bee2
chore: change session flusher api and hub functions
yordis Jan 13, 2022
121a455
fix: node client
yordis Jan 13, 2022
8bb8391
fix: minimal package
yordis Jan 13, 2022
ee258de
fix: core
yordis Jan 13, 2022
64e9d6b
fix: browser
yordis Jan 13, 2022
a9204ed
fix: react
yordis Jan 13, 2022
31c6711
fix: nextjs
yordis Jan 13, 2022
d1370e7
fix: tracing
yordis Jan 13, 2022
8929a9f
fix: compilers
yordis Jan 13, 2022
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
5 changes: 3 additions & 2 deletions packages/angular/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AfterViewInit, Directive, Injectable, Input, NgModule, OnDestroy, OnInit } from '@angular/core';
import { Event, NavigationEnd, NavigationStart, Router } from '@angular/router';
import { getCurrentHub } from '@sentry/browser';
import { getScopeTransaction, getHubScope } from '@sentry/hub';
import { Span, Transaction, TransactionContext } from '@sentry/types';
import { getGlobalObject, logger, stripUrlQueryAndFragment, timestampWithMs } from '@sentry/utils';
import { Observable, Subscription } from 'rxjs';
Expand Down Expand Up @@ -44,9 +45,9 @@ export function getActiveTransaction(): Transaction | undefined {
const currentHub = getCurrentHub();

if (currentHub) {
const scope = currentHub.getScope();
const scope = getHubScope(currentHub);
if (scope) {
return scope.getTransaction();
return getScopeTransaction(scope);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"access": "public"
},
"dependencies": {
"@sentry/hub": "6.17.0-beta.0",
"@sentry/core": "6.17.0-beta.0",
"@sentry/types": "6.17.0-beta.0",
"@sentry/utils": "6.17.0-beta.0",
Expand Down
20 changes: 13 additions & 7 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable max-lines */
import { getCurrentHub } from '@sentry/core';
import { addHubBreadcrumb } from '@sentry/hub';
import { Event, Integration } from '@sentry/types';
import {
addInstrumentationHandler,
Expand Down Expand Up @@ -62,7 +63,8 @@ export class Breadcrumbs implements Integration {
if (!this._options.sentry) {
return;
}
getCurrentHub().addBreadcrumb(
addHubBreadcrumb(
getCurrentHub(),
{
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
event_id: event.event_id,
Expand Down Expand Up @@ -130,7 +132,8 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
return;
}

getCurrentHub().addBreadcrumb(
addHubBreadcrumb(
getCurrentHub(),
{
category: `ui.${handlerData.name}`,
message: target,
Expand Down Expand Up @@ -171,7 +174,7 @@ function _consoleBreadcrumb(handlerData: { [key: string]: any }): void {
}
}

getCurrentHub().addBreadcrumb(breadcrumb, {
addHubBreadcrumb(getCurrentHub(), breadcrumb, {
input: handlerData.args,
level: handlerData.level,
});
Expand All @@ -190,7 +193,8 @@ function _xhrBreadcrumb(handlerData: { [key: string]: any }): void {

const { method, url, status_code, body } = handlerData.xhr.__sentry_xhr__ || {};

getCurrentHub().addBreadcrumb(
addHubBreadcrumb(
getCurrentHub(),
{
category: 'xhr',
data: {
Expand Down Expand Up @@ -226,7 +230,8 @@ function _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
}

if (handlerData.error) {
getCurrentHub().addBreadcrumb(
addHubBreadcrumb(
getCurrentHub(),
{
category: 'fetch',
data: handlerData.fetchData,
Expand All @@ -239,7 +244,8 @@ function _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
},
);
} else {
getCurrentHub().addBreadcrumb(
addHubBreadcrumb(
getCurrentHub(),
{
category: 'fetch',
data: {
Expand Down Expand Up @@ -282,7 +288,7 @@ function _historyBreadcrumb(handlerData: { [key: string]: any }): void {
from = parsedFrom.relative;
}

getCurrentHub().addBreadcrumb({
addHubBreadcrumb(getCurrentHub(), {
category: 'navigation',
data: {
from,
Expand Down
7 changes: 4 additions & 3 deletions packages/browser/src/integrations/dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Event, EventProcessor, Exception, Hub, Integration, StackFrame } from '@sentry/types';
import { Event, EventProcessor, Exception, Integration, StackFrame } from '@sentry/types';
import { logger } from '@sentry/utils';
import { Hub, getHubIntegration } from '@sentry/hub';

/** Deduplication filter */
export class Dedupe implements Integration {
Expand All @@ -16,14 +17,14 @@ export class Dedupe implements Integration {
/**
* @inheritDoc
*/
private _previousEvent?: Event;
public _previousEvent?: Event;

/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((currentEvent: Event) => {
const self = getCurrentHub().getIntegration(Dedupe);
const self = getHubIntegration(getCurrentHub(), Dedupe);
if (self) {
// Juuust in case something goes wrong
try {
Expand Down
11 changes: 6 additions & 5 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { getCurrentHub } from '@sentry/core';
import { Event, EventHint, Hub, Integration, Primitive } from '@sentry/types';
import { captureHubEvent, getHubClient, getHubIntegration, Hub } from '@sentry/hub';
import { Event, EventHint, Integration, Primitive } from '@sentry/types';
import {
addExceptionMechanism,
addInstrumentationHandler,
Expand Down Expand Up @@ -80,7 +81,7 @@ function _installGlobalOnErrorHandler(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(data: { msg: any; url: any; line: any; column: any; error: any }) => {
const [hub, attachStacktrace] = getHubAndAttachStacktrace();
if (!hub.getIntegration(GlobalHandlers)) {
if (!getHubIntegration(hub, GlobalHandlers)) {
return;
}
const { msg, url, line, column, error } = data;
Expand Down Expand Up @@ -113,7 +114,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(e: any) => {
const [hub, attachStacktrace] = getHubAndAttachStacktrace();
if (!hub.getIntegration(GlobalHandlers)) {
if (!getHubIntegration(hub, GlobalHandlers)) {
return;
}
let error = e;
Expand Down Expand Up @@ -250,14 +251,14 @@ function addMechanismAndCapture(hub: Hub, error: EventHint['originalException'],
handled: false,
type,
});
hub.captureEvent(event, {
captureHubEvent(hub, event, {
originalException: error,
});
}

function getHubAndAttachStacktrace(): [Hub, boolean | undefined] {
const hub = getCurrentHub();
const client = hub.getClient();
const client = getHubClient(hub);
const attachStacktrace = client && client.getOptions().attachStacktrace;
return [hub, attachStacktrace];
}
7 changes: 4 additions & 3 deletions packages/browser/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { getHubIntegration } from '@sentry/hub';
import { Event, EventHint, Exception, ExtendedError, Integration } from '@sentry/types';
import { isInstanceOf } from '@sentry/utils';

Expand Down Expand Up @@ -28,12 +29,12 @@ export class LinkedErrors implements Integration {
/**
* @inheritDoc
*/
private readonly _key: LinkedErrorsOptions['key'];
public readonly _key: LinkedErrorsOptions['key'];

/**
* @inheritDoc
*/
private readonly _limit: LinkedErrorsOptions['limit'];
public readonly _limit: LinkedErrorsOptions['limit'];

/**
* @inheritDoc
Expand All @@ -48,7 +49,7 @@ export class LinkedErrors implements Integration {
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(LinkedErrors);
const self = getHubIntegration(getCurrentHub(), LinkedErrors);
return self ? _handler(self._key, self._limit, event, hint) : event;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self is no longer this ... therefore, _key and _limit should be public ... thinking later how to continue to do more FP tuff in another part of the code base

});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/integrations/useragent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { getHubIntegration } from '@sentry/hub';
import { Event, Integration } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils';

Expand All @@ -21,7 +22,7 @@ export class UserAgent implements Integration {
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event) => {
if (getCurrentHub().getIntegration(UserAgent)) {
if (getHubIntegration(getCurrentHub(), UserAgent)) {
// if none of the information we want exists, don't bother
if (!global.navigator && !global.location && !global.document) {
return event;
Expand Down
35 changes: 22 additions & 13 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
import { Hub } from '@sentry/types';
import {
getScopeUser,
captureHubSession,
getHubClient,
getHubLastEventId,
getHubScope,
Hub,
startHubSession,
} from '@sentry/hub';
import { addInstrumentationHandler, getGlobalObject, isDebugBuild, logger, resolvedSyncPromise } from '@sentry/utils';

import { BrowserOptions } from './backend';
Expand Down Expand Up @@ -107,18 +115,18 @@ export function init(options: BrowserOptions = {}): void {
*/
export function showReportDialog(options: ReportDialogOptions = {}): void {
const hub = getCurrentHub();
const scope = hub.getScope();
const scope = getHubScope(hub);
if (scope) {
options.user = {
...scope.getUser(),
...getScopeUser(scope),
...options.user,
};
}

if (!options.eventId) {
options.eventId = hub.lastEventId();
options.eventId = getHubLastEventId(hub);
}
const client = hub.getClient<BrowserClient>();
const client = getHubClient<BrowserClient>(hub);
if (client) {
client.showReportDialog(options);
}
Expand All @@ -130,7 +138,7 @@ export function showReportDialog(options: ReportDialogOptions = {}): void {
* @returns The last event id of a captured event.
*/
export function lastEventId(): string | undefined {
return getCurrentHub().lastEventId();
return getHubLastEventId(getCurrentHub());
}

/**
Expand Down Expand Up @@ -158,7 +166,7 @@ export function onLoad(callback: () => void): void {
* doesn't (or if there's no client defined).
*/
export function flush(timeout?: number): PromiseLike<boolean> {
const client = getCurrentHub().getClient<BrowserClient>();
const client = getHubClient<BrowserClient>(getCurrentHub());
if (client) {
return client.flush(timeout);
}
Expand All @@ -177,7 +185,7 @@ export function flush(timeout?: number): PromiseLike<boolean> {
* doesn't (or if there's no client defined).
*/
export function close(timeout?: number): PromiseLike<boolean> {
const client = getCurrentHub().getClient<BrowserClient>();
const client = getHubClient<BrowserClient>(getCurrentHub());
if (client) {
return client.close(timeout);
}
Expand All @@ -200,8 +208,8 @@ export function wrap(fn: (...args: any) => any): any {
}

function startSessionOnHub(hub: Hub): void {
hub.startSession({ ignoreDuration: true });
hub.captureSession();
startHubSession(hub, { ignoreDuration: true });
captureHubSession(hub);
}

/**
Expand All @@ -226,9 +234,10 @@ function startSessionTracking(): void {
// https://github.com/getsentry/sentry-javascript/issues/3207 and
// https://github.com/getsentry/sentry-javascript/issues/3234 and
// https://github.com/getsentry/sentry-javascript/issues/3278.
if (!hub.captureSession) {
return;
}
// TODO: Follow up on this
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get it but I dont ... probably need to circle back on this one

// if (!hub.captureSession) {
// return;
// }

// The session duration for browser sessions does not track a meaningful
// concept that can be used as a metric.
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
wrap,
} from '../../src';
import { SimpleTransport } from './mocks/simpletransport';
import { getClient } from '@sentry/hub';

const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291';

Expand Down Expand Up @@ -285,7 +286,7 @@ describe('SentryBrowser initialization', () => {
},
});

const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
const sdkData = (getClient(getCurrentHub()) as any)._backend._transport._api.metadata?.sdk;

expect(sdkData.name).toBe('sentry.javascript.angular');
expect(sdkData.packages[0].name).toBe('npm:@sentry/angular');
Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-disable max-lines */
import { Scope, Session } from '@sentry/hub';
import {
applyScopeToEvent,
cloneScope,
getScopeSession,
Scope,
Session,
updateScope,
updateSession,
} from '@sentry/hub';
import {
Client,
DsnComponents,
Expand Down Expand Up @@ -186,7 +194,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
} else {
this._sendSession(session);
// After sending, we set init false to indicate it's not the first occurrence
session.update({ init: false });
updateSession(session, { init: false });
}
}

Expand Down Expand Up @@ -278,7 +286,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
const shouldUpdateAndSend = (sessionNonTerminal && session.errors === 0) || (sessionNonTerminal && crashed);

if (shouldUpdateAndSend) {
session.update({
updateSession(session, {
...(crashed && { status: 'crashed' }),
errors: session.errors || Number(errored || crashed),
});
Expand Down Expand Up @@ -360,7 +368,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
// This allows us to prevent unnecessary copying of data if `captureContext` is not provided.
let finalScope = scope;
if (hint && hint.captureContext) {
finalScope = Scope.clone(finalScope).update(hint.captureContext);
finalScope = updateScope(cloneScope(finalScope), hint.captureContext);
}

// We prepare the result here with a resolved Event.
Expand All @@ -370,7 +378,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
// {@link Hub.addEventProcessor} gets the finished prepared event.
if (finalScope) {
// In case we have a hub we reassign it.
result = finalScope.applyToEvent(prepared, hint);
result = applyScopeToEvent(finalScope, prepared, hint);
}

return result.then(evt => {
Expand Down Expand Up @@ -576,7 +584,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
throw new SentryError('`beforeSend` returned `null`, will not send event.');
}

const session = scope && scope.getSession && scope.getSession();
const session = getScopeSession(scope);
if (!isTransaction && session) {
this._updateSessionFromEvent(session, processedEvent);
}
Expand Down
Loading