Skip to content

Commit cf365cf

Browse files
HazATkamilogorek
authored andcommitted
feat: Add maxValueLength option (#1928)
* feat: Add maxValueLength option * meta: Add changelog
1 parent 24d1833 commit cf365cf

File tree

7 files changed

+45
-36
lines changed

7 files changed

+45
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ since we removed some methods from the public API and removed some classes from
3030
- **breaking** [utils] ref: Rename `safeNormalize` to `normalize` and rewrite its internals
3131
- **breaking** [utils] ref: Remove `serialize`, `deserialize`, `clone` and `serializeObject` functions
3232
- **breaking** [utils] ref: Rewrite normalization functions by removing most of them and leaving just `normalize` and `normalizeToSize`
33+
- [core] feat: Add `maxValueLength` option to adjust max string length for values, default is 250.
3334

3435
## 4.6.4
3536

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export class GlobalHandlers implements Integration {
114114
},
115115
};
116116

117-
const fallbackValue = stacktrace.original ? truncate(JSON.stringify(normalize(stacktrace.original)), 300) : '';
117+
const client = getCurrentHub().getClient();
118+
const maxValueLength = (client && client.getOptions().maxValueLength) || 250;
119+
120+
const fallbackValue = stacktrace.original
121+
? truncate(JSON.stringify(normalize(stacktrace.original)), maxValueLength)
122+
: '';
118123
const fallbackType = stacktrace.mechanism === 'onunhandledrejection' ? 'UnhandledRejection' : 'Error';
119124

120125
// This makes sure we have type/value in every exception

packages/core/src/baseclient.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import { Backend, BackendClass } from './basebackend';
99
import { Dsn } from './dsn';
1010
import { IntegrationIndex, setupIntegrations } from './integration';
1111

12-
/**
13-
* By default, truncates URL values to 250 chars
14-
*/
15-
const MAX_URL_LENGTH = 250;
16-
1712
/**
1813
* Base implementation for all JavaScript SDK clients.
1914
*
@@ -178,7 +173,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
178173
* @returns A new event with more information.
179174
*/
180175
protected prepareEvent(event: Event, scope?: Scope, hint?: EventHint): SyncPromise<Event | null> {
181-
const { environment, release, dist } = this.getOptions();
176+
const { environment, release, dist, maxValueLength = 250 } = this.getOptions();
182177

183178
const prepared: Event = { ...event };
184179
if (prepared.environment === undefined && environment !== undefined) {
@@ -193,17 +188,17 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
193188
}
194189

195190
if (prepared.message) {
196-
prepared.message = truncate(prepared.message, MAX_URL_LENGTH);
191+
prepared.message = truncate(prepared.message, maxValueLength);
197192
}
198193

199194
const exception = prepared.exception && prepared.exception.values && prepared.exception.values[0];
200195
if (exception && exception.value) {
201-
exception.value = truncate(exception.value, MAX_URL_LENGTH);
196+
exception.value = truncate(exception.value, maxValueLength);
202197
}
203198

204199
const request = prepared.request;
205200
if (request && request.url) {
206-
request.url = truncate(request.url, MAX_URL_LENGTH);
201+
request.url = truncate(request.url, maxValueLength);
207202
}
208203

209204
if (prepared.event_id === undefined) {

packages/hub/src/hub.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class Hub {
158158
}
159159

160160
/** Returns the client of the top stack. */
161-
public getClient(): any | undefined {
161+
public getClient(): Client | undefined {
162162
return this.getStackTop().client;
163163
}
164164

@@ -298,8 +298,12 @@ export class Hub {
298298

299299
/** Returns the integration if installed on the current client. */
300300
public getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null {
301+
const client = this.getClient();
302+
if (!client) {
303+
return null;
304+
}
301305
try {
302-
return this.getClient().getIntegration(integration);
306+
return client.getIntegration(integration);
303307
} catch (_oO) {
304308
logger.warn(`Cannot retrieve integration ${integration.id} from the current Hub`);
305309
return null;

packages/node/src/integrations/http.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,31 @@ function emitWrapper(origEmit: EventListener): (event: string, response: http.Se
138138
return origEmit.apply(this, arguments);
139139
}
140140

141-
const dsn = getCurrentHub()
142-
.getClient()
143-
.getDsn();
144-
145-
const isInterestingEvent = event === 'response' || event === 'error';
146-
const isNotSentryRequest = dsn && this.__ravenBreadcrumbUrl && !this.__ravenBreadcrumbUrl.includes(dsn.host);
147-
148-
if (isInterestingEvent && isNotSentryRequest && getCurrentHub().getIntegration(Http)) {
149-
getCurrentHub().addBreadcrumb(
150-
{
151-
category: 'http',
152-
data: {
153-
method: this.method,
154-
status_code: response.statusCode,
155-
url: this.__ravenBreadcrumbUrl,
141+
const client = getCurrentHub().getClient();
142+
if (client) {
143+
const dsn = client.getDsn();
144+
145+
const isInterestingEvent = event === 'response' || event === 'error';
146+
const isNotSentryRequest = dsn && this.__ravenBreadcrumbUrl && !this.__ravenBreadcrumbUrl.includes(dsn.host);
147+
148+
if (isInterestingEvent && isNotSentryRequest && getCurrentHub().getIntegration(Http)) {
149+
getCurrentHub().addBreadcrumb(
150+
{
151+
category: 'http',
152+
data: {
153+
method: this.method,
154+
status_code: response.statusCode,
155+
url: this.__ravenBreadcrumbUrl,
156+
},
157+
type: 'http',
156158
},
157-
type: 'http',
158-
},
159-
{
160-
event,
161-
request: this,
162-
response,
163-
},
164-
);
159+
{
160+
event,
161+
request: this,
162+
response,
163+
},
164+
);
165+
}
165166
}
166167

167168
return origEmit.apply(this, arguments);

packages/types/src/dsn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface DsnComponents {
2323
export type DsnLike = string | DsnComponents;
2424

2525
/** The Sentry Dsn, identifying a Sentry instance and project. */
26-
export interface Dsn {
26+
export interface Dsn extends DsnComponents {
2727
/**
2828
* Renders the string representation of this Dsn.
2929
*

packages/types/src/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export interface Options {
7878
/** Attaches stacktraces to pure capture message / log integrations */
7979
attachStacktrace?: boolean;
8080

81+
/** Maxium number of chars a single value can have before it will be truncated. */
82+
maxValueLength?: number;
83+
8184
/**
8285
* A callback invoked during event submission, allowing to optionally modify
8386
* the event before it is sent to Sentry.

0 commit comments

Comments
 (0)