Skip to content

Commit b76b28d

Browse files
authored
feat: Expose prio scope functions (getsentry#2092)
* feat: Expose prio scope functions * feat: setExtra + tests * Revert "feat: setExtra + tests" This reverts commit 74f44f9. * feat: Tests * feat: Expose setExtra, setTag * fix: Tests * fix: TypeDoc * ci: Typedocs try node 10
1 parent dc35419 commit b76b28d

File tree

10 files changed

+245
-10
lines changed

10 files changed

+245
-10
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
script: scripts/raven-js.sh
7171
- stage: Deploy
7272
name: '@sentry/packages - pack and zeus upload'
73-
node_js: '8'
73+
node_js: '10'
7474
script: scripts/pack-and-upload.sh || [[ ! "$TRAVIS_BRANCH" =~ ^release/ ]]
7575

7676
notifications:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- [global] feat: Exposed new simplified scope API. `Sentry.setTag`, `Sentry.setTags`, `Sentry.setExtra`, `Sentry.setExtras`, `Sentry.setUser`, `Sentry.setContext`
6+
37
## 5.3.1
48

59
- [integrations] fix: Tracing integration CDN build.

packages/browser/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ export {
2020
captureEvent,
2121
captureMessage,
2222
configureScope,
23-
withScope,
2423
getHubFromCarrier,
2524
getCurrentHub,
2625
Hub,
2726
Scope,
27+
setContext,
28+
setExtra,
29+
setExtras,
30+
setTag,
31+
setTags,
32+
setUser,
33+
withScope,
2834
} from '@sentry/core';
2935

3036
export { BrowserOptions } from './backend';

packages/core/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ export {
44
captureEvent,
55
captureMessage,
66
configureScope,
7+
setContext,
8+
setExtra,
9+
setExtras,
10+
setTag,
11+
setTags,
12+
setUser,
713
withScope,
814
} from '@sentry/minimal';
915
export { addGlobalEventProcessor, getCurrentHub, Hub, getHubFromCarrier, Scope } from '@sentry/hub';

packages/hub/src/hub.ts

+67-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Integration,
99
IntegrationClass,
1010
Severity,
11+
User,
1112
} from '@sentry/types';
1213
import { consoleSandbox, dynamicRequire, getGlobalObject, logger, uuid4 } from '@sentry/utils';
1314

@@ -225,13 +226,78 @@ export class Hub implements HubInterface {
225226
top.scope.addBreadcrumb(finalBreadcrumb, Math.min(maxBreadcrumbs, MAX_BREADCRUMBS));
226227
}
227228

229+
/**
230+
* @inheritDoc
231+
*/
232+
public setUser(user: User | null): void {
233+
const top = this.getStackTop();
234+
if (!top.scope) {
235+
return;
236+
}
237+
top.scope.setUser(user);
238+
}
239+
240+
/**
241+
* @inheritDoc
242+
*/
243+
public setTags(tags: { [key: string]: string }): void {
244+
const top = this.getStackTop();
245+
if (!top.scope) {
246+
return;
247+
}
248+
top.scope.setTags(tags);
249+
}
250+
251+
/**
252+
* @inheritDoc
253+
*/
254+
public setExtras(extras: { [key: string]: any }): void {
255+
const top = this.getStackTop();
256+
if (!top.scope) {
257+
return;
258+
}
259+
top.scope.setExtras(extras);
260+
}
261+
262+
/**
263+
* @inheritDoc
264+
*/
265+
public setTag(key: string, value: string): void {
266+
const top = this.getStackTop();
267+
if (!top.scope) {
268+
return;
269+
}
270+
top.scope.setTag(key, value);
271+
}
272+
273+
/**
274+
* @inheritDoc
275+
*/
276+
public setExtra(key: string, extra: any): void {
277+
const top = this.getStackTop();
278+
if (!top.scope) {
279+
return;
280+
}
281+
top.scope.setExtra(key, extra);
282+
}
283+
284+
/**
285+
* @inheritDoc
286+
*/
287+
public setContext(name: string, context: { [key: string]: any } | null): void {
288+
const top = this.getStackTop();
289+
if (!top.scope) {
290+
return;
291+
}
292+
top.scope.setContext(name, context);
293+
}
294+
228295
/**
229296
* @inheritDoc
230297
*/
231298
public configureScope(callback: (scope: Scope) => void): void {
232299
const top = this.getStackTop();
233300
if (top.scope && top.client) {
234-
// TODO: freeze flag
235301
callback(top.scope);
236302
}
237303
}

