generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 52
chore(cli): telemetry sink #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 26 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
0c1c0b3
chore(cli): telemetry client
kaizencc 2fe74df
docs
kaizencc c6bf5f4
wip
kaizencc e91e7e3
use interfaces and fix tests
kaizencc 0d33ac5
add schema and pr feedback
kaizencc c380240
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc e51d30d
change to parsed url
kaizencc 9d980d5
readonly
kaizencc 9126147
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc ab560fd
small change
kaizencc 4e361ae
retries implemented, not yet tested
kaizencc 69e63cd
add commented out test
kaizencc 8760b44
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc 3dbbe62
add proxy support, better retries, test succeeds
kaizencc feaf376
lint
kaizencc 83ebd08
lints
kaizencc 56f23e5
force not ci in tests
kaizencc 079bbc5
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc 635d6b7
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc 18d9349
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc ea4720c
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc 4bf3be9
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc 359a442
telemetry interface includes flush
kaizencc 54109d6
rename
kaizencc 334b9dd
lint
kaizencc 25dec4a
pr feedback
kaizencc 5628ed9
trace
kaizencc cc8c6dd
pr feedback
kaizencc c7d365a
renames
kaizencc ab8bc32
fix tests
kaizencc 0365976
remove url file
kaizencc 8fdd0e9
add test on errors
kaizencc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,140 @@ | ||
| import type { IncomingMessage } from 'http'; | ||
| import type { Agent } from 'https'; | ||
| import { request } from 'https'; | ||
| import type { UrlWithStringQuery } from 'url'; | ||
| import { ToolkitError } from '@aws-cdk/toolkit-lib'; | ||
| import { IoHelper } from '../../api-private'; | ||
| import type { IIoHost } from '../io-host'; | ||
| import type { TelemetrySchema } from './schema'; | ||
| import type { ITelemetrySink } from './sink-interface'; | ||
|
|
||
| const REQUEST_ATTEMPT_TIMEOUT_MS = 2_000; | ||
|
|
||
| /** | ||
| * Properties for the Endpoint Telemetry Client | ||
| */ | ||
| export interface EndpointTelemetryClientProps { | ||
| /** | ||
| * The external endpoint to hit | ||
| */ | ||
| readonly endpoint: UrlWithStringQuery; | ||
|
|
||
| /** | ||
| * Where messages are going to be sent | ||
| */ | ||
| readonly ioHost: IIoHost; | ||
|
|
||
| /** | ||
| * The agent responsible for making the network requests. | ||
| * | ||
| * Use this to set up a proxy connection. | ||
| * | ||
| * @default - Uses the shared global node agent | ||
| */ | ||
| readonly agent?: Agent; | ||
| } | ||
|
|
||
| /** | ||
| * The telemetry client that hits an external endpoint. | ||
| */ | ||
| export class EndpointTelemetryClient implements ITelemetrySink { | ||
| private events: TelemetrySchema[] = []; | ||
| private endpoint: UrlWithStringQuery; | ||
| private ioHost: IoHelper; | ||
| private agent?: Agent; | ||
|
|
||
| public constructor(props: EndpointTelemetryClientProps) { | ||
| this.endpoint = props.endpoint; | ||
| this.ioHost = IoHelper.fromActionAwareIoHost(props.ioHost); | ||
| this.agent = props.agent; | ||
|
|
||
| // Batch events every 30 seconds | ||
| setInterval(() => this.flush(), 30000).unref(); | ||
| } | ||
|
|
||
| /** | ||
| * Add an event to the collection. | ||
| */ | ||
| public async emit(event: TelemetrySchema): Promise<void> { | ||
| try { | ||
| this.events.push(event); | ||
| } catch (e: any) { | ||
| // Never throw errors, just log them via ioHost | ||
| await this.ioHost.defaults.warn(`Failed to add telemetry event: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| public async flush(): Promise<void> { | ||
| if (this.events.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const res = await this.https(this.endpoint, this.events); | ||
|
|
||
| // Clear the events array after successful output | ||
| if (res) { | ||
| this.events = []; | ||
| } | ||
| } catch (_e: any) { | ||
| // Never throw errors, and error message was previously logged to ioHost | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if telemetry successfully posted, false otherwise. | ||
| */ | ||
| private async https( | ||
| url: UrlWithStringQuery, | ||
| body: TelemetrySchema[], | ||
| ): Promise<boolean> { | ||
| try { | ||
| const res = await requestPromise(url, body, this.agent); | ||
|
|
||
| // Successfully posted | ||
| if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) { | ||
| return true; | ||
| } | ||
|
|
||
| await this.ioHost.defaults.debug(`Telemetry Unsuccessful: POST ${url.hostname}${url.pathname}: ${res.statusCode}:${res.statusMessage}`); | ||
|
|
||
| return false; | ||
| } catch (e: any) { | ||
| await this.ioHost.defaults.debug(`Telemetry Error: POST ${url.hostname}${url.pathname}: ${JSON.stringify(e)}`); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A Promisified version of `https.request()` | ||
| */ | ||
| function requestPromise( | ||
| url: UrlWithStringQuery, | ||
| data: TelemetrySchema[], | ||
| agent?: Agent, | ||
| ) { | ||
| return new Promise<IncomingMessage>((ok, ko) => { | ||
| const payload: string = JSON.stringify(data); | ||
| const req = request({ | ||
| hostname: url.hostname, | ||
| port: url.port, | ||
| path: url.pathname, | ||
| method: 'POST', | ||
| headers: { | ||
| 'content-type': 'application/json', | ||
| 'content-length': payload.length, | ||
| }, | ||
| agent, | ||
| timeout: REQUEST_ATTEMPT_TIMEOUT_MS, | ||
| }, ok); | ||
|
|
||
| req.on('error', ko); | ||
| req.on('timeout', () => { | ||
| const error = new ToolkitError(`Timeout after ${REQUEST_ATTEMPT_TIMEOUT_MS}ms, aborting request`); | ||
| req.destroy(error); | ||
| }); | ||
|
|
||
| req.end(payload); | ||
| }); | ||
| } |
This file contains hidden or 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,67 @@ | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { ToolkitError, type IIoHost } from '@aws-cdk/toolkit-lib'; | ||
| import type { TelemetrySchema } from './schema'; | ||
| import type { ITelemetrySink } from './sink-interface'; | ||
| import { IoHelper } from '../../api-private'; | ||
|
|
||
| /** | ||
| * Properties for the FileTelemetryClient | ||
| */ | ||
| export interface FileTelemetryClientProps { | ||
| /** | ||
| * Where messages are going to be sent | ||
| */ | ||
| readonly ioHost: IIoHost; | ||
|
|
||
| /** | ||
| * The local file to log telemetry data to. | ||
| */ | ||
| readonly logFilePath: string; | ||
| } | ||
|
|
||
| /** | ||
| * A telemetry client that collects events writes them to a file | ||
| */ | ||
| export class FileTelemetryClient implements ITelemetrySink { | ||
| private ioHelper: IoHelper; | ||
| private logFilePath: string; | ||
|
|
||
| /** | ||
| * Create a new FileTelemetryClient | ||
| */ | ||
| constructor(props: FileTelemetryClientProps) { | ||
| this.ioHelper = IoHelper.fromActionAwareIoHost(props.ioHost); | ||
| this.logFilePath = props.logFilePath; | ||
|
|
||
| if (fs.existsSync(this.logFilePath)) { | ||
| throw new ToolkitError(`Telemetry file already exists at ${this.logFilePath}`); | ||
| } | ||
|
|
||
| // Create the file if necessary | ||
| const directory = path.dirname(this.logFilePath); | ||
| if (!fs.existsSync(directory)) { | ||
| fs.mkdirSync(directory, { recursive: true }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Emit an event. | ||
| */ | ||
| public async emit(event: TelemetrySchema): Promise<void> { | ||
| try { | ||
| // Format the events as a JSON string with pretty printing | ||
| const output = JSON.stringify(event, null, 2); | ||
|
|
||
| // Write to file | ||
| fs.appendFileSync(this.logFilePath, output); | ||
| } catch (e: any) { | ||
| // Never throw errors, just log them via ioHost | ||
| await this.ioHelper.defaults.warn(`Failed to add telemetry event: ${e.message}`); | ||
|
||
| } | ||
| } | ||
|
|
||
| public async flush(): Promise<void> { | ||
| return; | ||
| } | ||
| } | ||
This file contains hidden or 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,48 @@ | ||
| import type { IIoHost } from '@aws-cdk/toolkit-lib'; | ||
| import type { TelemetrySchema } from './schema'; | ||
| import type { ITelemetrySink } from './sink-interface'; | ||
| import { IoHelper } from '../../api-private'; | ||
|
|
||
| /** | ||
| * Properties for the StdoutTelemetryClient | ||
| */ | ||
| export interface IoHostTelemetryClientProps { | ||
| /** | ||
| * Where messages are going to be sent | ||
| */ | ||
| readonly ioHost: IIoHost; | ||
| } | ||
|
|
||
| /** | ||
| * A telemetry client that collects events and flushes them to stdout. | ||
| */ | ||
| export class IoHostTelemetryClient implements ITelemetrySink { | ||
| private ioHelper: IoHelper; | ||
|
|
||
| /** | ||
| * Create a new StdoutTelemetryClient | ||
| */ | ||
| constructor(props: IoHostTelemetryClientProps) { | ||
| this.ioHelper = IoHelper.fromActionAwareIoHost(props.ioHost); | ||
| } | ||
|
|
||
| /** | ||
| * Emit an event | ||
| */ | ||
| public async emit(event: TelemetrySchema): Promise<void> { | ||
| try { | ||
| // Format the events as a JSON string with pretty printing | ||
| const output = JSON.stringify(event, null, 2); | ||
|
|
||
| // Write to IoHost | ||
| await this.ioHelper.defaults.trace(`--- TELEMETRY EVENT ---\n${output}\n-----------------------\n`); | ||
| } catch (e: any) { | ||
| // Never throw errors, just log them via ioHost | ||
| await this.ioHelper.defaults.trace(`Failed to add telemetry event: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| public async flush(): Promise<void> { | ||
| return; | ||
| } | ||
| } |
This file contains hidden or 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,63 @@ | ||
| interface Identifiers { | ||
| readonly cdkCliVersion: string; | ||
| readonly cdkLibraryVersion?: string; | ||
| readonly telemetryVersion: string; | ||
| readonly sessionId: string; | ||
| readonly eventId: string; | ||
| readonly installationId: string; | ||
| readonly timestamp: string; | ||
| readonly accountId?: string; | ||
| readonly region?: string; | ||
| } | ||
|
|
||
| interface Event { | ||
| readonly state: 'ABORTED' | 'FAILED' | 'SUCCEEDED'; | ||
| readonly eventType: string; | ||
kaizencc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| readonly command: { | ||
| readonly path: string[]; | ||
| readonly parameters: string[]; | ||
| readonly config: { [key: string]: any }; | ||
| }; | ||
| } | ||
|
|
||
| interface Environment { | ||
| readonly os: { | ||
| readonly platform: string; | ||
| readonly release: string; | ||
| }; | ||
| readonly ci: boolean; | ||
| readonly nodeVersion: string; | ||
| } | ||
|
|
||
| interface Duration { | ||
| readonly total: number; | ||
| readonly components?: { [key: string]: number }; | ||
| } | ||
|
|
||
| type Counters = { [key: string]: number }; | ||
|
|
||
| interface Error { | ||
| readonly name: string; | ||
| readonly message?: string; // anonymized stack message | ||
| readonly trace?: string; // anonymized stack trace | ||
| readonly logs?: string; // anonymized stack logs | ||
| } | ||
|
|
||
| interface Dependency { | ||
| readonly name: string; | ||
| readonly version: string; | ||
| } | ||
|
|
||
| interface Project { | ||
| readonly dependencies?: Dependency[]; | ||
| } | ||
|
|
||
| export interface TelemetrySchema { | ||
| readonly identifiers: Identifiers; | ||
| readonly event: Event; | ||
| readonly environment: Environment; | ||
| readonly project: Project; | ||
| readonly duration: Duration; | ||
| readonly counters?: Counters; | ||
| readonly error?: Error; | ||
| } | ||
This file contains hidden or 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,20 @@ | ||
| import type { TelemetrySchema } from './schema'; | ||
|
|
||
| /** | ||
| * All Telemetry Clients are Sinks. | ||
| * | ||
| * A telemtry client receives event data via 'emit' | ||
| * and sends batched events via 'flush' | ||
| */ | ||
| export interface ITelemetrySink { | ||
| /** | ||
| * Recieve an event | ||
| */ | ||
| emit(event: TelemetrySchema): Promise<void>; | ||
|
|
||
| /** | ||
| * If the implementer of ITelemetrySink batches events, | ||
| * flush sends the data and clears the cache. | ||
| */ | ||
| flush(): Promise<void>; | ||
| } |
kaizencc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or 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,23 @@ | ||
| import type { UrlWithStringQuery } from 'node:url'; | ||
| import { parse } from 'node:url'; | ||
|
|
||
| let cachedUrl: UrlWithStringQuery; | ||
|
|
||
| let prodUrl: string = ''; // TODO: add when its launched | ||
|
|
||
| /** | ||
| * Usage data tracking service URL | ||
| */ | ||
| export const getUrl = (): UrlWithStringQuery => { | ||
| if (!cachedUrl) { | ||
| cachedUrl = getParsedUrl(); | ||
| } | ||
|
|
||
| return cachedUrl; | ||
| }; | ||
|
|
||
| const getParsedUrl = (): UrlWithStringQuery => { | ||
| return parse( | ||
| process.env.CDK_TELEMETRY_ENDPOINT || prodUrl, | ||
| ); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.