Skip to content

Commit fb3343b

Browse files
committed
generate spans properly
1 parent ea907b1 commit fb3343b

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

packages/react/src/profiler.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ export type ProfilerProps = {
8888
name: string;
8989
// If the Profiler is disabled. False by default.
9090
disabled?: boolean;
91-
// If component updates should be displayed as spans. False by default.
92-
generateUpdateSpans?: boolean;
93-
// If time component is on page should be displayed as spans. True by default.
94-
generateRenderSpans?: boolean;
91+
// If time component is on page should be displayed as spans. False by default.
92+
hasRenderSpan?: boolean;
93+
// If component updates should be displayed as spans. True by default.
94+
hasUpdateSpan?: boolean;
9595
// props from child component
9696
updateProps: { [key: string]: any };
9797
};
@@ -110,8 +110,8 @@ class Profiler extends React.Component<ProfilerProps> {
110110

111111
public static defaultProps: Partial<ProfilerProps> = {
112112
disabled: false,
113-
generateRenderSpans: true,
114-
generateUpdateSpans: false,
113+
hasRenderSpan: false,
114+
hasUpdateSpan: true,
115115
};
116116

117117
public constructor(props: ProfilerProps) {
@@ -135,18 +135,21 @@ class Profiler extends React.Component<ProfilerProps> {
135135
popActivity(this.mountActivity);
136136
this.mountActivity = null;
137137

138+
const { name, hasRenderSpan = false } = this.props;
139+
138140
// If we were able to obtain the spanId of the mount activity, we should set the
139141
// next activity as a child to the component mount activity.
140-
if (this.mountSpan) {
142+
if (this.mountSpan && hasRenderSpan) {
141143
this.renderSpan = this.mountSpan.startChild({
142-
description: `<${this.props.name}>`,
144+
description: `<${name}>`,
143145
op: `react.render`,
144146
});
145147
}
146148
}
147149

148150
public componentDidUpdate(prevProps: ProfilerProps): void {
149-
if (prevProps.generateUpdateSpans && this.mountSpan && prevProps.updateProps !== this.props.updateProps) {
151+
const { hasUpdateSpan = true } = prevProps;
152+
if (hasUpdateSpan && this.mountSpan && prevProps.updateProps !== this.props.updateProps) {
150153
const changedProps = Object.keys(prevProps).filter(k => prevProps.updateProps[k] !== this.props.updateProps[k]);
151154
if (changedProps.length > 0) {
152155
const now = timestampWithMs();
@@ -209,8 +212,18 @@ function withProfiler<P extends object>(
209212
* Requires React 16.8 or above.
210213
* @param name displayName of component being profiled
211214
*/
212-
function useProfiler(name: string): void {
215+
function useProfiler(
216+
name: string,
217+
options?: {
218+
disabled?: boolean;
219+
hasRenderSpan?: boolean;
220+
},
221+
): void {
213222
const [mountActivity] = React.useState(() => {
223+
if (options && options.disabled) {
224+
return null;
225+
}
226+
214227
if (getTracingIntegration()) {
215228
return pushActivity(name, 'mount');
216229
}
@@ -223,12 +236,13 @@ function useProfiler(name: string): void {
223236
const mountSpan = getActivitySpan(mountActivity);
224237
popActivity(mountActivity);
225238

226-
const renderSpan = mountSpan
227-
? mountSpan.startChild({
228-
description: `<${name}>`,
229-
op: `react.render`,
230-
})
231-
: undefined;
239+
const renderSpan =
240+
mountSpan && options && options.hasRenderSpan
241+
? mountSpan.startChild({
242+
description: `<${name}>`,
243+
op: `react.render`,
244+
})
245+
: undefined;
232246

233247
return () => {
234248
if (renderSpan) {

0 commit comments

Comments
 (0)