-
Notifications
You must be signed in to change notification settings - Fork 975
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cli): Add OpenTelemetry (#8653)
* Initial OTel kernel for CLI * Add helper functions for telemetry * Add telemetry for info command * Capture and record top level errors * WIP compute of telemetry details in the background * wip changes to the background compute * Instrument CLI commands I'm not in love with this code. Seems difficult to ensure key consisitency but I'm working around the existing code and yargs. * Rework OTel exporting. Switched to use a custom exporter which writes spans to a file. Then have a background job that fires when OTel shuts down to read those saved spans and send them to the collector. This means we have one background job that runs to both compute the telemetry resources and send the data to the collector. * Re-enable existing telemetry and remove error messaging * Revert change to locking.js * Refactor the new telemetry setup Move some files around and rename some. Adds in the missing telemetry fields to the resource. Rewrites the spans to disk with the added resource information for verbose visibility. * Remove stray console logs and remove events from spans. * Produce top level error output and record an error ref. code We will likely have to iterate on how the output looks. * fix experiments and webBundler resource values * Tidy telemetry/index.js * Record top level errors with legacy telemetry also * Use error exit code if it exists I wouldn't have written this myself but I seen it used in the code so I may as well keep using it. * Started to remove the handler level try/catch. Stopped because of some many edge or special cases where the CLI immediately exists with non-zero exit code. * Remove unused imports * Add back try/catch for the lint command * Update test-fixture * Revert and just hardcode the command name * Revert change to vite.config.ts * Add missing otel api dep * Make use of fs-extra json funcs * Replace require with import * Simplify the exitCode setting * Fix telemetry yargs option * Introduce background job helper function The helper ensure's we maintain the spawn options we need to support different platforms. It also ensures the output is written to a log file within the '.redwood' folder. Reworks the new telemetry background process to use this helper. Introduces some relatively verbose output to the send process now that we do not need to be quiet. * lint * refactor telemetry enabling --------- Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
- Loading branch information
1 parent
0e23c6e
commit a87e8a4
Showing
77 changed files
with
1,195 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import opentelemetry, { | ||
SpanStatusCode, | ||
AttributeValue, | ||
Span, | ||
} from '@opentelemetry/api' | ||
|
||
type TelemetryAttributes = { | ||
[key: string]: AttributeValue | ||
} | ||
|
||
/** | ||
* Safely records attributes to the opentelemetry span | ||
* | ||
* @param attributes An object of key-value pairs to be individually recorded as attributes | ||
* @param span An optional span to record the attributes to. If not provided, the current active span will be used | ||
*/ | ||
export function recordTelemetryAttributes( | ||
attributes: TelemetryAttributes, | ||
span?: Span | ||
) { | ||
const spanToRecord = span ?? opentelemetry.trace.getActiveSpan() | ||
if (spanToRecord === undefined) { | ||
return | ||
} | ||
for (const [key, value] of Object.entries(attributes)) { | ||
spanToRecord.setAttribute(key, value) | ||
} | ||
} | ||
|
||
/** | ||
* Safely records an error to the opentelemetry span | ||
* | ||
* @param error An error to record to the span | ||
* @param span An optional span to record the error to. If not provided, the current active span will be used | ||
*/ | ||
export function recordTelemetryError(error: any, span?: Span) { | ||
const spanToRecord = span ?? opentelemetry.trace.getActiveSpan() | ||
if (spanToRecord === undefined) { | ||
return | ||
} | ||
spanToRecord.setStatus({ | ||
code: SpanStatusCode.ERROR, | ||
message: error.toString().split('\n')[0], | ||
}) | ||
spanToRecord.recordException(error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers' | ||
|
||
import { deployBuilder, deployHandler } from './helpers/helpers' | ||
|
||
export const command = 'netlify [...commands]' | ||
export const description = 'Build command for Netlify deploy' | ||
|
||
export const builder = (yargs) => deployBuilder(yargs) | ||
|
||
export const handler = deployHandler | ||
export const handler = (yargs) => { | ||
recordTelemetryAttributes({ | ||
command: ['deploy', 'netlify'].join(' '), | ||
}) | ||
deployHandler(yargs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers' | ||
|
||
import { deployBuilder, deployHandler } from './helpers/helpers' | ||
|
||
export const command = 'vercel [...commands]' | ||
export const description = 'Build command for Vercel deploy' | ||
|
||
export const builder = (yargs) => deployBuilder(yargs) | ||
|
||
export const handler = deployHandler | ||
export const handler = (yargs) => { | ||
recordTelemetryAttributes({ | ||
command: ['deploy', 'vercel'].join(' '), | ||
build: yargs.build, | ||
prisma: yargs.prisma, | ||
dataMigrate: yargs.dataMigrate, | ||
}) | ||
deployHandler(yargs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.