Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0c1c0b3
chore(cli): telemetry client
kaizencc Jun 9, 2025
2fe74df
docs
kaizencc Jun 9, 2025
c6bf5f4
wip
kaizencc Jun 13, 2025
e91e7e3
use interfaces and fix tests
kaizencc Jun 13, 2025
0d33ac5
add schema and pr feedback
kaizencc Jun 16, 2025
c380240
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jun 18, 2025
e51d30d
change to parsed url
kaizencc Jun 18, 2025
9d980d5
readonly
kaizencc Jun 18, 2025
9126147
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jun 19, 2025
ab560fd
small change
kaizencc Jun 19, 2025
4e361ae
retries implemented, not yet tested
kaizencc Jun 19, 2025
69e63cd
add commented out test
kaizencc Jun 19, 2025
8760b44
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jun 21, 2025
3dbbe62
add proxy support, better retries, test succeeds
kaizencc Jun 25, 2025
feaf376
lint
kaizencc Jun 25, 2025
83ebd08
lints
kaizencc Jun 25, 2025
56f23e5
force not ci in tests
kaizencc Jun 26, 2025
079bbc5
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jun 26, 2025
635d6b7
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jun 30, 2025
18d9349
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jul 1, 2025
ea4720c
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jul 2, 2025
4bf3be9
Merge branch 'main' into conroy/basic-telemetry-client
kaizencc Jul 2, 2025
359a442
telemetry interface includes flush
kaizencc Jul 2, 2025
54109d6
rename
kaizencc Jul 2, 2025
334b9dd
lint
kaizencc Jul 2, 2025
25dec4a
pr feedback
kaizencc Jul 2, 2025
5628ed9
trace
kaizencc Jul 2, 2025
cc8c6dd
pr feedback
kaizencc Jul 2, 2025
c7d365a
renames
kaizencc Jul 3, 2025
ab8bc32
fix tests
kaizencc Jul 3, 2025
0365976
remove url file
kaizencc Jul 3, 2025
8fdd0e9
add test on errors
kaizencc Jul 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
retries implemented, not yet tested
  • Loading branch information
kaizencc committed Jun 19, 2025
commit 4e361aebb75366935a9cb74e929ef897ab13c6e4
80 changes: 60 additions & 20 deletions packages/aws-cdk/lib/cli/telemetry/endpoint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { IoHelper } from '../../api-private';
import type { IIoHost } from '../io-host';
import type { ITelemetryClient } from './client-interface';
import type { TelemetrySchema } from './schema';
import { IncomingMessage } from 'http';

const REQUEST_DEADLINE_MS = 5_000;

const REQUEST_ATTEMPT_TIMEOUT_MS = 2_000;

/**
* Properties for the Endpoint Telemetry Client
Expand Down Expand Up @@ -56,7 +61,7 @@ export class EndpointTelemetryClient implements ITelemetryClient {
}

try {
await this.https(this.endpoint, this.events, this.ioHost);
await this.https(this.endpoint, this.events);

// Clear the events array after successful output
this.events = [];
Expand All @@ -69,11 +74,41 @@ export class EndpointTelemetryClient implements ITelemetryClient {
private async https(
url: UrlWithStringQuery,
body: TelemetrySchema[],
ioHost: IoHelper,
): Promise<void> {
// TODO: sigv4 authentication
// TODO: Handle retries and stuff
return requestPromise(url, body, ioHost);
const deadline = Date.now() + REQUEST_DEADLINE_MS;
let maxDelay = 100;
while (true) {
try {
const res = await requestPromise(url, body);
// eslint-disable-next-line
console.log(res);

if (res.statusCode == null) {
throw new RetryableError('No status code available');
}

// Server errors. We can't know whether these are really retryable but we usually pretend that they are.
if (res.statusCode >= 500 && res.statusCode < 600) {
throw new RetryableError(`HTTP ${res.statusCode} ${res.statusMessage}`);
}

// Permanent (client) errors:
if (res.statusCode >= 400 && res.statusCode < 500) {
throw new Error(`HTTP ${res.statusCode} ${res.statusMessage}`);
}

return;
} catch (e: any) {
if (Date.now() > deadline || !isRetryableError(e)) {
this.ioHost.defaults.debug(`Fatal Telemetry Error: POST ${url}: ${e}`);
return;
}
this.ioHost.defaults.debug(`Retryable Telemetry Error: POST ${url}: ${e}`);

await sleep(Math.floor(Math.random() * maxDelay));
maxDelay *= 2;
}
}
}
}

Expand All @@ -82,10 +117,9 @@ export class EndpointTelemetryClient implements ITelemetryClient {
*/
function requestPromise(
url: UrlWithStringQuery,
data: any, // to be schema
ioHost: IoHelper,
data: TelemetrySchema[],
) {
return new Promise<void>((resolve) => {
return new Promise<IncomingMessage>((ok, ko) => {
const payload: string = JSON.stringify(data);
const req = request({
hostname: url.hostname,
Expand All @@ -96,19 +130,25 @@ function requestPromise(
'content-type': 'application/json',
'content-length': payload.length,
},
});
// TODO: retryable errors
req.on('error', async (e: any) => {
await ioHost.defaults.warn(`Telemetry endpoint request failed: ${e.message}`);
});
req.setTimeout(2000, () => {
// 2 seconds
resolve();
});
timeout: REQUEST_ATTEMPT_TIMEOUT_MS,
}, ok);

req.write(payload);
req.end(() => {
resolve();
req.on('error', ko);
req.on('timeout', () => {
const error = new RetryableError(`Timeout after ${REQUEST_ATTEMPT_TIMEOUT_MS}ms, aborting request`);
req.destroy(error);
});

req.end(payload);
});
}

class RetryableError extends Error {}

function isRetryableError(e: Error): boolean {
return e instanceof RetryableError || (e as any).code === 'ECONNRESET';
}

async function sleep(ms: number) {
return new Promise((ok) => setTimeout(ok, ms));
}
19 changes: 11 additions & 8 deletions packages/aws-cdk/test/cli/telemetry/endpoint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ describe('EndpointTelemetryClient', () => {
'content-type': 'application/json',
'content-length': expectedPayload.length,
},
});
timeout: 2000,
}, expect.anything());

expect(mockRequest.write).toHaveBeenCalledWith(expectedPayload);
expect(mockRequest.end).toHaveBeenCalled();
expect(mockRequest.end).toHaveBeenCalledWith(expectedPayload);
});

test('silently catches request errors', async () => {
Expand Down Expand Up @@ -140,10 +140,10 @@ describe('EndpointTelemetryClient', () => {
'content-type': 'application/json',
'content-length': expectedPayload.length,
},
});
timeout: 2000,
}, expect.anything());

expect(mockRequest.write).toHaveBeenCalledWith(expectedPayload);
expect(mockRequest.end).toHaveBeenCalled();
expect(mockRequest.end).toHaveBeenCalledWith(expectedPayload);
});

test('flush clears events cache', async () => {
Expand Down Expand Up @@ -171,8 +171,10 @@ describe('EndpointTelemetryClient', () => {
headers: {
'content-type': 'application/json',
'content-length': expectedPayload1.length,

},
});
timeout: 2000,
}, expect.anything());

const expectedPayload2 = JSON.stringify([testEvent2]);
expect(https.request).toHaveBeenCalledWith({
Expand All @@ -184,7 +186,8 @@ describe('EndpointTelemetryClient', () => {
'content-type': 'application/json',
'content-length': expectedPayload2.length,
},
});
timeout: 2000,
}, expect.anything());
});

test('flush is called every 30 seconds', async () => {
Expand Down
Loading