Skip to content

Commit ac72ee4

Browse files
committed
fix(vue): prevent after hook from starting new span
1 parent eaf2e81 commit ac72ee4

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

packages/vue/src/tracing.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export const createTracingMixins = (options: TracingOptions): Mixins => {
6666
continue;
6767
}
6868

69-
for (const internalHook of internalHooks) {
69+
internalHooks.forEach((internalHook, hookType) => {
70+
const isBeforeHook = hookType === 0;
71+
7072
mixins[internalHook] = function(this: VueSentry) {
7173
const isRoot = this.$root === this;
7274

@@ -95,24 +97,24 @@ export const createTracingMixins = (options: TracingOptions): Mixins => {
9597

9698
this.$_sentrySpans = this.$_sentrySpans || {};
9799

98-
// On the first handler call (before), it'll be undefined, as `$once` will add it in the future.
99-
// However, on the second call (after), it'll be already in place.
100-
const span = this.$_sentrySpans[operation];
101-
102-
if (span) {
103-
span.finish();
104-
finishRootSpan(this, timestampInSeconds(), options.timeout);
105-
} else {
100+
if (isBeforeHook) {
106101
const activeTransaction = this.$root?.$_sentryRootSpan || getActiveTransaction();
107102
if (activeTransaction) {
108103
this.$_sentrySpans[operation] = activeTransaction.startChild({
109104
description: `Vue <${name}>`,
110105
op: `${VUE_OP}.${operation}`,
111106
});
112107
}
108+
} else {
109+
// The span should already be added via the first handler call (in the 'before' hook)
110+
const span = this.$_sentrySpans[operation];
111+
if (!span) return; // If not, then the before hook did not start the tracking span, probably because it happened even before there is an active transaction
112+
113+
span.finish();
114+
finishRootSpan(this, timestampInSeconds(), options.timeout);
113115
}
114116
};
115-
}
117+
});
116118
}
117119

118120
return mixins;

0 commit comments

Comments
 (0)