Skip to content

Commit 7dad11e

Browse files
authored
fix(metric): avoid awaiting the metric report call (#1285)
## Proposed change Avoid awaiting the metric report call ## Related issues - 🐛 Fixes #1201 <!-- Please make sure to follow the contributing guidelines on https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
2 parents 2c795b1 + badf9ff commit 7dad11e

File tree

3 files changed

+77
-79
lines changed

3 files changed

+77
-79
lines changed

packages/@o3r/telemetry/src/builders/index.ts

+30-33
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import type {
44
} from '@angular-devkit/architect';
55
import { getEnvironmentInfo } from '../environment/index';
66
import { performance } from 'node:perf_hooks';
7-
import { BuilderMetricData, sendData as defaultSendData } from '../sender';
7+
import { BuilderMetricData, sendData as defaultSendData, type SendDataFn } from '../sender';
88

99
type BuilderWrapperFn<S, O extends BuilderOutput = BuilderOutput> =
1010
(opts: S, ctx: BuilderContext) => O | Promise<O>;
1111

12-
type SendDataFn = (data: BuilderMetricData, logger?: { error: (msg: string) => void } | undefined) => Promise<void>;
13-
1412
/**
1513
* Type of a function that wraps a builder
1614
*/
@@ -20,11 +18,12 @@ export type BuilderWrapper = <S, O extends BuilderOutput = BuilderOutput>
2018
/**
2119
* Wrapper method of a builder to retrieve some metrics around the builder run
2220
* @param builderFn
21+
* @param sendData
2322
*/
2423
export const createBuilderWithMetrics: BuilderWrapper = (builderFn, sendData = defaultSendData) =>
2524
async (options, context) => {
2625
const startTime = Math.floor(performance.now());
27-
let error;
26+
let error: any;
2827
try {
2928
const result = await builderFn(options, context);
3029
return result;
@@ -35,38 +34,36 @@ export const createBuilderWithMetrics: BuilderWrapper = (builderFn, sendData = d
3534
throw err;
3635
}
3736
finally {
38-
try {
39-
const endTime = Math.floor(performance.now());
40-
const duration = endTime - startTime;
41-
// context.builder.builderName does not contain the package name
42-
const builderName = context.builder.name as string;
43-
context.logger.info(`${builderName} run in ${duration}ms`);
44-
const environment = getEnvironmentInfo();
45-
const data: BuilderMetricData = {
46-
environment,
47-
duration,
48-
builder: {
49-
name: builderName,
50-
options,
51-
...(context.target
52-
? {
53-
target: {
54-
name: context.target.target,
55-
projectName: context.target.project,
56-
configuration: context.target.configuration
57-
}
58-
} : {}
59-
)
60-
},
61-
error
62-
};
63-
context.logger.debug(JSON.stringify(data, null, 2));
64-
await sendData(data, context.logger);
65-
} catch (e: any) {
37+
const endTime = Math.floor(performance.now());
38+
const duration = endTime - startTime;
39+
// context.builder.builderName does not contain the package name
40+
const builderName = context.builder.name as string;
41+
context.logger.info(`${builderName} run in ${duration}ms`);
42+
const environment = getEnvironmentInfo();
43+
const data: BuilderMetricData = {
44+
environment,
45+
duration,
46+
builder: {
47+
name: builderName,
48+
options,
49+
...(context.target
50+
? {
51+
target: {
52+
name: context.target.target,
53+
projectName: context.target.project,
54+
configuration: context.target.configuration
55+
}
56+
} : {}
57+
)
58+
},
59+
error
60+
};
61+
context.logger.debug(JSON.stringify(data, null, 2));
62+
void sendData(data, context.logger).catch((e) => {
6663
// Do not throw error if we don't manage to collect data
6764
const err = (e instanceof Error ? e : new Error(error));
6865
context.logger.error(err.stack || err.toString());
69-
}
66+
});
7067
}
7168
};
7269

packages/@o3r/telemetry/src/schematics/index.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { callRule, Rule } from '@angular-devkit/schematics';
22
import { performance } from 'node:perf_hooks';
33
import { lastValueFrom } from 'rxjs';
44
import { getEnvironmentInfo } from '../environment/index';
5-
import { sendData as defaultSendData, SchematicMetricData } from '../sender';
5+
import { sendData as defaultSendData, SchematicMetricData, type SendDataFn } from '../sender';
66

7-
type SchematicWrapperFn<S> = (opts: S) => Rule;
8-
9-
type SendDataFn = (data: SchematicMetricData, logger?: { error: (msg: string) => void } | undefined) => Promise<void>;
7+
/**
8+
* Factory of the schematic to wrap
9+
* @param options Options of the factory
10+
*/
11+
type SchematicWrapperFn<S> = (options: S) => Rule;
1012

1113
/**
1214
* Type of a function that wraps a schematic
@@ -20,7 +22,7 @@ export type SchematicWrapper = <S>(schematicFn: SchematicWrapperFn<S>, sendData?
2022
export const createSchematicWithMetrics: SchematicWrapper =
2123
(schematicFn, sendData = defaultSendData) => (options) => async (tree, context) => {
2224
const startTime = Math.floor(performance.now());
23-
let error;
25+
let error: any;
2426
try {
2527
const rule = schematicFn(options);
2628
await lastValueFrom(callRule(rule, tree, context));
@@ -31,28 +33,26 @@ export const createSchematicWithMetrics: SchematicWrapper =
3133
throw err;
3234
}
3335
finally {
34-
try {
35-
const endTime = Math.floor(performance.now());
36-
const duration = endTime - startTime;
37-
const environment = getEnvironmentInfo();
38-
const schematic = {
39-
name: `${context.schematic.description.collection.name}:${context.schematic.description.name}`,
40-
options,
41-
interactive: context.interactive
42-
};
43-
const data: SchematicMetricData = {
44-
environment,
45-
schematic,
46-
duration,
47-
error
48-
};
49-
context.logger.debug(JSON.stringify(data, null, 2));
50-
await sendData(data, context.logger);
51-
} catch (e: any) {
36+
const endTime = Math.floor(performance.now());
37+
const duration = endTime - startTime;
38+
const environment = getEnvironmentInfo();
39+
const schematic = {
40+
name: `${context.schematic.description.collection.name}:${context.schematic.description.name}`,
41+
options,
42+
interactive: context.interactive
43+
};
44+
const data: SchematicMetricData = {
45+
environment,
46+
schematic,
47+
duration,
48+
error
49+
};
50+
context.logger.debug(JSON.stringify(data, null, 2));
51+
void sendData(data, context.logger).catch((e) => {
5252
// Do not throw error if we don't manage to collect data
5353
const err = (e instanceof Error ? e : new Error(error));
5454
context.logger.error(err.stack || err.toString());
55-
}
55+
});
5656
}
5757
};
5858

packages/@o3r/telemetry/src/sender/index.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,28 @@ export interface SchematicMetricData extends BaseMetricData {
4646
export type MetricData = BuilderMetricData | SchematicMetricData;
4747

4848
/**
49-
* Send metric to a server
50-
* @param data
51-
* @param logger
49+
* Function sending metrics to the server
50+
* @param data Metrics to report
51+
* @param logger Optional logger to provide to the function
5252
*/
53-
export const sendData = async (data: MetricData, logger?: { error: (msg: string) => void }) => {
54-
try {
55-
const message = JSON.stringify(data);
56-
const body = JSON.stringify({
57-
messages: [{
58-
applicationName: 'OTTER',
59-
message
60-
}]
61-
});
62-
await fetch('https://uat.digital-logging.saas.amadeus.com/postUILogs', {
63-
method: 'POST',
64-
body
65-
});
66-
} catch (e: any) {
67-
// Do not throw error if we don't manage to send data to a server
68-
const err = e instanceof Error ? e : new Error(e);
69-
// eslint-disable-next-line no-console
70-
(logger || console).error(err.stack || err.toString());
71-
}
53+
export type SendDataFn = (data: MetricData, logger?: { error: (msg: string) => void } | undefined) => Promise<void>;
54+
55+
/**
56+
* Send metric to a Amadeus Log Server
57+
* @param data Metrics to report
58+
* @param _logger Optional logger to provide to the function
59+
* @param _logger.error
60+
*/
61+
export const sendData: SendDataFn = async (data: MetricData, _logger?: { error: (msg: string) => void }) => {
62+
const message = JSON.stringify(data);
63+
const body = JSON.stringify({
64+
messages: [{
65+
applicationName: 'OTTER',
66+
message
67+
}]
68+
});
69+
await fetch('https://uat.digital-logging.saas.amadeus.com/postUILogs', {
70+
method: 'POST',
71+
body
72+
});
7273
};

0 commit comments

Comments
 (0)