-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathupload.ts
More file actions
472 lines (410 loc) · 15.8 KB
/
Copy pathupload.ts
File metadata and controls
472 lines (410 loc) · 15.8 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import fs from 'fs'
import {URL} from 'url'
import type {ApiKeyValidator} from '@datadog/datadog-ci-base/helpers/apikey'
import type {RepositoryData} from '@datadog/datadog-ci-base/helpers/git/format-git-sourcemaps-data'
import type {RequestBuilder} from '@datadog/datadog-ci-base/helpers/interfaces'
import type {MetricsLogger} from '@datadog/datadog-ci-base/helpers/metrics'
import chalk from 'chalk'
import {Command, Option} from 'clipanion'
import upath from 'upath'
import {BaseCommand} from '@datadog/datadog-ci-base'
import {FIPS_ENV_VAR, FIPS_IGNORE_ERROR_ENV_VAR} from '@datadog/datadog-ci-base/constants'
import {getDatadogSite, getDatadogSiteFromEnv} from '@datadog/datadog-ci-base/helpers/api'
import {newApiKeyValidator} from '@datadog/datadog-ci-base/helpers/apikey'
import {getBaseSourcemapIntakeUrl} from '@datadog/datadog-ci-base/helpers/base-intake-url'
import {doWithMaxConcurrency} from '@datadog/datadog-ci-base/helpers/concurrency'
import {toBoolean} from '@datadog/datadog-ci-base/helpers/env'
import {InvalidConfigurationError} from '@datadog/datadog-ci-base/helpers/errors'
import {enableFips} from '@datadog/datadog-ci-base/helpers/fips'
import {
getRepositoryData,
newSimpleGit,
normalizeSources,
} from '@datadog/datadog-ci-base/helpers/git/format-git-sourcemaps-data'
import {getMetricsLogger} from '@datadog/datadog-ci-base/helpers/metrics'
import {datadogRoute} from '@datadog/datadog-ci-base/helpers/request/datadog-route'
import {upload, UploadStatus} from '@datadog/datadog-ci-base/helpers/upload'
import {getRequestBuilder, buildPath} from '@datadog/datadog-ci-base/helpers/utils'
import * as validation from '@datadog/datadog-ci-base/helpers/validation'
import {cliVersion} from '@datadog/datadog-ci-base/version'
import {addDebugIdToPayloads, extractDebugId} from './debugId'
import {findSourcemaps} from './findSourcemaps'
import {Sourcemap} from './interfaces'
import {
renderCommandInfo,
renderAbsolutePathWarning,
renderConfigurationError,
renderDiscoveryWarning,
renderFailedUpload,
renderGitDataNotAttachedWarning,
renderGitWarning,
renderSourceCodeContextFoundWithoutDebugIdFlag,
renderInvalidPrefix,
renderNoDebugIdFound,
renderRetriedUpload,
renderSourcesNotFoundWarning,
renderSuccessfulCommand,
renderUpload,
} from './renderer'
import {InvalidPayload, validatePayload} from './validation'
export class SourcemapsUploadCommand extends BaseCommand {
public static paths = [['sourcemaps', 'upload']]
public static usage = Command.Usage({
category: 'RUM',
description: 'Upload JavaScript sourcemaps to Datadog.',
details: `
This command will upload all JavaScript sourcemaps and their corresponding JavaScript file to Datadog in order to un-minify front-end stack traces received by Datadog.\n
See README for details.
`,
examples: [
[
'Upload all sourcemaps in current directory',
'datadog-ci sourcemaps upload . --service my-service --minified-path-prefix https://static.datadog.com --release-version 1.234',
],
[
'Upload all sourcemaps in /home/users/ci with 50 concurrent uploads',
'datadog-ci sourcemaps upload /home/users/ci --service my-service --minified-path-prefix https://static.datadog.com --release-version 1.234 --max-concurrency 50',
],
],
})
private basePath = Option.String({required: true})
private disableGit = Option.Boolean('--disable-git')
private quiet = Option.Boolean('--quiet', false)
private dryRun = Option.Boolean('--dry-run', false)
private maxConcurrency = Option.String('--max-concurrency', '20', {validator: validation.isInteger()})
private minifiedPathPrefix = Option.String('--minified-path-prefix')
private projectPath = Option.String('--project-path')
private releaseVersion = Option.String('--release-version')
private repositoryURL = Option.String('--repository-url')
private commitSha = Option.String('--commit-sha')
private debugId = Option.Boolean('--debug-id', false)
private service = Option.String('--service')
private cliVersion = cliVersion
private fips = Option.Boolean('--fips', false)
private fipsIgnoreError = Option.Boolean('--fips-ignore-error', false)
private config = {
apiKey: process.env.DATADOG_API_KEY || process.env.DD_API_KEY,
commitSha: process.env.DD_GIT_COMMIT_SHA,
datadogSite: getDatadogSite(),
fips: toBoolean(process.env[FIPS_ENV_VAR]) ?? false,
fipsIgnoreError: toBoolean(process.env[FIPS_IGNORE_ERROR_ENV_VAR]) ?? false,
repositoryURL: process.env.DD_GIT_REPOSITORY_URL,
}
public async execute() {
enableFips(this.fips || this.config.fips, this.fipsIgnoreError || this.config.fipsIgnoreError)
if (!this.validateOptions()) {
return 1
}
// Normalizing the basePath to resolve .. and .
this.basePath = upath.normalize(this.basePath)
this.context.stdout.write(
renderCommandInfo(
this.basePath,
this.minifiedPathPrefix,
this.projectPath,
this.releaseVersion,
this.service,
this.debugId,
this.maxConcurrency,
this.dryRun
)
)
const loggerTags = [`cli_version:${this.cliVersion}`]
if (!this.debugId) {
loggerTags.push(`version:${this.releaseVersion}`)
loggerTags.push(`service:${this.service}`)
}
const metricsLogger = getMetricsLogger({
datadogSite: getDatadogSiteFromEnv(),
defaultTags: loggerTags,
prefix: 'datadog.ci.sourcemaps.',
})
const apiKeyValidator = newApiKeyValidator({
apiKey: this.config.apiKey,
datadogSite: this.config.datadogSite,
metricsLogger: metricsLogger.logger,
})
const useGit = this.disableGit === undefined || !this.disableGit
const initialTime = Date.now()
const payloads = await this.getPayloadsToUpload(useGit)
if (this.debugId) {
if (payloads.length > 0 && !addDebugIdToPayloads(payloads)) {
this.context.stderr.write(renderNoDebugIdFound())
return 1
}
} else {
for (const {minifiedFilePath} of payloads) {
if (extractDebugId(minifiedFilePath) !== undefined) {
this.context.stderr.write(renderSourceCodeContextFoundWithoutDebugIdFlag())
return 1
}
}
}
const requestBuilder = this.getRequestBuilder()
const uploadMultipart = this.upload(requestBuilder, metricsLogger, apiKeyValidator)
try {
const results = await doWithMaxConcurrency(this.maxConcurrency, payloads, uploadMultipart)
const totalTime = (Date.now() - initialTime) / 1000
this.context.stdout.write(renderSuccessfulCommand(results, totalTime, this.dryRun))
metricsLogger.logger.gauge('duration', totalTime)
return 0
} catch (error) {
if (error instanceof InvalidConfigurationError) {
this.context.stdout.write(renderConfigurationError(error))
return 1
}
// Otherwise unknown error, let's propagate the exception
throw error
} finally {
try {
await metricsLogger.flush()
} catch (err) {
this.context.stdout.write(`WARN: ${err}\n`)
}
}
}
// Fills the 'repository' field of each payload with data gathered using git or provided overrides.
private addRepositoryDataToPayloads = async (payloads: Sourcemap[]) => {
const userProvidedRepoUrl = this.repositoryURL ?? this.config.repositoryURL
const userProvidedCommitSha = this.commitSha ?? this.config.commitSha
// If both user-provided repository URL and commit SHA are provided, use alternative approach without git.
if (userProvidedRepoUrl && userProvidedCommitSha) {
payloads.forEach((payload) => {
const repoPayload = this.getRepositoryPayloadWithoutGit(
userProvidedCommitSha,
userProvidedRepoUrl,
payload.sourcemapPath
)
payload.addRepositoryData({
gitCommitSha: userProvidedCommitSha,
gitRepositoryPayload: repoPayload,
gitRepositoryURL: userProvidedRepoUrl,
})
})
return
}
try {
const repositoryData = await getRepositoryData(await newSimpleGit(), userProvidedRepoUrl, userProvidedCommitSha)
await Promise.all(
payloads.map(async (payload) => {
const repositoryPayload = this.getRepositoryPayload(repositoryData, payload.sourcemapPath)
payload.addRepositoryData({
gitCommitSha: repositoryData.hash,
gitRepositoryPayload: repositoryPayload,
gitRepositoryURL: repositoryData.remote,
})
})
)
} catch (e) {
this.context.stdout.write(renderGitWarning(e) + '\n')
}
}
// Looks for the sourcemaps and minified files on disk and returns
// the associated payloads.
private getMatchingSourcemapFiles = async (): Promise<Sourcemap[]> => {
return findSourcemaps(
this.basePath,
this.maxConcurrency,
(minifiedFilePath, sourcemapPath) => {
const [minifiedURL, relativePath] = this.getMinifiedURLAndRelativePath(minifiedFilePath)
return new Sourcemap(minifiedFilePath, minifiedURL, sourcemapPath, relativePath, this.minifiedPathPrefix)
},
(message) => this.context.stdout.write(renderDiscoveryWarning(message))
)
}
private getMinifiedURLAndRelativePath(minifiedFilePath: string): [string, string] {
const relativePath = upath.relative(this.basePath, minifiedFilePath)
return [buildPath(this.minifiedPathPrefix ?? '', relativePath), relativePath]
}
private getPayloadsToUpload = async (useGit: boolean): Promise<Sourcemap[]> => {
const payloads = await this.getMatchingSourcemapFiles()
if (!useGit) {
return payloads
}
await this.addRepositoryDataToPayloads(payloads)
return payloads
}
// GetRepositoryPayload generates the repository payload for a specific sourcemap.
// It specifically looks for the list of tracked files that are associated to the source paths
// declared inside the sourcemap.
private getRepositoryPayload = (repositoryData: RepositoryData, sourcemapPath: string): string | undefined => {
const onSourcesNotFound = () => {
this.context.stdout.write(renderSourcesNotFoundWarning(sourcemapPath))
}
let repositoryPayload: string | undefined
try {
const files = repositoryData.trackedFilesMatcher.matchSourcemap(sourcemapPath, onSourcesNotFound)
if (files) {
repositoryPayload = JSON.stringify({
data: [
{
files,
hash: repositoryData.hash,
repository_url: repositoryData.remote,
},
],
// Make sure to update the version if the format of the JSON payloads changes in any way.
version: 1,
})
}
return repositoryPayload
} catch (error) {
this.context.stdout.write(renderGitDataNotAttachedWarning(sourcemapPath, error.message))
return undefined
}
}
// Builds the repository payload for a sourcemap using its 'sources' field directly,
// without relying on git. Used when both --repository-url and --commit-sha are provided.
private getRepositoryPayloadWithoutGit = (
commitSha: string,
repositoryURL: string,
sourcemapPath: string
): string | undefined => {
try {
const buff = fs.readFileSync(sourcemapPath, 'utf8')
const srcmapObj = JSON.parse(buff)
if (!srcmapObj.sources || srcmapObj.sources.length === 0) {
return undefined
}
const files = normalizeSources(
srcmapObj.sources as string[],
upath.dirname(sourcemapPath),
process.cwd(),
(source) => this.context.stdout.write(renderAbsolutePathWarning(source))
)
if (files.length === 0) {
this.context.stdout.write(renderSourcesNotFoundWarning(sourcemapPath))
return undefined
}
return JSON.stringify({
data: [{files, hash: commitSha, repository_url: repositoryURL}],
version: 1,
})
} catch (error) {
this.context.stdout.write(renderGitDataNotAttachedWarning(sourcemapPath, error.message))
return undefined
}
}
private getRequestBuilder(): RequestBuilder {
if (!this.config.apiKey) {
throw new InvalidConfigurationError(
`Missing ${chalk.bold('DATADOG_API_KEY')} or ${chalk.bold('DD_API_KEY')} in your environment.`
)
}
return getRequestBuilder({
apiKey: this.config.apiKey,
baseUrl: getBaseSourcemapIntakeUrl(this.config.datadogSite),
headers: new Map([
['DD-EVP-ORIGIN', 'datadog-ci_sourcemaps'],
['DD-EVP-ORIGIN-VERSION', this.cliVersion],
]),
overrideUrl: datadogRoute('/api/v2/srcmap'),
})
}
private validateOptions(): boolean {
if (this.debugId) {
const conflicting: string[] = []
if (this.service !== undefined) {
conflicting.push('--service')
}
if (this.releaseVersion !== undefined) {
conflicting.push('--release-version')
}
if (this.minifiedPathPrefix !== undefined) {
conflicting.push('--minified-path-prefix')
}
if (conflicting.length > 0) {
this.context.stderr.write(
`${conflicting.join(', ')} cannot be used with --debug-id. Sourcemaps are matched by debug ID in this mode.\n`
)
return false
}
return true
}
if (!this.releaseVersion) {
this.context.stderr.write('Missing release version\n')
return false
}
if (!this.service) {
this.context.stderr.write('Missing service\n')
return false
}
if (!this.minifiedPathPrefix) {
this.context.stderr.write('Missing minified path prefix\n')
return false
}
if (!this.isMinifiedPathPrefixValid()) {
this.context.stdout.write(renderInvalidPrefix)
return false
}
return true
}
private isMinifiedPathPrefixValid(): boolean {
let host
try {
const objUrl = new URL(this.minifiedPathPrefix!)
host = objUrl.host
} catch {
// Do nothing.
}
if (!host && !this.minifiedPathPrefix!.startsWith('/')) {
return false
}
return true
}
private upload(
requestBuilder: RequestBuilder,
metricsLogger: MetricsLogger,
apiKeyValidator: ApiKeyValidator
): (sourcemap: Sourcemap) => Promise<UploadStatus> {
return async (sourcemap: Sourcemap) => {
try {
validatePayload(sourcemap, this.context.stdout, this.debugId)
} catch (error) {
if (error instanceof InvalidPayload) {
this.context.stdout.write(renderFailedUpload(sourcemap, error.message))
metricsLogger.logger.increment('skipped_sourcemap', 1, [`reason:${error.reason}`])
} else {
this.context.stdout.write(
renderFailedUpload(
sourcemap,
`Skipping sourcemap ${sourcemap.sourcemapPath} because of error: ${error.message}`
)
)
metricsLogger.logger.increment('skipped_sourcemap', 1, ['reason:unknown'])
}
return UploadStatus.Skipped
}
const payload = sourcemap.asMultipartPayload({
cliVersion: this.cliVersion,
service: this.service,
version: this.releaseVersion,
projectPath: this.projectPath,
context: this.context,
})
if (this.dryRun) {
this.context.stdout.write(`[DRYRUN] ${renderUpload(sourcemap)}`)
return UploadStatus.Success
}
return upload(requestBuilder)(payload, {
apiKeyValidator,
onError: (e) => {
this.context.stdout.write(renderFailedUpload(sourcemap, e.message))
metricsLogger.logger.increment('failed', 1)
},
onRetry: (e, attempts) => {
this.context.stdout.write(renderRetriedUpload(sourcemap, (e as Error).message, attempts))
metricsLogger.logger.increment('retries', 1)
},
onUpload: () => {
if (this.quiet) {
return
}
this.context.stdout.write(renderUpload(sourcemap))
},
retries: 5,
useGzip: true,
})
}
}
}