Skip to content

Commit 80519fa

Browse files
committed
refactor metrics
1 parent 6644f4e commit 80519fa

1 file changed

Lines changed: 8 additions & 18 deletions

File tree

lambdas/api-handler/src/handlers/get-mi.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,30 @@ export default function createGetMIHandler(deps: Deps): APIGatewayProxyHandler {
4444
});
4545

4646
// metric with count 1 specifying the supplier
47-
const dimensions: Record<string, string> = { supplier: supplierId };
48-
const metric: MetricEntry = {
49-
key: MetricStatus.Success,
50-
value: 1,
51-
unit: Unit.Count,
52-
};
53-
let emf = buildEMFObject("getMi", dimensions, metric);
54-
deps.logger.info(emf);
47+
const dimensions: Record<string, string> = {supplierId: supplierId};
48+
emitMetric("getMi", dimensions, deps.logger, MetricStatus.Success, 1);
5549

5650
// metric displaying the type/number of lineItems posted per supplier
5751
dimensions.lineItem = result.data.attributes.lineItem;
58-
metric.key = "LineItem per supplier";
59-
metric.value = result.data.attributes.quantity;
60-
emf = buildEMFObject("getMi", dimensions, metric);
61-
deps.logger.info(emf);
52+
emitMetric("getMi", dimensions, deps.logger, "LineItem per supplier", result.data.attributes.quantity);
6253

6354
return {
6455
statusCode: 200,
6556
body: JSON.stringify(result, null, 2),
6657
};
6758
} catch (error) {
68-
emitErrorMetric(supplierId, deps.logger);
59+
emitMetric("getMi", {supplierId: supplierId}, deps.logger, MetricStatus.Failure, 1);
6960
return processError(error, commonIds.value.correlationId, deps.logger);
7061
}
7162
};
7263
}
7364

74-
function emitErrorMetric(supplierId: string, logger: pino.Logger) {
75-
const dimensions: Record<string, string> = { supplier: supplierId };
65+
function emitMetric(source: string, dimensions: Record<string, string>, logger: pino.Logger, key: string, value: number){
7666
const metric: MetricEntry = {
77-
key: MetricStatus.Failure,
78-
value: 1,
67+
key: key,
68+
value: value,
7969
unit: Unit.Count,
8070
};
81-
const emf = buildEMFObject("getMi", dimensions, metric);
71+
const emf = buildEMFObject(source, dimensions, metric);
8272
logger.info(emf);
8373
}

0 commit comments

Comments
 (0)