MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / useProfiler

Function useProfiler

packages/react/src/profiler.tsx:183–236  ·  view source on GitHub ↗

* * `useProfiler` is a React hook that profiles a React component. * * Requires React 16.8 or above. * @param name displayName of component being profiled

(
  name: string,
  options: { disabled?: boolean; hasRenderSpan?: boolean } = {
    disabled: false,
    hasRenderSpan: true,
  },
)

Source from the content-addressed store, hash-verified

181 * @param name displayName of component being profiled
182 */
183function useProfiler(
184 name: string,
185 options: { disabled?: boolean; hasRenderSpan?: boolean } = {
186 disabled: false,
187 hasRenderSpan: true,
188 },
189): void {
190 const [mountSpan] = React.useState(() => {
191 if (options?.disabled) {
192 return undefined;
193 }
194
195 return startInactiveSpan({
196 name: `<${name}>`,
197 onlyIfParent: true,
198 op: REACT_MOUNT_OP,
199 attributes: {
200 [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.react.profiler',
201 'ui.component_name': name,
202 },
203 });
204 });
205
206 React.useEffect(() => {
207 if (mountSpan) {
208 mountSpan.end();
209 }
210
211 return (): void => {
212 if (mountSpan && options.hasRenderSpan) {
213 const startTime = spanToJSON(mountSpan).timestamp;
214 const endTimestamp = timestampInSeconds();
215
216 const renderSpan = startInactiveSpan({
217 name: `<${name}>`,
218 onlyIfParent: true,
219 op: REACT_RENDER_OP,
220 startTime,
221 attributes: {
222 [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.react.profiler',
223 'ui.component_name': name,
224 },
225 });
226 if (renderSpan) {
227 // Have to cast to Span because the type of _mountSpan is Span | undefined
228 // and not getting narrowed properly
229 renderSpan.end(endTimestamp);
230 }
231 }
232 };
233 // We only want this to run once.
234 // eslint-disable-next-line react-hooks/exhaustive-deps
235 }, []);
236}
237
238export { Profiler, useProfiler, withProfiler };

Callers 1

profiler.test.tsxFile · 0.90

Calls 4

spanToJSONFunction · 0.90
timestampInSecondsFunction · 0.90
endMethod · 0.65
startInactiveSpanFunction · 0.50

Tested by

no test coverage detected