Skip to content

Commit a51d701

Browse files
authored
fix: Fix for if timestamp of last span is taken for end of transaction (#2410)
* fix: Fix for if timestamp of last span is taken for end of transaction * feat: Add 0 to bail out of maxTransactionTimeout * meta: Changelog
1 parent fe99c14 commit a51d701

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
## 5.12.1
6+
7+
- [apm] ref: If `maxTransactionTimeout` = `0` there is no timeout
8+
- [apm] fix: Make sure that the `maxTransactionTimeout` is always enforced on transaction events
9+
510
## 5.12.0
611

712
- [core] feat: Provide `normalizeDepth` option and sensible default for scope methods (#2404)

packages/apm/src/integrations/tracing.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventProcessor, Hub, Integration, Span, SpanContext, SpanStatus } from '@sentry/types';
1+
import { Event, EventProcessor, Hub, Integration, Span, SpanContext, SpanStatus } from '@sentry/types';
22
import {
33
addInstrumentationHandler,
44
getGlobalObject,
@@ -67,9 +67,10 @@ interface TracingOptions {
6767
/**
6868
* The maximum time a transaction can be before it will be dropped. This is for some edge cases where a browser
6969
* completely freezes the JS state and picks it up later. So after this timeout, the SDK will not send the event.
70+
* If you want to have an unlimited timeout set it to 0.
7071
* Time is in ms.
7172
*
72-
* Default: 120000
73+
* Default: 600000 = 10min
7374
*/
7475
maxTransactionTimeout: number;
7576
}
@@ -126,7 +127,7 @@ export class Tracing implements Integration {
126127
const defaultTracingOrigins = ['localhost', /^\//];
127128
const defaults = {
128129
idleTimeout: 500,
129-
maxTransactionTimeout: 120000,
130+
maxTransactionTimeout: 600000,
130131
shouldCreateSpanForRequest(url: string): boolean {
131132
const origins = (_options && _options.tracingOrigins) || defaultTracingOrigins;
132133
return (
@@ -155,7 +156,7 @@ export class Tracing implements Integration {
155156
/**
156157
* @inheritDoc
157158
*/
158-
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
159+
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
159160
Tracing._getCurrentHub = getCurrentHub;
160161

161162
if (!Tracing._isEnabled()) {
@@ -192,6 +193,25 @@ export class Tracing implements Integration {
192193
sampled: true,
193194
});
194195
}
196+
197+
// This EventProcessor makes sure that we never send an transaction that is older than maxTransactionTimeout
198+
addGlobalEventProcessor((event: Event) => {
199+
const self = getCurrentHub().getIntegration(Tracing);
200+
if (!self) {
201+
return event;
202+
}
203+
204+
if (
205+
event.type === 'transaction' &&
206+
event.timestamp &&
207+
Tracing.options.maxTransactionTimeout !== 0 &&
208+
timestampWithMs() > event.timestamp + Tracing.options.maxTransactionTimeout
209+
) {
210+
return null;
211+
}
212+
213+
return event;
214+
});
195215
}
196216

197217
/**
@@ -282,7 +302,10 @@ export class Tracing implements Integration {
282302
public static finishIdleTransaction(): void {
283303
const active = Tracing._activeTransaction as SpanClass;
284304
if (active) {
285-
if (timestampWithMs() > active.startTimestamp + Tracing.options.maxTransactionTimeout) {
305+
if (
306+
Tracing.options.maxTransactionTimeout !== 0 &&
307+
timestampWithMs() > active.startTimestamp + Tracing.options.maxTransactionTimeout
308+
) {
286309
// If we reached the max timeout of the transaction, we will just not finish it and therefore discard it.
287310
Tracing._activeTransaction = undefined;
288311
} else {

0 commit comments

Comments
 (0)