-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathflare.ts
724 lines (638 loc) · 24.5 KB
/
flare.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import IService = google.cloud.run.v2.IService
import IContainer = google.cloud.run.v2.IContainer
import fs from 'fs'
import path from 'path'
import process from 'process'
import util from 'util'
import {Logging} from '@google-cloud/logging'
import {RevisionsClient, ServicesClient} from '@google-cloud/run'
import {google} from '@google-cloud/run/build/protos/protos'
import chalk from 'chalk'
import {Command, Option} from 'clipanion'
import {GoogleAuth} from 'google-auth-library'
import {
ADDITIONAL_FILES_DIRECTORY,
API_KEY_ENV_VAR,
CI_API_KEY_ENV_VAR,
FIPS_ENV_VAR,
FIPS_IGNORE_ERROR_ENV_VAR,
FLARE_OUTPUT_DIRECTORY,
FLARE_PROJECT_FILES,
INSIGHTS_FILE_NAME,
LOGS_DIRECTORY,
PROJECT_FILES_DIRECTORY,
} from '../../constants'
import {toBoolean} from '../../helpers/env'
import {enableFips} from '../../helpers/fips'
import {getProjectFiles, sendToDatadog, validateFilePath, validateStartEndFlags} from '../../helpers/flare'
import {createDirectories, deleteFolder, writeFile, zipContents} from '../../helpers/fs'
import {requestConfirmation, requestFilePath} from '../../helpers/prompt'
import * as helpersRenderer from '../../helpers/renderer'
import {renderAdditionalFiles, renderProjectFiles} from '../../helpers/renderer'
import {formatBytes, maskString} from '../../helpers/utils'
import {version} from '../../helpers/version'
import {getUniqueFileNames} from '../lambda/flare'
import {SKIP_MASKING_CLOUDRUN_ENV_VARS} from './constants'
import {CloudRunLog, LogConfig} from './interfaces'
import {renderAuthenticationInstructions} from './renderer'
const SERVICE_CONFIG_FILE_NAME = 'service_config.json'
const FLARE_ZIP_FILE_NAME = 'cloud-run-flare-output.zip'
const ALL_LOGS_FILE_NAME = 'all_logs.csv'
const WARNING_LOGS_FILE_NAME = 'warning_logs.csv'
const ERRORS_LOGS_FILE_NAME = 'error_logs.csv'
const DEBUG_LOGS_FILE_NAME = 'debug_logs.csv'
// What's the maximum number of revisions we want to include? Too many revisions will flood the INSIGHTS.md file
const MAX_REVISIONS = 10
// Must be in range 0 - 1000. If more logs are needed, pagination must be implemented
export const MAX_LOGS = 1000
// How old the logs can be in minutes. Skip older logs
const MAX_LOG_AGE_MINUTES = 1440
const FILTER_ORDER = 'timestamp asc'
// Types of log files to create
const LOG_CONFIGS: LogConfig[] = [
{type: 'total', fileName: ALL_LOGS_FILE_NAME},
{type: 'warning', severityFilter: ' AND severity>="WARNING"', fileName: WARNING_LOGS_FILE_NAME},
{type: 'error', severityFilter: ' AND severity>="ERROR"', fileName: ERRORS_LOGS_FILE_NAME},
{type: 'debug', severityFilter: ' AND severity="DEBUG"', fileName: DEBUG_LOGS_FILE_NAME},
]
export class CloudRunFlareCommand extends Command {
public static paths = [['cloud-run', 'flare']]
public static usage = Command.Usage({
category: 'Serverless',
description: 'Gather Cloud Run service configuration and sends it to Datadog.',
})
private isDryRun = Option.Boolean('-d,--dry,--dry-run', false)
private withLogs = Option.Boolean('--with-logs', false)
private service = Option.String('-s,--service')
private project = Option.String('-p,--project')
private region = Option.String('-r,--region,-l,--location')
private caseId = Option.String('-c,--case-id')
private email = Option.String('-e,--email')
private start = Option.String('--start')
private end = Option.String('--end')
private apiKey?: string
private fips = Option.Boolean('--fips', false)
private fipsIgnoreError = Option.Boolean('--fips-ignore-error', false)
private config = {
fips: toBoolean(process.env[FIPS_ENV_VAR]) ?? false,
fipsIgnoreError: toBoolean(process.env[FIPS_IGNORE_ERROR_ENV_VAR]) ?? false,
}
/**
* Entry point for the `cloud-run flare` command.
* Gathers Cloud Run service configuration and sends it to Datadog.
* @returns 0 if the command ran successfully, 1 otherwise.
*/
public async execute() {
enableFips(this.fips || this.config.fips, this.fipsIgnoreError || this.config.fipsIgnoreError)
this.context.stdout.write(helpersRenderer.renderFlareHeader('Cloud Run', this.isDryRun))
const errorMessages: string[] = []
// Validate service
if (this.service === undefined) {
errorMessages.push(helpersRenderer.renderError('No service specified. [-s,--service]'))
}
// Validate project
if (this.project === undefined) {
errorMessages.push(helpersRenderer.renderError('No project specified. [-p,--project]'))
}
// Validate region
if (this.region === undefined) {
errorMessages.push(helpersRenderer.renderError('No region specified. [-r,--region]'))
}
// Validate Datadog API key
this.apiKey = process.env[CI_API_KEY_ENV_VAR] ?? process.env[API_KEY_ENV_VAR]
if (this.apiKey === undefined) {
errorMessages.push(
helpersRenderer.renderError(
'No Datadog API key specified. Set an API key with the DATADOG_API_KEY environment variable.'
)
)
}
// Validate case ID
if (this.caseId === undefined) {
errorMessages.push(helpersRenderer.renderError('No case ID specified. [-c,--case-id]'))
}
// Validate email
if (this.email === undefined) {
errorMessages.push(helpersRenderer.renderError('No email specified. [-e,--email]'))
}
// Validate start/end flags if both are specified
let startMillis
let endMillis
try {
;[startMillis, endMillis] = validateStartEndFlags(this.start, this.end)
} catch (err) {
if (err instanceof Error) {
errorMessages.push(helpersRenderer.renderError(err.message))
}
}
// If there are errors, print them and exit
if (errorMessages.length > 0) {
for (const message of errorMessages) {
this.context.stderr.write(message)
}
return 1
}
// Verify GCP credentials
this.context.stdout.write(chalk.bold('\n🔑 Verifying GCP credentials...\n'))
const authenticated = await checkAuthentication()
if (!authenticated) {
this.context.stderr.write(renderAuthenticationInstructions())
return 1
}
this.context.stdout.write('GCP credentials verified!\n')
// Get and print service configuration
this.context.stdout.write(chalk.bold('\n🔍 Fetching service configuration...\n'))
const runClient = new ServicesClient()
let config: IService
try {
config = await getCloudRunServiceConfig(runClient, this.service!, this.project!, this.region!)
} catch (err) {
if (err instanceof Error) {
this.context.stderr.write(helpersRenderer.renderError(`Unable to fetch service configuration: ${err.message}`))
}
return 1
}
config = maskConfig(config)
const summarizedConfig = summarizeConfig(config)
// 10 is the depth when inspecting the config file. Cloud-run configs have high depth, so
// we must raise the depth from the default depth of 2.
const summarizedConfigStr = util.inspect(summarizedConfig, false, 10, true)
this.context.stdout.write(`\n${summarizedConfigStr}\n`)
this.context.stdout.write(
chalk.italic(
`(This is a summary of the configuration. The full configuration will be saved in "${SERVICE_CONFIG_FILE_NAME}".)\n`
)
)
// Get project files
this.context.stdout.write(chalk.bold('\n📁 Searching for project files in current directory...\n'))
const projectFilePaths = await getProjectFiles(FLARE_PROJECT_FILES)
this.context.stdout.write(renderProjectFiles(projectFilePaths))
// Additional files
this.context.stdout.write('\n')
const additionalFilePaths = new Set<string>()
let confirmAdditionalFiles
try {
confirmAdditionalFiles = await requestConfirmation('Do you want to specify any additional files to flare?', false)
} catch (err) {
if (err instanceof Error) {
this.context.stderr.write(helpersRenderer.renderError(err.message))
}
return 1
}
while (confirmAdditionalFiles) {
this.context.stdout.write('\n')
let filePath: string
try {
filePath = await requestFilePath()
} catch (err) {
if (err instanceof Error) {
this.context.stderr.write(helpersRenderer.renderError(err.message))
}
return 1
}
if (filePath === '') {
this.context.stdout.write(renderAdditionalFiles(additionalFilePaths))
break
}
try {
filePath = validateFilePath(filePath, projectFilePaths, additionalFilePaths)
additionalFilePaths.add(filePath)
const fileName = path.basename(filePath)
this.context.stdout.write(`• Added file '${fileName}'\n`)
} catch (err) {
if (err instanceof Error) {
this.context.stderr.write(err.message)
}
}
}
// Get recent revisions, which will be used to generate insights file
this.context.stdout.write(chalk.bold('\n🌧 Fetching recent revisions...\n'))
let revisions: string[] = []
try {
revisions = await getRecentRevisions(this.service!, this.region!, this.project!)
this.context.stdout.write(`• Found ${revisions.length} revisions\n`)
} catch (err) {
const errorDetails = err instanceof Error ? err.message : ''
this.context.stdout.write(helpersRenderer.renderSoftWarning(`Unable to fetch recent revisions. ${errorDetails}`))
}
// Get logs
const logFileMappings = new Map<string, CloudRunLog[]>()
if (this.withLogs) {
this.context.stdout.write(chalk.bold('\n📖 Getting logs...\n'))
const logClient = new Logging({projectId: this.project})
for (const logConfig of LOG_CONFIGS) {
try {
const logs = await getLogs(
logClient,
this.service!,
this.region!,
startMillis,
endMillis,
logConfig.severityFilter
)
if (logs.length === 0) {
this.context.stdout.write(`• No ${logConfig.type} logs were found\n`)
} else {
this.context.stdout.write(`• Found ${logs.length} ${logConfig.type} logs\n`)
logFileMappings.set(logConfig.fileName, logs)
}
} catch (err) {
const msg = err instanceof Error ? err.message : ''
this.context.stderr.write(`• Unable to get ${logConfig.type} logs: ${msg}\n`)
}
}
}
try {
// Create folders
const rootFolderPath = path.join(process.cwd(), FLARE_OUTPUT_DIRECTORY)
const logsFolderPath = path.join(rootFolderPath, LOGS_DIRECTORY)
const projectFilesFolderPath = path.join(rootFolderPath, PROJECT_FILES_DIRECTORY)
const additionalFilesFolderPath = path.join(rootFolderPath, ADDITIONAL_FILES_DIRECTORY)
this.context.stdout.write(chalk.bold(`\n💾 Saving files to ${rootFolderPath}...\n`))
if (fs.existsSync(rootFolderPath)) {
deleteFolder(rootFolderPath)
}
const subFolders = []
if (logFileMappings.size > 0) {
subFolders.push(logsFolderPath)
}
if (projectFilePaths.size > 0) {
subFolders.push(projectFilesFolderPath)
}
if (additionalFilePaths.size > 0) {
subFolders.push(additionalFilesFolderPath)
}
createDirectories(rootFolderPath, subFolders)
// Write config file
const configFilePath = path.join(rootFolderPath, SERVICE_CONFIG_FILE_NAME)
writeFile(configFilePath, JSON.stringify(config, undefined, 2))
this.context.stdout.write(`• Saved function config to ./${SERVICE_CONFIG_FILE_NAME}\n`)
// Write logs
for (const [fileName, logs] of logFileMappings) {
const logFilePath = path.join(logsFolderPath, fileName)
saveLogsFile(logs, logFilePath)
this.context.stdout.write(`• Saved logs to ./${LOGS_DIRECTORY}/${fileName}\n`)
}
// Write project files
for (const filePath of projectFilePaths) {
const fileName = path.basename(filePath)
const newFilePath = path.join(projectFilesFolderPath, fileName)
fs.copyFileSync(filePath, newFilePath)
this.context.stdout.write(`• Copied ${fileName} to ./${PROJECT_FILES_DIRECTORY}/${fileName}\n`)
}
// Write additional files
const additionalFilesMap = getUniqueFileNames(additionalFilePaths)
for (const [originalFilePath, newFileName] of additionalFilesMap) {
const originalFileName = path.basename(originalFilePath)
const newFilePath = path.join(additionalFilesFolderPath, newFileName)
fs.copyFileSync(originalFilePath, newFilePath)
this.context.stdout.write(`• Copied ${originalFileName} to ./${ADDITIONAL_FILES_DIRECTORY}/${newFileName}\n`)
}
// Write insights file
try {
const insightsFilePath = path.join(rootFolderPath, INSIGHTS_FILE_NAME)
generateInsightsFile(
insightsFilePath,
this.isDryRun,
config,
this.service!,
this.region!,
this.project!,
revisions
)
this.context.stdout.write(`• Saved insights file to ./${INSIGHTS_FILE_NAME}\n`)
} catch (err) {
const errorDetails = err instanceof Error ? err.message : ''
this.context.stdout.write(
helpersRenderer.renderSoftWarning(`Unable to create INSIGHTS.md file. ${errorDetails}`)
)
}
// Exit if dry run
const outputMsg = `\nℹ️ Your output files are located at: ${rootFolderPath}\n\n`
if (this.isDryRun) {
this.context.stdout.write(
'\n🚫 The flare files were not sent because the command was executed in dry run mode.'
)
this.context.stdout.write(outputMsg)
return 0
}
// Confirm before sending
this.context.stdout.write('\n')
const confirmSendFiles = await requestConfirmation(
'Are you sure you want to send the flare file to Datadog Support?',
false
)
if (!confirmSendFiles) {
this.context.stdout.write('\n🚫 The flare files were not sent based on your selection.')
this.context.stdout.write(outputMsg)
return 0
}
// Zip folder
const zipPath = path.join(rootFolderPath, FLARE_ZIP_FILE_NAME)
await zipContents(rootFolderPath, zipPath)
// Send to Datadog
this.context.stdout.write(chalk.bold('\n🚀 Sending to Datadog Support...\n'))
await sendToDatadog(zipPath, this.caseId!, this.email!, this.apiKey!, rootFolderPath)
this.context.stdout.write(chalk.bold('\n✅ Successfully sent flare file to Datadog Support!\n'))
// Delete contents
deleteFolder(rootFolderPath)
} catch (err) {
if (err instanceof Error) {
this.context.stderr.write(helpersRenderer.renderError(err.message))
}
return 1
}
return 0
}
}
/**
* Check if the user is authenticated with GCP.
* @returns true if the user is authenticated, false otherwise
*/
export const checkAuthentication = async () => {
const auth = new GoogleAuth()
try {
await auth.getApplicationDefault()
return true
} catch (_) {
return false
}
}
/**
* Call the google-cloud run sdk to get the configuration
* for the given service.
* @param runClient the google-cloud run sdk client
* @param serviceName the name of the service
* @param projectName the project where the service is deployed
* @param region the region where the service is deployed
* @returns the configuration for the given service
*/
export const getCloudRunServiceConfig = async (
runClient: ServicesClient,
serviceName: string,
projectName: string,
region: string
) => {
const request = {
name: runClient.servicePath(projectName, region, serviceName),
}
const [response] = await runClient.getService(request)
return response
}
/**
* Masks environment variables in a Cloud Run service configuration.
* Makes a copy as to not modify the config in place.
* @param config
* @returns masked config
*/
export const maskConfig = (config: any) => {
// We stringify and parse again to make a deep copy
const configCopy: IService = JSON.parse(JSON.stringify(config))
const containers = configCopy.template?.containers
if (!containers) {
return configCopy
}
for (const container of containers) {
const env = container.env ?? []
for (const envVar of env) {
const name = envVar.name
const val = envVar.value
if (!name || !val) {
continue
}
if (!SKIP_MASKING_CLOUDRUN_ENV_VARS.has(name)) {
envVar.value = maskString(val)
}
}
}
return configCopy
}
/**
* Summarizes the Cloud Run config as to not flood the terminal
* @param config
* @returns a summarized config
*/
export const summarizeConfig = (config: IService) => {
const summarizedConfig: any = {}
summarizedConfig.name = config.name
summarizedConfig.uid = config.uid
summarizedConfig.uri = config.uri
// Get env vars and image for each containers
const template = config.template
if (template) {
const summarizedContainers: IContainer[] = []
const containers = template.containers ?? []
containers.forEach((container) => {
const summarizedContainer: any = {}
summarizedContainer.env = container.env
summarizedContainer.image = container.image
summarizedContainers.push(summarizedContainer)
})
summarizedConfig.containers = summarizedContainers
}
return summarizedConfig
}
/**
* Gets recent logs
* @param logClient Logging client
* @param serviceId
* @param location
* @param startMillis start time in milliseconds or undefined if no start time is specified
* @param endMillis end time in milliseconds or undefined if no end time is specified
* @param severityFilter if included, adds the string to the filter
* @returns array of logs as CloudRunLog interfaces
*/
export const getLogs = async (
logClient: Logging,
serviceId: string,
location: string,
startMillis?: number,
endMillis?: number,
severityFilter?: string
) => {
const logs: CloudRunLog[] = []
// Default to the recent logs using MAX_LOG_AGE_MINUTES
const date = new Date()
date.setMinutes(date.getMinutes() - MAX_LOG_AGE_MINUTES)
let startDate: string = date.toISOString()
let endDate: string = new Date().toISOString() // Current time
// If startMillis and endMillis are provided, use them to set the date range
if (startMillis && endMillis) {
startDate = new Date(startMillis).toISOString()
endDate = new Date(endMillis).toISOString()
}
// Query options
let filter = `resource.labels.service_name="${serviceId}" AND resource.labels.location="${location}" AND timestamp>="${startDate}" AND timestamp<="${endDate}" AND (textPayload:* OR httpRequest:*)`
// We only want to include logs with a textPayload or logs that were an HTTP request.
// Any other logs are just audit logs which are spammy and don't have any relevant information.
filter += severityFilter ?? ''
const options = {
filter,
orderBy: FILTER_ORDER,
pageSize: MAX_LOGS,
}
const [entries] = await logClient.getEntries(options)
for (const entry of entries) {
let msg = ''
if (entry.metadata.textPayload) {
msg = entry.metadata.textPayload
}
if (entry.metadata.httpRequest) {
const request = entry.metadata.httpRequest
const status = request.status ?? ''
let ms = 'unknown'
const latency = request.latency
if (latency) {
ms = (Number(latency.seconds) * 1000 + Math.round(Number(latency.nanos) / 1000000)).toString()
}
const bytes = formatBytes(Number(request.responseSize))
const method = request.requestMethod ?? ''
const requestUrl = request.requestUrl ?? ''
msg += `${method} ${status}. responseSize: ${bytes}. latency: ${ms} ms. requestUrl: ${requestUrl}`
}
const log: CloudRunLog = {
severity: entry.metadata.severity?.toString() ?? '',
timestamp: entry.metadata.timestamp?.toString() ?? '',
logName: entry.metadata.logName ?? '',
message: `"${msg}"`,
}
logs.push(log)
}
return logs
}
/**
* Save logs in a CSV format
* @param logs array of logs stored as CloudRunLog interfaces
* @param filePath path to save the CSV file
*/
export const saveLogsFile = (logs: CloudRunLog[], filePath: string) => {
const rows = [['severity', 'timestamp', 'logName', 'message']]
logs.forEach((log) => {
const severity = `"${log.severity}"`
const timestamp = `"${log.timestamp}"`
const logName = `"${log.logName}"`
const logMessage = `"${log.message}"`
rows.push([severity, timestamp, logName, logMessage])
})
const data = rows.join('\n')
writeFile(filePath, data)
}
/**
* Gets recent revisions for a cloud-run service
* @param service
* @param location
* @param project
* @returns a string array of recent revisions and their deployment timestamp
*/
export const getRecentRevisions = async (service: string, location: string, project: string) => {
const client = new RevisionsClient()
const request = {
parent: client.servicePath(project, location, service),
}
// listRevisions() returns a tuple [Revision[], Request, Response],
// so we index the first element to get the array of revisions
const revisions = (await client.listRevisions(request))[0]
const revisionTimestampStrings: string[] = []
let counter = 1
for (const entry of revisions) {
const fullName = entry.name
const timestamp = entry.createTime
if (fullName && timestamp) {
// Get the revision name
const nameSplit = fullName.split('/')
const revisionName = nameSplit[nameSplit.length - 1]
// Format the timestamp by first converting seconds/nanos to milliseconds, then using `new Date()`
const milliseconds = Number(timestamp.seconds ?? 0) * 1000
const timestampString = new Date(milliseconds).toISOString().replace('T', ' ').replace('Z', '').slice(0, -4) // Chop off the milliseconds, which will always be .000
revisionTimestampStrings.push(`\`${revisionName}\` Deployed on ${timestampString}`)
}
// Stop iterating once we reach MAX_REVISIONS
counter += 1
if (counter > MAX_REVISIONS) {
break
}
}
return revisionTimestampStrings
}
/**
* Generate the insights file
* @param insightsFilePath path to the insights file
* @param isDryRun whether or not this is a dry run
* @param config Cloud run service configuration
* @param service
* @param location
* @param project
* @param revisions a string array of recent revisions
*/
export const generateInsightsFile = (
insightsFilePath: string,
isDryRun: boolean,
config: IService,
service: string,
location: string,
project: string,
revisions: string[]
) => {
const lines: string[] = []
// Header
lines.push('# Flare Insights')
lines.push('\n_Autogenerated file from `cloud-run flare`_ ')
if (isDryRun) {
lines.push('_This command was run in dry mode._')
}
// Cloud Run Service Configuration
lines.push('\n## Cloud Run Service Configuration')
lines.push(`**Service Name**: \`${service}\` `)
lines.push(`**Location**: \`${location}\` `)
lines.push(`**Project**: \`${project}\` `)
const description = config.description
if (description && description.length > 0) {
lines.push(`**Description**: \`${description}\` `)
}
lines.push(`**URI**: \`${config.uri ?? ''}\``)
// Environment variables
const containers = config.template?.containers ?? []
for (const container of containers) {
// We want to separate environment variables by container if there are multiple containers
// We can use the container image to uniquely identify each container
lines.push(`\n**Environment Variables** (${container.image ?? 'unknown image'}):`)
const envVars = new Map<string, string>()
for (const envVar of container.env ?? []) {
const name = envVar.name
const value = envVar.value
if (name && value) {
envVars.set(name, value)
}
}
if (envVars.size === 0) {
lines.push('- No environment variables found.')
}
for (const [key, value] of envVars) {
lines.push(`- \`${key}\`: \`${value}\``)
}
}
// Labels
lines.push('\n**Labels**:')
const labels = config.labels ?? {}
const entries = Object.entries(labels)
if (entries.length === 0) {
lines.push('- No labels found.')
}
for (const [key, value] of entries) {
lines.push(`- \`${key}\`: \`${value}\``)
}
// Recent revisions
if (revisions.length > 0) {
lines.push('\n**Recent Revisions**:')
for (const revision of revisions) {
lines.push(`- ${revision}`)
}
}
// CLI Insights
lines.push('\n ## CLI')
lines.push(`**Run Location**: \`${process.cwd()}\` `)
lines.push(`**CLI Version**: \`${version}\` `)
const timeString = new Date().toISOString().replace('T', ' ').replace('Z', '') + ' UTC'
lines.push(`**Timestamp**: \`${timeString}\` `)
writeFile(insightsFilePath, lines.join('\n'))
}