packages/minimal/src/index.ts

+58-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub, Hub, Scope } from '@sentry/hub';
2-
import { Breadcrumb, Event, Severity } from '@sentry/types';
2+
import { Breadcrumb, Event, Severity, User } from '@sentry/types';
33

44
/**
55
* This calls a function on the current hub.
@@ -64,6 +64,14 @@ export function captureEvent(event: Event): string {
6464
return callOnHub('captureEvent', event);
6565
}
6666

67+
/**
68+
* Callback to set context information onto the scope.
69+
* @param callback Callback function that receives Scope.
70+
*/
71+
export function configureScope(callback: (scope: Scope) => void): void {
72+
callOnHub<void>('configureScope', callback);
73+
}
74+
6775
/**
6876
* Records a new breadcrumb which will be attached to future events.
6977
*
@@ -77,11 +85,56 @@ export function addBreadcrumb(breadcrumb: Breadcrumb): void {
7785
}
7886

7987
/**
80-
* Callback to set context information onto the scope.
81-
* @param callback Callback function that receives Scope.
88+
* Sets context data with the given name.
89+
* @param name of the context
90+
* @param context Any kind of data. This data will be normailzed.
8291
*/
83-
export function configureScope(callback: (scope: Scope) => void): void {
84-
callOnHub<void>('configureScope', callback);
92+
export function setContext(name: string, context: { [key: string]: any } | null): void {
93+
callOnHub<void>('setContext', name, context);
94+
}
95+
96+
/**
97+
* Set an object that will be merged sent as extra data with the event.
98+
* @param extras Extras object to merge into current context.
99+
*/
100+
export function setExtras(extras: { [key: string]: any }): void {
101+
callOnHub<void>('setExtras', extras);
102+
}
103+
104+
/**
105+
* Set an object that will be merged sent as tags data with the event.
106+
* @param tags Tags context object to merge into current context.
107+
*/
108+
export function setTags(tags: { [key: string]: string }): void {
109+
callOnHub<void>('setTags', tags);
110+
}
111+
112+
/**
113+
* Set key:value that will be sent as extra data with the event.
114+
* @param key String of extra
115+
* @param extra Any kind of data. This data will be normailzed.
116+
*/
117+
118+
export function setExtra(key: string, extra: any): void {
119+
callOnHub<void>('setExtra', key, extra);
120+
}
121+
122+
/**
123+
* Set key:value that will be sent as tags data with the event.
124+
* @param key String key of tag
125+
* @param value String value of tag
126+
*/
127+
export function setTag(key: string, value: string): void {
128+
callOnHub<void>('setTag', key, value);
129+
}
130+
131+
/**
132+
* Updates user context information for future events.
133+
*
134+
* @param user User context object to be set in the current context. Pass `null` to unset the user.
135+
*/
136+
export function setUser(user: User | null): void {
137+
callOnHub<void>('setUser', user);
85138
}
86139

87140
/**

packages/minimal/test/lib/minimal.test.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import { getCurrentHub, getHubFromCarrier, Scope } from '@sentry/hub';
22
import { Severity } from '@sentry/types';
3-
import { _callOnClient, captureEvent, captureException, captureMessage, configureScope, withScope } from '../../src';
3+
4+
import {
5+
_callOnClient,
6+
captureEvent,
7+
captureException,
8+
captureMessage,
9+
configureScope,
10+
setContext,
11+
setExtra,
12+
setExtras,
13+
setTag,
14+
setTags,
15+
setUser,
16+
withScope,
17+
} from '../../src';
418
import { init, TestClient, TestClient2 } from '../mocks/client';
519

620
declare var global: any;
@@ -208,4 +222,42 @@ describe('Minimal', () => {
208222
});
209223
expect(global.__SENTRY__.hub._stack).toHaveLength(1);
210224
});
225+
226+
test('setExtras', () => {
227+
init({});
228+
setExtras({ a: 'b' });
229+
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' });
230+
});
231+
232+
test('setTags', () => {
233+
init({});
234+
setTags({ a: 'b' });
235+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' });
236+
});
237+
238+
test('setExtra', () => {
239+
init({});
240+
setExtra('a', 'b');
241+
// prettier-ignore
242+
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' });
243+
});
244+
245+
test('setTag', () => {
246+
init({});
247+
setTag('a', 'b');
248+
// prettier-ignore
249+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' });
250+
});
251+
252+
test('setUser', () => {
253+
init({});
254+
setUser({ id: 'b' });
255+
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' });
256+
});
257+
258+
test('setContext', () => {
259+
init({});
260+
setContext('test', { id: 'b' });
261+
expect(global.__SENTRY__.hub._stack[0].scope._context).toEqual({ test: { id: 'b' } });
262+
});
211263
});

packages/node/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export {
2424
getHubFromCarrier,
2525
Hub,
2626
Scope,
27+
setContext,
28+
setExtra,
29+
setExtras,
30+
setTag,
31+
setTags,
32+
setUser,
2733
withScope,
2834
} from '@sentry/core';
2935

packages/types/src/hub.ts

+42
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Event, EventHint } from './event';
44
import { Integration, IntegrationClass } from './integration';
55
import { Scope } from './scope';
66
import { Severity } from './severity';
7+
import { User } from './user';
78

89
/**
910
* Internal class used to make sure we always have the latest internal functions
@@ -109,6 +110,47 @@ export interface Hub {
109110
* @param hint May contain additional information about the original breadcrumb.
110111
*/
111112
addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): void;
113+
114+
/**
115+
* Updates user context information for future events.
116+
*
117+
* @param user User context object to be set in the current context. Pass `null` to unset the user.
118+
*/
119+
setUser(user: User | null): void;
120+
121+
/**
122+
* Set an object that will be merged sent as tags data with the event.
123+
* @param tags Tags context object to merge into current context.
124+
*/
125+
setTags(tags: { [key: string]: string }): void;
126+
127+
/**
128+
* Set key:value that will be sent as tags data with the event.
129+
* @param key String key of tag
130+
* @param value String value of tag
131+
*/
132+
setTag(key: string, value: string): void;
133+
134+
/**
135+
* Set key:value that will be sent as extra data with the event.
136+
* @param key String of extra
137+
* @param extra Any kind of data. This data will be normailzed.
138+
*/
139+
setExtra(key: string, extra: any): void;
140+
141+
/**
142+
* Set an object that will be merged sent as extra data with the event.
143+
* @param extras Extras object to merge into current context.
144+
*/
145+
setExtras(extras: { [key: string]: any }): void;
146+
147+
/**
148+
* Sets context data with the given name.
149+
* @param name of the context
150+
* @param context Any kind of data. This data will be normailzed.
151+
*/
152+
setContext(name: string, context: { [key: string]: any } | null): void;
153+
112154
/**
113155
* Callback to set context information onto the scope.
114156
*

typedoc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ module.exports = {
1919
excludeExternals: true,
2020
excludeNotExported: true,
2121
excludePrivate: true,
22-
'external-modulemap': '.*packages/([^/]+)/.*',
22+
'external-modulemap': '.*/packages/([^/]+)/.*',
2323
};

0 commit comments

Comments
 (0)