@@ -19,7 +19,7 @@ import {
1919 LetterRequestPreparedEventV2 ,
2020} from "@nhsdigital/nhs-notify-event-schemas-letter-rendering" ;
2121import z from "zod" ;
22- import { Unit , metricScope } from "aws-embedded-metrics" ;
22+ import { MetricsLogger , Unit , metricScope } from "aws-embedded-metrics" ;
2323import { Deps } from "../config/deps" ;
2424
2525type SupplierSpec = { supplierId : string ; specId : string } ;
@@ -153,58 +153,70 @@ async function runUpsert(
153153 throw new Error ( "No matching schema for received message" ) ;
154154}
155155
156+ async function emitMetrics (
157+ metrics : MetricsLogger ,
158+ successMetrics : Map < string , number > ,
159+ failMetrics : Map < string , number > ,
160+ ) {
161+ metrics . setNamespace ( process . env . AWS_LAMBDA_FUNCTION_NAME || `upsertLetter` ) ;
162+ // emit success metrics
163+ for ( const [ supplier , count ] of Object . entries ( successMetrics ) ) {
164+ metrics . putDimensions ( {
165+ Supplier : supplier ,
166+ } ) ;
167+ metrics . putMetric ( "MessagesProcessed" , count , Unit . Count ) ;
168+ }
169+ // emit failure metrics
170+ for ( const [ supplier , count ] of Object . entries ( failMetrics ) ) {
171+ metrics . putDimensions ( {
172+ Supplier : supplier ,
173+ } ) ;
174+ metrics . putMetric ( "MessageFailed" , count , Unit . Count ) ;
175+ }
176+ }
177+
156178export default function createUpsertLetterHandler ( deps : Deps ) : SQSHandler {
157179 return metricScope ( ( metrics ) => {
158180 return async ( event : SQSEvent ) => {
159- console . log (
160- "the environment variables:" ,
161- JSON . stringify ( process . env , null , 2 ) ,
162- ) ;
163- console . log ( "The SQSEvent:" , event ) ;
164181 const batchItemFailures : SQSBatchItemFailure [ ] = [ ] ;
165- metrics . setNamespace (
166- process . env . AWS_LAMBDA_FUNCTION_NAME || "upsertletter" ,
167- ) ;
182+ const perSupplierSuccess : Map < string , number > = new Map < string , number > ( ) ;
183+ const perSupplierFailure : Map < string , number > = new Map < string , number > ( ) ;
168184
169185 const tasks = event . Records . map ( async ( record ) => {
186+ let supplier = "unknown" ;
170187 try {
171188 const message : string = parseSNSNotification ( record ) ;
172- console . log ( "the message:" , message ) ;
173-
174189 const snsEvent = JSON . parse ( message ) ;
175- console . log ( "the snsEvent:" , snsEvent ) ;
176-
190+ supplier = snsEvent . data . supplierId || "unknown" ;
177191 const letterEvent : unknown = removeEventBridgeWrapper ( snsEvent ) ;
178-
179192 const type = getType ( letterEvent ) ;
180193
181194 const operation = getOperationFromType ( type ) ;
182- console . log ( "letterEvent:" , letterEvent ) ;
183- console . log ( "operation:" , operation ) ;
184195 metrics . putDimensions ( {
185- // eslint-disable-next-line sonarjs/pseudo-random
186- OddOrEven : `${ Math . floor ( Math . random ( ) * 10 ) % 2 } ` ,
196+ supplier : snsEvent . data . supplierId || "unknown" ,
187197 } ) ;
188198
189199 await runUpsert ( operation , letterEvent , deps ) ;
190- metrics . setProperty ( "operation" , operation . name ) ;
191200
192- metrics . putMetric ( "MessagesProcessed" , 1 , Unit . Count ) ;
201+ perSupplierSuccess . set (
202+ supplier ,
203+ ( perSupplierSuccess . get ( supplier ) || 0 ) + 1 ,
204+ ) ;
193205 } catch ( error ) {
194206 deps . logger . error (
195207 { err : error , message : record . body } ,
196208 `Error processing upsert of record ${ record . messageId } ` ,
197209 ) ;
198- metrics . putDimensions ( {
199- // eslint-disable-next-line sonarjs/pseudo-random
200- OddOrEven : `${ Math . floor ( Math . random ( ) * 10 ) % 2 } ` ,
201- } ) ;
202- metrics . putMetric ( "MessageFailed" , 1 , Unit . Count ) ;
210+ perSupplierFailure . set (
211+ supplier ,
212+ ( perSupplierFailure . get ( supplier ) || 0 ) + 1 ,
213+ ) ;
203214 batchItemFailures . push ( { itemIdentifier : record . messageId } ) ;
204215 }
205216 } ) ;
206217
207218 await Promise . all ( tasks ) ;
219+ await emitMetrics ( metrics , perSupplierSuccess , perSupplierFailure ) ;
208220
209221 return { batchItemFailures } ;
210222 } ;
0 commit comments