Skip to content

Commit 371a247

Browse files
committed
ref: create children from span
1 parent 75913f0 commit 371a247

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

packages/apm/src/integrations/tracing.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// tslint:disable: max-file-line-count
12
import { Hub } from '@sentry/hub';
23
import { Event, EventProcessor, Integration, Severity, Span, SpanContext, TransactionContext } from '@sentry/types';
34
import {
@@ -827,7 +828,7 @@ export class Tracing implements Integration {
827828
* @param finish if a span should be finished after the activity is removed
828829
*
829830
*/
830-
public static popActivity(id: number, spanData?: { [key: string]: any }, finish: boolean = true): void {
831+
public static popActivity(id: number, spanData?: { [key: string]: any }): void {
831832
// The !id is on purpose to also fail with 0
832833
// Since 0 is returned by push activity in case there is no active transaction
833834
if (!id) {
@@ -854,9 +855,7 @@ export class Tracing implements Integration {
854855
if (Tracing.options && Tracing.options.debug && Tracing.options.debug.spanDebugTimingInfo) {
855856
Tracing._addSpanDebugInfo(span);
856857
}
857-
if (finish) {
858-
span.finish();
859-
}
858+
span.finish();
860859
}
861860
// tslint:disable-next-line: no-dynamic-delete
862861
delete Tracing._activities[id];
@@ -888,7 +887,10 @@ export class Tracing implements Integration {
888887
if (Tracing._getCurrentHub) {
889888
const hub = Tracing._getCurrentHub();
890889
if (hub) {
891-
return Tracing._activities[id].span;
890+
const activity = Tracing._activities[id];
891+
if (activity) {
892+
return activity.span;
893+
}
892894
}
893895
}
894896
return undefined;

packages/react/src/profiler.tsx

+33-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub } from '@sentry/browser';
2-
import { Integration, IntegrationClass, Span, SpanContext } from '@sentry/types';
2+
import { Integration, IntegrationClass, Span } from '@sentry/types';
33
import { logger, timestampWithMs } from '@sentry/utils';
44
import * as hoistNonReactStatic from 'hoist-non-react-statics';
55
import * as React from 'react';
@@ -68,7 +68,6 @@ function warnAboutTracing(name: string): void {
6868
function pushActivity(
6969
name: string,
7070
op: string,
71-
context?: SpanContext,
7271
options?: {
7372
autoPopAfter?: number;
7473
parentSpanId?: string;
@@ -84,7 +83,6 @@ function pushActivity(
8483
{
8584
description: `<${name}>`,
8685
op: `react.${op}`,
87-
...context,
8886
},
8987
options,
9088
);
@@ -96,13 +94,13 @@ function pushActivity(
9694
* @param activity id of activity that is being popped
9795
* @param finish if a span should be finished after the activity is removed
9896
*/
99-
function popActivity(activity: number | null, finish: boolean = true): void {
97+
function popActivity(activity: number | null): void {
10098
if (activity === null || globalTracingIntegration === null) {
10199
return;
102100
}
103101

104102
// tslint:disable-next-line:no-unsafe-any
105-
(globalTracingIntegration as any).constructor.popActivity(activity, undefined, finish);
103+
(globalTracingIntegration as any).constructor.popActivity(activity, undefined);
106104
}
107105

108106
function getActivitySpan(activity: number | null): Span | undefined {
@@ -130,14 +128,15 @@ export type ProfilerProps = {
130128
class Profiler extends React.Component<ProfilerProps> {
131129
// The activity representing how long it takes to mount a component.
132130
public mountActivity: number | null = null;
133-
// The spanId of the mount activity
134-
public mountSpanId: string | null = null;
131+
// The span of the mount activity
132+
public span: Span | undefined = undefined;
135133
// The activity representing how long a component was on the page.
136134
public renderActivity: number | null = null;
135+
public renderSpan: Span | undefined = undefined;
137136

138137
public static defaultProps: Partial<ProfilerProps> = {
139138
disabled: false,
140-
generateUpdateSpans: false,
139+
generateUpdateSpans: true,
141140
};
142141

143142
public constructor(props: ProfilerProps) {
@@ -157,42 +156,42 @@ class Profiler extends React.Component<ProfilerProps> {
157156

158157
// If a component mounted, we can finish the mount activity.
159158
public componentDidMount(): void {
160-
afterNextFrame(() => {
161-
const span = getActivitySpan(this.mountActivity);
162-
if (span) {
163-
this.mountSpanId = span.spanId;
164-
}
165-
popActivity(this.mountActivity);
166-
this.mountActivity = null;
167-
168-
// If we were able to obtain the spanId of the mount activity, we should set the
169-
// next activity as a child to the component mount activity.
170-
const options = span ? { parentSpanId: span.spanId } : {};
171-
this.renderActivity = pushActivity(this.props.name, 'render', {}, options);
172-
});
159+
// afterNextFrame(() => {
160+
this.span = getActivitySpan(this.mountActivity);
161+
popActivity(this.mountActivity);
162+
this.mountActivity = null;
163+
164+
// If we were able to obtain the spanId of the mount activity, we should set the
165+
// next activity as a child to the component mount activity.
166+
if (this.span) {
167+
this.renderSpan = this.span.startChild({
168+
description: `<${this.props.name}>`,
169+
op: `react.render`,
170+
});
171+
}
172+
// });
173173
}
174174

175175
public componentDidUpdate(prevProps: ProfilerProps): void {
176-
if (prevProps.generateUpdateSpans && this.mountSpanId) {
176+
if (prevProps.generateUpdateSpans && this.span && prevProps !== this.props) {
177177
const now = timestampWithMs();
178-
const updateActivity = pushActivity(
179-
prevProps.name,
180-
'update',
181-
{
182-
endTimestamp: now,
183-
startTimestamp: now,
184-
},
185-
{ parentSpanId: this.mountSpanId },
186-
);
187-
popActivity(updateActivity, false);
178+
this.span.startChild({
179+
description: `<${prevProps.name}>`,
180+
endTimestamp: now,
181+
op: `react.update`,
182+
startTimestamp: now,
183+
});
188184
}
189185
}
190186

191187
// If a component doesn't mount, the render activity will be end when the
192188
public componentWillUnmount(): void {
193189
afterNextFrame(() => {
194-
popActivity(this.renderActivity, false);
195-
this.renderActivity = null;
190+
if (this.renderSpan) {
191+
this.renderSpan.finish();
192+
}
193+
// popActivity(this.renderActivity);
194+
// this.renderActivity = null;
196195
});
197196
}
198197

0 commit comments

Comments
 (0)