|
1 | 1 | import { Scope } from '@sentry/hub'; |
2 | | -import { Client, Event, EventHint, Integration, IntegrationClass, Options, SdkInfo, Severity } from '@sentry/types'; |
| 2 | +import { Client, Event, EventHint, Integration, IntegrationClass, Options, Severity } from '@sentry/types'; |
3 | 3 | import { |
4 | 4 | Dsn, |
5 | 5 | isPrimitive, |
@@ -248,54 +248,31 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement |
248 | 248 | * @returns A new event with more information. |
249 | 249 | */ |
250 | 250 | protected _prepareEvent(event: Event, scope?: Scope, hint?: EventHint): PromiseLike<Event | null> { |
251 | | - const { environment, release, dist, maxValueLength = 250, normalizeDepth = 3 } = this.getOptions(); |
252 | | - |
253 | | - const prepared: Event = { ...event }; |
254 | | - |
255 | | - if (!prepared.timestamp) { |
256 | | - prepared.timestamp = timestampWithMs(); |
257 | | - } |
258 | | - |
259 | | - if (prepared.environment === undefined && environment !== undefined) { |
260 | | - prepared.environment = environment; |
261 | | - } |
262 | | - |
263 | | - if (prepared.release === undefined && release !== undefined) { |
264 | | - prepared.release = release; |
265 | | - } |
266 | | - |
267 | | - if (prepared.dist === undefined && dist !== undefined) { |
268 | | - prepared.dist = dist; |
269 | | - } |
270 | | - |
271 | | - if (prepared.message) { |
272 | | - prepared.message = truncate(prepared.message, maxValueLength); |
273 | | - } |
274 | | - |
275 | | - const exception = prepared.exception && prepared.exception.values && prepared.exception.values[0]; |
276 | | - if (exception && exception.value) { |
277 | | - exception.value = truncate(exception.value, maxValueLength); |
278 | | - } |
| 251 | + const { normalizeDepth = 3 } = this.getOptions(); |
| 252 | + const prepared: Event = { |
| 253 | + ...event, |
| 254 | + event_id: event.event_id || (hint && hint.event_id ? hint.event_id : uuid4()), |
| 255 | + timestamp: event.timestamp || timestampWithMs(), |
| 256 | + }; |
279 | 257 |
|
280 | | - const request = prepared.request; |
281 | | - if (request && request.url) { |
282 | | - request.url = truncate(request.url, maxValueLength); |
283 | | - } |
| 258 | + this._applyClientOptions(prepared); |
| 259 | + this._applyIntegrationsMetadata(prepared); |
284 | 260 |
|
285 | | - if (prepared.event_id === undefined) { |
286 | | - prepared.event_id = hint && hint.event_id ? hint.event_id : uuid4(); |
| 261 | + // If we have scope given to us, use it as the base for further modifications. |
| 262 | + // This allows us to prevent unnecessary copying of data if `captureContext` is not provided. |
| 263 | + let finalScope = scope; |
| 264 | + if (hint && hint.captureContext) { |
| 265 | + finalScope = Scope.clone(finalScope).update(hint.captureContext); |
287 | 266 | } |
288 | 267 |
|
289 | | - this._addIntegrations(prepared.sdk); |
290 | | - |
291 | 268 | // We prepare the result here with a resolved Event. |
292 | 269 | let result = SyncPromise.resolve<Event | null>(prepared); |
293 | 270 |
|
294 | 271 | // This should be the last thing called, since we want that |
295 | 272 | // {@link Hub.addEventProcessor} gets the finished prepared event. |
296 | | - if (scope) { |
| 273 | + if (finalScope) { |
297 | 274 | // In case we have a hub we reassign it. |
298 | | - result = scope.applyToEvent(prepared, hint); |
| 275 | + result = finalScope.applyToEvent(prepared, hint); |
299 | 276 | } |
300 | 277 |
|
301 | 278 | return result.then(evt => { |
@@ -345,11 +322,48 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement |
345 | 322 | }; |
346 | 323 | } |
347 | 324 |
|
| 325 | + /** |
| 326 | + * Enhances event using the client configuration. |
| 327 | + * It takes care of all "static" values like environment, release and `dist`, |
| 328 | + * as well as truncating overly long values. |
| 329 | + * @param event event instance to be enhanced |
| 330 | + */ |
| 331 | + protected _applyClientOptions(event: Event): void { |
| 332 | + const { environment, release, dist, maxValueLength = 250 } = this.getOptions(); |
| 333 | + |
| 334 | + if (event.environment === undefined && environment !== undefined) { |
| 335 | + event.environment = environment; |
| 336 | + } |
| 337 | + |
| 338 | + if (event.release === undefined && release !== undefined) { |
| 339 | + event.release = release; |
| 340 | + } |
| 341 | + |
| 342 | + if (event.dist === undefined && dist !== undefined) { |
| 343 | + event.dist = dist; |
| 344 | + } |
| 345 | + |
| 346 | + if (event.message) { |
| 347 | + event.message = truncate(event.message, maxValueLength); |
| 348 | + } |
| 349 | + |
| 350 | + const exception = event.exception && event.exception.values && event.exception.values[0]; |
| 351 | + if (exception && exception.value) { |
| 352 | + exception.value = truncate(exception.value, maxValueLength); |
| 353 | + } |
| 354 | + |
| 355 | + const request = event.request; |
| 356 | + if (request && request.url) { |
| 357 | + request.url = truncate(request.url, maxValueLength); |
| 358 | + } |
| 359 | + } |
| 360 | + |
348 | 361 | /** |
349 | 362 | * This function adds all used integrations to the SDK info in the event. |
350 | 363 | * @param sdkInfo The sdkInfo of the event that will be filled with all integrations. |
351 | 364 | */ |
352 | | - protected _addIntegrations(sdkInfo?: SdkInfo): void { |
| 365 | + protected _applyIntegrationsMetadata(event: Event): void { |
| 366 | + const sdkInfo = event.sdk; |
353 | 367 | const integrationsArray = Object.keys(this._integrations); |
354 | 368 | if (sdkInfo && integrationsArray.length > 0) { |
355 | 369 | sdkInfo.integrations = integrationsArray; |
|
0 commit comments