Skip to content

Commit 1fb0b4c

Browse files
authored
ref(types): Loosen tag types, create new Primitive type (#3108)
* loosen tag types * use new Primitive type rather than listing primitives * use typeguard for isPrimitive and take advantage of that where possible * deal with difficult-to-seriallize primitives * remove obsolete linter directive * remove unused NonPrimitive type
1 parent dfa97e4 commit 1fb0b4c

File tree

17 files changed

+91
-44
lines changed

17 files changed

+91
-44
lines changed

MIGRATION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ like this:
8888

8989
## New Scope functions
9090

91-
We realized how annoying it is to set a whole object using `setExtra`, that's why there are now a few new methods on the
91+
We realized how annoying it is to set a whole object using `setExtra`, so there are now a few new methods on the
9292
`Scope`.
9393

9494
```typescript
95-
setTags(tags: { [key: string]: string }): this;
95+
setTags(tags: { [key: string]: string | number | boolean | null | undefined }): this;
9696
setExtras(extras: { [key: string]: any }): this;
9797
clearBreadcrumbs(): this;
9898
```

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
import { getCurrentHub } from '@sentry/core';
3-
import { Event, Integration, Severity } from '@sentry/types';
3+
import { Event, Integration, Primitive, Severity } from '@sentry/types';
44
import {
55
addExceptionMechanism,
66
addInstrumentationHandler,
@@ -152,7 +152,7 @@ export class GlobalHandlers implements Integration {
152152

153153
const client = currentHub.getClient();
154154
const event = isPrimitive(error)
155-
? this._eventFromIncompleteRejection(error)
155+
? this._eventFromRejectionWithPrimitive(error)
156156
: eventFromUnknownInput(error, undefined, {
157157
attachStacktrace: client && client.getOptions().attachStacktrace,
158158
rejection: true,
@@ -211,16 +211,19 @@ export class GlobalHandlers implements Integration {
211211
}
212212

213213
/**
214-
* This function creates an Event from an TraceKitStackTrace that has part of it missing.
214+
* Create an event from a promise rejection where the `reason` is a primitive.
215+
*
216+
* @param reason: The `reason` property of the promise rejection
217+
* @returns An Event object with an appropriate `exception` value
215218
*/
216-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
217-
private _eventFromIncompleteRejection(error: any): Event {
219+
private _eventFromRejectionWithPrimitive(reason: Primitive): Event {
218220
return {
219221
exception: {
220222
values: [
221223
{
222224
type: 'UnhandledRejection',
223-
value: `Non-Error promise rejection captured with value: ${error}`,
225+
// String() is needed because the Primitive type includes symbols (which can't be automatically stringified)
226+
value: `Non-Error promise rejection captured with value: ${String(reason)}`,
224227
},
225228
],
226229
},

packages/core/src/baseclient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
119119
let eventId: string | undefined = hint && hint.event_id;
120120

121121
const promisedEvent = isPrimitive(message)
122-
? this._getBackend().eventFromMessage(`${message}`, level, hint)
122+
? this._getBackend().eventFromMessage(String(message), level, hint)
123123
: this._getBackend().eventFromException(message, hint);
124124

125125
this._process(

packages/hub/src/hub.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Hub as HubInterface,
1212
Integration,
1313
IntegrationClass,
14+
Primitive,
1415
SessionContext,
1516
Severity,
1617
Span,
@@ -261,7 +262,7 @@ export class Hub implements HubInterface {
261262
/**
262263
* @inheritDoc
263264
*/
264-
public setTags(tags: { [key: string]: string }): void {
265+
public setTags(tags: { [key: string]: Primitive }): void {
265266
const scope = this.getScope();
266267
if (scope) scope.setTags(tags);
267268
}
@@ -277,7 +278,7 @@ export class Hub implements HubInterface {
277278
/**
278279
* @inheritDoc
279280
*/
280-
public setTag(key: string, value: string): void {
281+
public setTag(key: string, value: Primitive): void {
281282
const scope = this.getScope();
282283
if (scope) scope.setTag(key, value);
283284
}

packages/hub/src/scope.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
EventProcessor,
1010
Extra,
1111
Extras,
12+
Primitive,
1213
Scope as ScopeInterface,
1314
ScopeContext,
1415
Severity,
@@ -41,7 +42,7 @@ export class Scope implements ScopeInterface {
4142
protected _user: User = {};
4243

4344
/** Tags */
44-
protected _tags: { [key: string]: string } = {};
45+
protected _tags: { [key: string]: Primitive } = {};
4546

4647
/** Extra */
4748
protected _extra: Extras = {};
@@ -124,7 +125,7 @@ export class Scope implements ScopeInterface {
124125
/**
125126
* @inheritDoc
126127
*/
127-
public setTags(tags: { [key: string]: string }): this {
128+
public setTags(tags: { [key: string]: Primitive }): this {
128129
this._tags = {
129130
...this._tags,
130131
...tags,
@@ -136,7 +137,7 @@ export class Scope implements ScopeInterface {
136137
/**
137138
* @inheritDoc
138139
*/
139-
public setTag(key: string, value: string): this {
140+
public setTag(key: string, value: Primitive): this {
140141
this._tags = { ...this._tags, [key]: value };
141142
this._notifyScopeListeners();
142143
return this;

packages/minimal/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Event,
77
Extra,
88
Extras,
9+
Primitive,
910
Severity,
1011
Transaction,
1112
TransactionContext,
@@ -127,7 +128,7 @@ export function setExtras(extras: Extras): void {
127128
* Set an object that will be merged sent as tags data with the event.
128129
* @param tags Tags context object to merge into current context.
129130
*/
130-
export function setTags(tags: { [key: string]: string }): void {
131+
export function setTags(tags: { [key: string]: Primitive }): void {
131132
callOnHub<void>('setTags', tags);
132133
}
133134

@@ -142,10 +143,13 @@ export function setExtra(key: string, extra: Extra): void {
142143

143144
/**
144145
* Set key:value that will be sent as tags data with the event.
146+
*
147+
* Can also be used to unset a tag, by passing `undefined`.
148+
*
145149
* @param key String key of tag
146-
* @param value String value of tag
150+
* @param value Value of tag
147151
*/
148-
export function setTag(key: string, value: string): void {
152+
export function setTag(key: string, value: Primitive): void {
149153
callOnHub<void>('setTag', key, value);
150154
}
151155

packages/react/src/reactrouterv3.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Transaction, TransactionContext } from '@sentry/types';
1+
import { Primitive, Transaction, TransactionContext } from '@sentry/types';
22
import { getGlobalObject } from '@sentry/utils';
33

44
import { Location, ReactRouterInstrumentation } from './types';
@@ -62,7 +62,9 @@ export function reactRouterV3Instrumentation(
6262
if (activeTransaction) {
6363
activeTransaction.finish();
6464
}
65-
const tags: Record<string, string> = { 'routing.instrumentation': 'react-router-v3' };
65+
const tags: Record<string, Primitive> = {
66+
'routing.instrumentation': 'react-router-v3',
67+
};
6668
if (prevName) {
6769
tags.from = prevName;
6870
}

packages/tracing/src/span.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-lines */
2-
import { Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
2+
import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
33
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
44

55
import { SpanStatus } from './spanstatus';
@@ -86,7 +86,7 @@ export class Span implements SpanInterface {
8686
/**
8787
* @inheritDoc
8888
*/
89-
public tags: { [key: string]: string } = {};
89+
public tags: { [key: string]: Primitive } = {};
9090

9191
/**
9292
* @inheritDoc
@@ -187,7 +187,7 @@ export class Span implements SpanInterface {
187187
/**
188188
* @inheritDoc
189189
*/
190-
public setTag(key: string, value: string): this {
190+
public setTag(key: string, value: Primitive): this {
191191
this.tags = { ...this.tags, [key]: value };
192192
return this;
193193
}
@@ -257,7 +257,7 @@ export class Span implements SpanInterface {
257257
parent_span_id?: string;
258258
span_id: string;
259259
status?: string;
260-
tags?: { [key: string]: string };
260+
tags?: { [key: string]: Primitive };
261261
trace_id: string;
262262
} {
263263
return dropUndefinedKeys({
@@ -284,7 +284,7 @@ export class Span implements SpanInterface {
284284
span_id: string;
285285
start_timestamp: number;
286286
status?: string;
287-
tags?: { [key: string]: string };
287+
tags?: { [key: string]: Primitive };
288288
timestamp?: number;
289289
trace_id: string;
290290
} {

packages/types/src/event.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Breadcrumb } from './breadcrumb';
22
import { Contexts } from './context';
33
import { Exception } from './exception';
44
import { Extras } from './extra';
5+
import { Primitive } from './misc';
56
import { Request } from './request';
67
import { CaptureContext } from './scope';
78
import { SdkInfo } from './sdkinfo';
@@ -35,7 +36,7 @@ export interface Event {
3536
stacktrace?: Stacktrace;
3637
breadcrumbs?: Breadcrumb[];
3738
contexts?: Contexts;
38-
tags?: { [key: string]: string };
39+
tags?: { [key: string]: Primitive };
3940
extra?: Extras;
4041
user?: User;
4142
type?: EventType;

packages/types/src/hub.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client } from './client';
33
import { Event, EventHint } from './event';
44
import { Extra, Extras } from './extra';
55
import { Integration, IntegrationClass } from './integration';
6+
import { Primitive } from './misc';
67
import { Scope } from './scope';
78
import { Session, SessionContext } from './session';
89
import { Severity } from './severity';
@@ -124,16 +125,20 @@ export interface Hub {
124125

125126
/**
126127
* Set an object that will be merged sent as tags data with the event.
128+
*
127129
* @param tags Tags context object to merge into current context.
128130
*/
129-
setTags(tags: { [key: string]: string }): void;
131+
setTags(tags: { [key: string]: Primitive }): void;
130132

131133
/**
132134
* Set key:value that will be sent as tags data with the event.
135+
*
136+
* Can also be used to unset a tag, by passing `undefined`.
137+
*
133138
* @param key String key of tag
134-
* @param value String value of tag
139+
* @param value Value of tag
135140
*/
136-
setTag(key: string, value: string): void;
141+
setTag(key: string, value: Primitive): void;
137142

138143
/**
139144
* Set key:value that will be sent as extra data with the event.

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { Hub } from './hub';
1111
export { Integration, IntegrationClass } from './integration';
1212
export { LogLevel } from './loglevel';
1313
export { Mechanism } from './mechanism';
14-
export { ExtractedNodeRequestData, WorkerLocation } from './misc';
14+
export { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
1515
export { Options } from './options';
1616
export { Package } from './package';
1717
export { Request, SentryRequest, SentryRequestType } from './request';

packages/types/src/misc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ export interface WorkerLocation {
5959
/** Synonym for `href` attribute */
6060
toString(): string;
6161
}
62+
63+
export type Primitive = number | string | boolean | bigint | symbol | null | undefined;

packages/types/src/scope.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Breadcrumb } from './breadcrumb';
22
import { Context, Contexts } from './context';
33
import { EventProcessor } from './eventprocessor';
44
import { Extra, Extras } from './extra';
5+
import { Primitive } from './misc';
56
import { Session } from './session';
67
import { Severity } from './severity';
78
import { Span } from './span';
@@ -17,7 +18,7 @@ export interface ScopeContext {
1718
level: Severity;
1819
extra: Extras;
1920
contexts: Contexts;
20-
tags: { [key: string]: string };
21+
tags: { [key: string]: Primitive };
2122
fingerprint: string[];
2223
}
2324

@@ -45,14 +46,17 @@ export interface Scope {
4546
* Set an object that will be merged sent as tags data with the event.
4647
* @param tags Tags context object to merge into current context.
4748
*/
48-
setTags(tags: { [key: string]: string }): this;
49+
setTags(tags: { [key: string]: Primitive }): this;
4950

5051
/**
5152
* Set key:value that will be sent as tags data with the event.
53+
*
54+
* Can also be used to unset a tag by passing `undefined`.
55+
*
5256
* @param key String key of tag
53-
* @param value String value of tag
57+
* @param value Value of tag
5458
*/
55-
setTag(key: string, value: string): this;
59+
setTag(key: string, value: Primitive): this;
5660

5761
/**
5862
* Set an object that will be merged sent as extra data with the event.

packages/types/src/span.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Primitive } from './misc';
12
import { Transaction } from './transaction';
23

34
/** Interface holding all properties that can be set on a Span on creation. */
@@ -41,7 +42,7 @@ export interface SpanContext {
4142
/**
4243
* Tags of the Span.
4344
*/
44-
tags?: { [key: string]: string };
45+
tags?: { [key: string]: Primitive };
4546

4647
/**
4748
* Data of the Span.
@@ -79,7 +80,7 @@ export interface Span extends SpanContext {
7980
/**
8081
* @inheritDoc
8182
*/
82-
tags: { [key: string]: string };
83+
tags: { [key: string]: Primitive };
8384

8485
/**
8586
* @inheritDoc
@@ -98,11 +99,14 @@ export interface Span extends SpanContext {
9899
finish(endTimestamp?: number): void;
99100

100101
/**
101-
* Sets the tag attribute on the current span
102+
* Sets the tag attribute on the current span.
103+
*
104+
* Can also be used to unset a tag, by passing `undefined`.
105+
*
102106
* @param key Tag key
103107
* @param value Tag value
104108
*/
105-
setTag(key: string, value: string): this;
109+
setTag(key: string, value: Primitive): this;
106110

107111
/**
108112
* Sets the data attribute on the current span
@@ -156,7 +160,7 @@ export interface Span extends SpanContext {
156160
parent_span_id?: string;
157161
span_id: string;
158162
status?: string;
159-
tags?: { [key: string]: string };
163+
tags?: { [key: string]: Primitive };
160164
trace_id: string;
161165
};
162166
/** Convert the object to JSON */
@@ -168,7 +172,7 @@ export interface Span extends SpanContext {
168172
span_id: string;
169173
start_timestamp: number;
170174
status?: string;
171-
tags?: { [key: string]: string };
175+
tags?: { [key: string]: Primitive };
172176
timestamp?: number;
173177
trace_id: string;
174178
};

packages/types/src/transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExtractedNodeRequestData, WorkerLocation } from './misc';
1+
import { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
22
import { Span, SpanContext } from './span';
33

44
/**
@@ -51,7 +51,7 @@ export interface Transaction extends TransactionContext, Span {
5151
/**
5252
* @inheritDoc
5353
*/
54-
tags: { [key: string]: string };
54+
tags: { [key: string]: Primitive };
5555

5656
/**
5757
* @inheritDoc

0 commit comments

Comments
 (0)