1+ import {
2+ MetricUnits ,
3+ Metrics ,
4+ logMetrics ,
5+ } from '@aws-lambda-powertools/metrics'
16import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
27import { EventBridge } from '@aws-sdk/client-eventbridge'
38import { proto , type HelloMessage } from '@hello.nrfcloud.com/proto/hello'
9+ import middy from '@middy/core'
410import { fromEnv } from '@nordicsemiconductor/from-env'
511import type { Static } from '@sinclair/typebox'
612import { getModelForDevice } from '../devices/getModelForDevice.js'
@@ -37,7 +43,12 @@ const parseMessage = async (
3743 return converted
3844}
3945
40- export const handler = async ( event : {
46+ const metrics = new Metrics ( {
47+ namespace : 'hello-nrfcloud-backend' ,
48+ serviceName : 'onDeviceMessage' ,
49+ } )
50+
51+ const h = async ( event : {
4152 message : unknown
4253 deviceId : string
4354 timestamp : number
@@ -47,8 +58,20 @@ export const handler = async (event: {
4758 const { model } = await modelFetcher ( deviceId )
4859 log . debug ( 'model' , { model } )
4960
61+ metrics . addMetric ( 'deviceMessage' , MetricUnits . Count , 1 )
62+
5063 const converted = await parseMessage ( model , message )
5164
65+ if ( converted . length === 0 ) {
66+ metrics . addMetric ( 'unknownDeviceMessage' , MetricUnits . Count , 1 )
67+ } else {
68+ metrics . addMetric (
69+ 'convertedDeviceMessage' ,
70+ MetricUnits . Count ,
71+ converted . length ,
72+ )
73+ }
74+
5275 for ( const message of converted ) {
5376 log . debug ( 'websocket message' , { payload : message } )
5477 await eventBus . putEvents ( {
@@ -66,3 +89,5 @@ export const handler = async (event: {
6689 } )
6790 }
6891}
92+
93+ export const handler = middy ( h ) . use ( logMetrics ( metrics ) )
0 commit comments