Skip to content

Commit 4df15af

Browse files
committed
Remove apm as dependency, make tracing default w. integration
1 parent c4f46e0 commit 4df15af

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

packages/integrations/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"module": "esm/index.js",
1717
"types": "dist/index.d.ts",
1818
"dependencies": {
19-
"@sentry/apm": "5.15.5",
2019
"@sentry/types": "5.15.5",
2120
"@sentry/utils": "5.15.5",
2221
"tslib": "^1.9.3"

packages/integrations/src/vue.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { Integrations as APMIntegrations, Span as SpanClass } from '@sentry/apm';
2-
import { EventProcessor, Hub, Integration } from '@sentry/types';
1+
import { EventProcessor, Hub, Integration, IntegrationClass, Span } from '@sentry/types';
32
import { basename, getGlobalObject, logger, timestampWithMs } from '@sentry/utils';
43

4+
/**
5+
* Used to extract Tracing integration from the current client,
6+
* without the need to import `Tracing` itself from the @sentry/apm package.
7+
*/
8+
const TRACING_GETTER = {
9+
id: 'Tracing',
10+
} as IntegrationClass<Integration>;
11+
512
/** Global Vue object limited to the methods/attributes we require */
613
interface VueInstance {
714
config?: {
@@ -46,7 +53,10 @@ interface IntegrationOptions {
4653
*/
4754
logErrors: boolean;
4855

49-
/** When set to `true`, enables tracking of components lifecycle performance. */
56+
/**
57+
* When set to `false`, disables tracking of components lifecycle performance.
58+
* By default, it tracks only when `Tracing` integration is also enabled.
59+
*/
5060
tracing: boolean;
5161

5262
/** {@link TracingOptions} */
@@ -130,7 +140,7 @@ export class Vue implements Integration {
130140
* Cache holding already processed component names
131141
*/
132142
private readonly _componentsCache: { [key: string]: string } = {};
133-
private _rootSpan?: SpanClass;
143+
private _rootSpan?: Span;
134144
private _rootSpanTimer?: ReturnType<typeof setTimeout>;
135145
private _tracingActivity?: number;
136146

@@ -142,7 +152,7 @@ export class Vue implements Integration {
142152
Vue: getGlobalObject<any>().Vue, // tslint:disable-line:no-unsafe-any
143153
attachProps: true,
144154
logErrors: false,
145-
tracing: false,
155+
tracing: true,
146156
...options,
147157
tracingOptions: {
148158
hooks: ['beforeMount', 'mounted', 'beforeUpdate', 'updated'],
@@ -203,7 +213,7 @@ export class Vue implements Integration {
203213

204214
const name = this._getComponentName(vm);
205215
const rootMount = name === ROOT_COMPONENT_NAME;
206-
const spans: { [key: string]: SpanClass } = {};
216+
const spans: { [key: string]: Span } = {};
207217

208218
// Render hook starts after once event is emitted,
209219
// but it ends before the second event of the same type.
@@ -216,16 +226,22 @@ export class Vue implements Integration {
216226
// On the first handler call (before), it'll be undefined, as `$once` will add it in the future.
217227
// However, on the second call (after), it'll be already in place.
218228
if (this._rootSpan) {
219-
this._finishRootSpan(now);
229+
this._finishRootSpan(now, getCurrentHub);
220230
} else {
221231
vm.$once(`hook:${hook}`, () => {
222232
// Create an activity on the first event call. There'll be no second call, as rootSpan will be in place,
223233
// thus new event handler won't be attached.
224-
this._tracingActivity = APMIntegrations.Tracing.pushActivity('Vue Application Render');
234+
235+
// We do this whole dance with `TRACING_GETTER` to prevent `@sentry/apm` from becoming a peerDependency.
236+
// We also need to ask for the `.constructor`, as `pushActivity` and `popActivity` are static, not instance methods.
237+
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
238+
if (tracingIntegration) {
239+
this._tracingActivity = (tracingIntegration as any).constructor.pushActivity('Vue Application Render');
240+
}
225241
this._rootSpan = getCurrentHub().startSpan({
226242
description: 'Application Render',
227243
op: 'Vue',
228-
}) as SpanClass;
244+
});
229245
});
230246
}
231247
};
@@ -248,7 +264,7 @@ export class Vue implements Integration {
248264
// However, on the second call (after), it'll be already in place.
249265
if (span) {
250266
span.finish();
251-
this._finishRootSpan(now);
267+
this._finishRootSpan(now, getCurrentHub);
252268
} else {
253269
vm.$once(`hook:${hook}`, () => {
254270
if (this._rootSpan) {
@@ -277,17 +293,22 @@ export class Vue implements Integration {
277293
};
278294

279295
/** Finish top-level span and activity with a debounce configured using `timeout` option */
280-
private _finishRootSpan(timestamp: number): void {
296+
private _finishRootSpan(timestamp: number, getCurrentHub: () => Hub): void {
281297
if (this._rootSpanTimer) {
282298
clearTimeout(this._rootSpanTimer);
283299
}
284300

285301
this._rootSpanTimer = setTimeout(() => {
286302
if (this._rootSpan) {
287-
this._rootSpan.timestamp = timestamp;
303+
((this._rootSpan as unknown) as { timestamp: number }).timestamp = timestamp;
288304
}
289305
if (this._tracingActivity) {
290-
APMIntegrations.Tracing.popActivity(this._tracingActivity);
306+
// We do this whole dance with `TRACING_GETTER` to prevent `@sentry/apm` from becoming a peerDependency.
307+
// We also need to ask for the `.constructor`, as `pushActivity` and `popActivity` are static, not instance methods.
308+
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
309+
if (tracingIntegration) {
310+
(tracingIntegration as any).constructor.popActivity(this._tracingActivity);
311+
}
291312
}
292313
}, this._options.tracingOptions.timeout);
293314
}
@@ -298,8 +319,7 @@ export class Vue implements Integration {
298319

299320
this._options.Vue.mixin({
300321
beforeCreate(this: ViewModel): void {
301-
// TODO: Move this check to `setupOnce` when we rework integrations initialization in v6
302-
if (getCurrentHub().getIntegration(APMIntegrations.Tracing)) {
322+
if (getCurrentHub().getIntegration(TRACING_GETTER)) {
303323
// `this` points to currently rendered component
304324
applyTracingHooks(this, getCurrentHub);
305325
} else {

0 commit comments

Comments
 (0)