Skip to content

Commit 8e731ce

Browse files
committed
feat(tracking): log device message metrics
See #4
1 parent d81bd84 commit 8e731ce

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lambda/onDeviceMessage.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import {
2+
MetricUnits,
3+
Metrics,
4+
logMetrics,
5+
} from '@aws-lambda-powertools/metrics'
16
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
27
import { EventBridge } from '@aws-sdk/client-eventbridge'
38
import { proto, type HelloMessage } from '@hello.nrfcloud.com/proto/hello'
9+
import middy from '@middy/core'
410
import { fromEnv } from '@nordicsemiconductor/from-env'
511
import type { Static } from '@sinclair/typebox'
612
import { 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

Comments
 (0)