-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtelemetry.ts
166 lines (134 loc) · 4.79 KB
/
telemetry.ts
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
import SentryCli from "@sentry/cli";
import { defaultStackParser, Hub, makeNodeTransport, NodeClient } from "@sentry/node";
import { NormalizedOptions, SENTRY_SAAS_URL } from "../options-mapping";
const SENTRY_SAAS_HOSTNAME = "sentry.io";
export function createSentryInstance(
options: NormalizedOptions,
shouldSendTelemetry: Promise<boolean>,
bundler: string
) {
const client = new NodeClient({
dsn: "https://[email protected]/6690737",
tracesSampleRate: 1,
sampleRate: 1,
release: __PACKAGE_VERSION__,
integrations: [],
tracePropagationTargets: ["sentry.io/api"],
stackParser: defaultStackParser,
beforeSend: (event) => {
event.exception?.values?.forEach((exception) => {
delete exception.stacktrace;
});
delete event.server_name; // Server name might contain PII
return event;
},
beforeSendTransaction: (event) => {
delete event.server_name; // Server name might contain PII
return event;
},
// We create a transport that stalls sending events until we know that we're allowed to (i.e. when Sentry CLI told
// us that the upload URL is the Sentry SaaS URL)
transport: (nodeTransportOptions) => {
const nodeTransport = makeNodeTransport(nodeTransportOptions);
return {
flush: (timeout) => nodeTransport.flush(timeout),
send: async (request) => {
if (await shouldSendTelemetry) {
return nodeTransport.send(request);
} else {
return undefined;
}
},
};
},
});
const hub = new Hub(client);
setTelemetryDataOnHub(options, hub, bundler);
return { sentryHub: hub, sentryClient: client };
}
export function setTelemetryDataOnHub(options: NormalizedOptions, hub: Hub, bundler: string) {
const { org, project, release, errorHandler, sourcemaps, reactComponentAnnotation } = options;
hub.setTag("upload-legacy-sourcemaps", !!release.uploadLegacySourcemaps);
if (release.uploadLegacySourcemaps) {
hub.setTag(
"uploadLegacySourcemapsEntries",
Array.isArray(release.uploadLegacySourcemaps) ? release.uploadLegacySourcemaps.length : 1
);
}
hub.setTag("module-metadata", !!options.moduleMetadata);
hub.setTag("inject-build-information", !!options._experiments.injectBuildInformation);
// Optional release pipeline steps
if (release.setCommits) {
hub.setTag("set-commits", release.setCommits.auto === true ? "auto" : "manual");
} else {
hub.setTag("set-commits", "undefined");
}
hub.setTag("finalize-release", release.finalize);
hub.setTag("deploy-options", !!release.deploy);
// Miscelaneous options
hub.setTag("custom-error-handler", !!errorHandler);
hub.setTag("sourcemaps-assets", !!sourcemaps?.assets);
hub.setTag(
"delete-after-upload",
!!sourcemaps?.deleteFilesAfterUpload || !!sourcemaps?.filesToDeleteAfterUpload
);
hub.setTag("react-annotate", !!reactComponentAnnotation?.enabled);
hub.setTag("node", process.version);
hub.setTag("platform", process.platform);
hub.setTag("meta-framework", options._metaOptions.telemetry.metaFramework ?? "none");
hub.setTag("application-key-set", options.applicationKey !== undefined);
hub.setTags({
organization: org,
project,
bundler,
});
hub.setUser({ id: org });
}
export async function allowedToSendTelemetry(options: NormalizedOptions): Promise<boolean> {
const { silent, org, project, authToken, url, headers, telemetry, release } = options;
// `options.telemetry` defaults to true
if (telemetry === false) {
return false;
}
if (url === SENTRY_SAAS_URL) {
return true;
}
const cli = new SentryCli(null, {
url,
authToken,
org,
project,
vcsRemote: release.vcsRemote,
silent,
headers,
});
let cliInfo;
try {
// Makes a call to SentryCLI to get the Sentry server URL the CLI uses.
// We need to check and decide to use telemetry based on the CLI's respone to this call
// because only at this time we checked a possibly existing .sentryclirc file. This file
// could point to another URL than the default URL.
cliInfo = await cli.execute(["info"], false);
} catch (e) {
return false;
}
const cliInfoUrl = cliInfo
.split(/(\r\n|\n|\r)/)[0]
?.replace(/^Sentry Server: /, "")
?.trim();
if (cliInfoUrl === undefined) {
return false;
}
return new URL(cliInfoUrl).hostname === SENTRY_SAAS_HOSTNAME;
}
/**
* Flushing the SDK client can fail. We never want to crash the plugin because of telemetry.
*/
export async function safeFlushTelemetry(sentryClient: NodeClient) {
try {
await sentryClient.flush(2000);
} catch {
// Noop when flushing fails.
// We don't even need to log anything because there's likely nothing the user can do and they likely will not care.
}
}