-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtelemetry.ts
More file actions
174 lines (137 loc) · 5.37 KB
/
telemetry.ts
File metadata and controls
174 lines (137 loc) · 5.37 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
import SentryCli from "@sentry/cli";
import { Client } from "@sentry/types";
import { applySdkMetadata, ServerRuntimeClient, ServerRuntimeClientOptions } from "@sentry/core";
import { NormalizedOptions, SENTRY_SAAS_URL } from "../options-mapping";
import { Scope } from "@sentry/core";
import { createStackParser, nodeStackLineParser } from "@sentry/utils";
import { makeOptionallyEnabledNodeTransport } from "./transports";
import { getProjects } from "../utils";
const SENTRY_SAAS_HOSTNAME = "sentry.io";
const stackParser = createStackParser(nodeStackLineParser());
export function createSentryInstance(
options: NormalizedOptions,
shouldSendTelemetry: Promise<boolean>,
buildTool: string
): { sentryScope: Scope; sentryClient: Client } {
const clientOptions: ServerRuntimeClientOptions = {
platform: "node",
runtime: { name: "node", version: global.process.version },
dsn: "https://4c2bae7d9fbc413e8f7385f55c515d51@o1.ingest.sentry.io/6690737",
tracesSampleRate: 1,
sampleRate: 1,
release: __PACKAGE_VERSION__,
integrations: [],
tracePropagationTargets: ["sentry.io/api"],
stackParser,
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: makeOptionallyEnabledNodeTransport(shouldSendTelemetry),
};
applySdkMetadata(clientOptions, "node");
const client = new ServerRuntimeClient(clientOptions);
const scope = new Scope();
scope.setClient(client);
setTelemetryDataOnScope(options, scope, buildTool);
return { sentryScope: scope, sentryClient: client };
}
export function setTelemetryDataOnScope(
options: NormalizedOptions,
scope: Scope,
buildTool: string
): void {
const { org, project, release, errorHandler, sourcemaps, reactComponentAnnotation } = options;
scope.setTag("upload-legacy-sourcemaps", !!release.uploadLegacySourcemaps);
if (release.uploadLegacySourcemaps) {
scope.setTag(
"uploadLegacySourcemapsEntries",
Array.isArray(release.uploadLegacySourcemaps) ? release.uploadLegacySourcemaps.length : 1
);
}
scope.setTag("module-metadata", !!options.moduleMetadata);
scope.setTag("inject-build-information", !!options._experiments.injectBuildInformation);
// Optional release pipeline steps
if (release.setCommits) {
scope.setTag("set-commits", release.setCommits.auto === true ? "auto" : "manual");
} else {
scope.setTag("set-commits", "undefined");
}
scope.setTag("finalize-release", release.finalize);
scope.setTag("deploy-options", !!release.deploy);
// Miscellaneous options
scope.setTag("custom-error-handler", !!errorHandler);
scope.setTag("sourcemaps-assets", !!sourcemaps?.assets);
scope.setTag("delete-after-upload", !!sourcemaps?.filesToDeleteAfterUpload);
scope.setTag("sourcemaps-disabled", !!sourcemaps?.disable);
scope.setTag("react-annotate", !!reactComponentAnnotation?.enabled);
scope.setTag("node", process.version);
scope.setTag("platform", process.platform);
scope.setTag("meta-framework", options._metaOptions.telemetry.metaFramework ?? "none");
scope.setTag("application-key-set", options.applicationKey !== undefined);
scope.setTag("ci", !!process.env["CI"]);
scope.setTags({
organization: org,
project: Array.isArray(project) ? project.join(", ") : project ?? "undefined",
bundler: buildTool,
});
scope.setTag("bundler-major-version", options._metaOptions.telemetry.bundlerMajorVersion);
scope.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: getProjects(project)?.[0],
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 response 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: Client): Promise<void> {
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.
}
}