-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathreactnativetracing.ts
More file actions
189 lines (162 loc) · 6.01 KB
/
reactnativetracing.ts
File metadata and controls
189 lines (162 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import type { Client, Event, EventHint, Integration, StartSpanOptions } from '@sentry/core';
import { instrumentOutgoingRequests } from '@sentry/browser';
import { debug, getClient } from '@sentry/core';
import { isWeb } from '../utils/environment';
import { getDevServer } from './../integrations/debugsymbolicatorutils';
import { getTransactionEventDiscardReason } from './onSpanEndUtils';
import { addDefaultOpForSpanFrom, addThreadInfoToSpan, defaultIdleOptions } from './span';
export const INTEGRATION_NAME = 'ReactNativeTracing';
export interface ReactNativeTracingOptions {
/**
* The time that has to pass without any span being created.
* If this time is exceeded, the idle span will finish.
*
* @default 1_000 (ms)
*/
idleTimeoutMs?: number;
/**
* The max. time an idle span may run.
* If this time is exceeded, the idle span will finish no matter what.
*
* @default 60_0000 (ms)
*/
finalTimeoutMs?: number;
/**
* Flag to disable patching all together for fetch requests.
*
* Fetch in React Native is a `whatwg-fetch` polyfill which uses XHR under the hood.
* This causes duplicates when both `traceFetch` and `traceXHR` are enabled at the same time.
*
* @default false
*/
traceFetch: boolean;
/**
* Flag to disable patching all together for xhr requests.
*
* @default true
*/
traceXHR: boolean;
/**
* If true, Sentry will capture http timings and add them to the corresponding http spans.
*
* @default true
*/
enableHTTPTimings: boolean;
/**
* A callback which is called before a span for a navigation is started.
* It receives the options passed to `startSpan`, and expects to return an updated options object.
*/
beforeStartSpan?: (options: StartSpanOptions) => StartSpanOptions;
/**
* This function will be called before creating a span for a request with the given url.
* Return false if you don't want a span for the given url.
*
* @default (url: string) => true
*/
shouldCreateSpanForRequest?(this: void, url: string): boolean;
}
function getDefaultTracePropagationTargets(): RegExp[] | undefined {
if (isWeb()) {
return undefined;
}
return [/.*/];
}
export const defaultReactNativeTracingOptions: ReactNativeTracingOptions = {
// Fetch in React Native is a `whatwg-fetch` polyfill which uses XHR under the hood.
// This causes duplicates when both `traceFetch` and `traceXHR` are enabled at the same time.
// https://github.com/facebook/react-native/blob/28945c68da056ab2ac01de7e542a845b2bca6096/packages/react-native/Libraries/Network/fetch.js
// (RN Web uses browsers native fetch implementation)
traceFetch: isWeb() ? true : false,
traceXHR: true,
enableHTTPTimings: true,
};
export type ReactNativeTracingState = {
currentRoute: string | undefined;
};
export const reactNativeTracingIntegration = (
options: Partial<ReactNativeTracingOptions> = {},
): Integration & {
options: ReactNativeTracingOptions;
state: ReactNativeTracingState;
setCurrentRoute: (route: string) => void;
} => {
const state: ReactNativeTracingState = {
currentRoute: undefined,
};
const finalOptions = {
...defaultReactNativeTracingOptions,
...options,
beforeStartSpan: options.beforeStartSpan ?? ((options: StartSpanOptions) => options),
finalTimeoutMs: options.finalTimeoutMs ?? defaultIdleOptions.finalTimeout,
idleTimeoutMs: options.idleTimeoutMs ?? defaultIdleOptions.idleTimeout,
};
const userShouldCreateSpanForRequest = finalOptions.shouldCreateSpanForRequest;
// Drop Dev Server Spans
const devServerUrl = getDevServer()?.url;
const finalShouldCreateSpanForRequest =
devServerUrl === undefined
? userShouldCreateSpanForRequest
: (url: string): boolean => {
if (url.startsWith(devServerUrl)) {
return false;
}
if (userShouldCreateSpanForRequest) {
return userShouldCreateSpanForRequest(url);
}
return true;
};
finalOptions.shouldCreateSpanForRequest = finalShouldCreateSpanForRequest;
const setup = (client: Client): void => {
addDefaultOpForSpanFrom(client);
addThreadInfoToSpan(client);
instrumentOutgoingRequests(client, {
traceFetch: finalOptions.traceFetch,
traceXHR: finalOptions.traceXHR,
shouldCreateSpanForRequest: finalOptions.shouldCreateSpanForRequest,
tracePropagationTargets: client.getOptions().tracePropagationTargets || getDefaultTracePropagationTargets(),
});
};
const processEvent = (event: Event, _hint: EventHint, _client: Client): Event | null => {
if (event.type === 'transaction') {
const discardReason = getTransactionEventDiscardReason(event);
if (discardReason) {
debug.log(`[ReactNativeTracing] Dropping transaction marked for discard (reason: ${discardReason}).`);
// `@sentry/core` automatically records a dropped event with reason
// `event_processor` when a processor returns `null`, so we don't call
// `recordDroppedEvent` here to avoid double-counting in client reports.
return null;
}
}
if (event.contexts && state.currentRoute) {
event.contexts.app = { view_names: [state.currentRoute], ...event.contexts.app };
}
return event;
};
return {
name: INTEGRATION_NAME,
setup,
processEvent,
options: finalOptions,
state,
setCurrentRoute: (route: string) => {
state.currentRoute = route;
},
};
};
export type ReactNativeTracingIntegration = ReturnType<typeof reactNativeTracingIntegration>;
/**
* Returns the current React Native Tracing integration.
*/
export function getCurrentReactNativeTracingIntegration(): ReactNativeTracingIntegration | undefined {
const client = getClient();
if (!client) {
return undefined;
}
return getReactNativeTracingIntegration(client);
}
/**
* Returns React Native Tracing integration of given client.
*/
export function getReactNativeTracingIntegration(client: Client): ReactNativeTracingIntegration | undefined {
return client.getIntegrationByName(INTEGRATION_NAME);
}