Skip to content

Commit a556d3e

Browse files
committed
test(e2e): disable metrics in tests
1 parent 8e8dbd6 commit a556d3e

File tree

14 files changed

+90
-71
lines changed

14 files changed

+90
-71
lines changed

.github/workflows/test-and-release.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ jobs:
7575
./cli.sh configure thirdParty nrfcloud apiKey apiKey
7676
7777
- name: Deploy solution stack
78+
env:
79+
IS_TEST: 1
7880
run: npx cdk deploy --require-approval never
7981

8082
- name: Run End-to-End Tests

cdk/BackendApp.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { App } from 'aws-cdk-lib'
22
import { BackendStack } from './stacks/BackendStack.js'
33

44
export class BackendApp extends App {
5-
public constructor(args: ConstructorParameters<typeof BackendStack>[1]) {
6-
super()
5+
public constructor({
6+
isTest,
7+
...rest
8+
}: ConstructorParameters<typeof BackendStack>[1] & { isTest: boolean }) {
9+
super({
10+
context: {
11+
isTest,
12+
},
13+
})
714

8-
new BackendStack(this, args)
15+
new BackendStack(this, rest)
916
}
1017
}

cdk/backend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,5 @@ new BackendApp({
103103
iam,
104104
}),
105105
env: accountEnv,
106+
isTest: process.env.IS_TEST === '1',
106107
})

cdk/resources/ConvertDeviceMessages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class ConvertDeviceMessages extends Construct {
4949
DEVICES_TABLE_NAME: deviceStorage.devicesTable.tableName,
5050
DEVICES_INDEX_NAME: deviceStorage.devicesTableFingerprintIndexName,
5151
NODE_NO_WARNINGS: '1',
52+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
5253
},
5354
layers,
5455
logRetention: Logs.RetentionDays.ONE_WEEK,

cdk/resources/DeviceShadow.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export class DeviceShadow extends Construct {
8282
QUEUE_URL: shadowQueue.queueUrl,
8383
LOG_LEVEL: this.node.tryGetContext('logLevel'),
8484
NODE_NO_WARNINGS: '1',
85+
DISABLE_METRICS:
86+
this.node.tryGetContext('isTest') === true ? '1' : '0',
8587
},
8688
initialPolicy: [],
8789
layers,
@@ -108,6 +110,7 @@ export class DeviceShadow extends Construct {
108110
LOG_LEVEL: this.node.tryGetContext('logLevel'),
109111
STACK_NAME: Stack.of(this).stackName,
110112
NODE_NO_WARNINGS: '1',
113+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
111114
},
112115
initialPolicy: [
113116
new IAM.PolicyStatement({

cdk/resources/HealthCheckMqttBridge.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class HealthCheckMqttBridge extends Construct {
5555
STACK_NAME: Stack.of(this).stackName,
5656
DEVICES_TABLE_NAME: deviceStorage.devicesTable.tableName,
5757
WEBSOCKET_URL: websocketAPI.websocketURI,
58+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
5859
},
5960
initialPolicy: [],
6061
layers,

cdk/resources/HistoricalData.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class HistoricalData extends Construct {
6666
LOG_LEVEL: this.node.tryGetContext('logLevel'),
6767
HISTORICAL_DATA_TABLE_INFO: this.table.ref,
6868
NODE_NO_WARNINGS: '1',
69+
DISABLE_METRICS:
70+
this.node.tryGetContext('isTest') === true ? '1' : '0',
6971
},
7072
layers,
7173
initialPolicy: [

cdk/resources/WebsocketAPI.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export class WebsocketAPI extends Construct {
8585
EVENTBUS_NAME: this.eventBus.eventBusName,
8686
LOG_LEVEL: this.node.tryGetContext('logLevel'),
8787
NODE_NO_WARNINGS: '1',
88+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
8889
},
8990
layers,
9091
logRetention: Logs.RetentionDays.ONE_WEEK,
@@ -106,6 +107,7 @@ export class WebsocketAPI extends Construct {
106107
WEBSOCKET_CONNECTIONS_TABLE_NAME: this.connectionsTable.tableName,
107108
LOG_LEVEL: this.node.tryGetContext('logLevel'),
108109
NODE_NO_WARNINGS: '1',
110+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
109111
},
110112
layers,
111113
logRetention: Logs.RetentionDays.ONE_WEEK,
@@ -127,6 +129,7 @@ export class WebsocketAPI extends Construct {
127129
EVENTBUS_NAME: this.eventBus.eventBusName,
128130
LOG_LEVEL: this.node.tryGetContext('logLevel'),
129131
NODE_NO_WARNINGS: '1',
132+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
130133
},
131134
layers,
132135
logRetention: Logs.RetentionDays.ONE_WEEK,
@@ -151,6 +154,7 @@ export class WebsocketAPI extends Construct {
151154
DEVICES_INDEX_NAME: deviceStorage.devicesTableFingerprintIndexName,
152155
LOG_LEVEL: this.node.tryGetContext('logLevel'),
153156
NODE_NO_WARNINGS: '1',
157+
DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0',
154158
},
155159
initialPolicy: [
156160
new IAM.PolicyStatement({
@@ -305,6 +309,8 @@ export class WebsocketAPI extends Construct {
305309
EVENTBUS_NAME: this.eventBus.eventBusName,
306310
LOG_LEVEL: this.node.tryGetContext('logLevel'),
307311
NODE_NO_WARNINGS: '1',
312+
DISABLE_METRICS:
313+
this.node.tryGetContext('isTest') === true ? '1' : '0',
308314
},
309315
initialPolicy: [
310316
new IAM.PolicyStatement({

lambda/authorizer.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import {
2-
MetricUnits,
3-
Metrics,
4-
logMetrics,
5-
} from '@aws-lambda-powertools/metrics'
1+
import { MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics'
62
import { DynamoDBClient, QueryCommand } from '@aws-sdk/client-dynamodb'
73
import { unmarshall } from '@aws-sdk/util-dynamodb'
84
import middy from '@middy/core'
95
import { fromEnv } from '@nordicsemiconductor/from-env'
106
import type { PolicyDocument } from 'aws-lambda'
7+
import { metricsForComponent } from './metrics/metrics.js'
118
import { logger } from './util/logger.js'
129
import type { WebsocketConnectionContext } from './ws/AuthorizedEvent.js'
1310

@@ -19,10 +16,7 @@ const { DevicesTableName, DevicesIndexName } = fromEnv({
1916
const log = logger('connect')
2017
const db = new DynamoDBClient({})
2118

22-
const metrics = new Metrics({
23-
namespace: 'hello-nrfcloud-backend',
24-
serviceName: 'websocket',
25-
})
19+
const { track, metrics } = metricsForComponent('websocket')
2620

2721
/**
2822
* Verifies the fingerprint passed as a query parameter and creates a context for the websocket connect that includes the deviceId and the model.
@@ -57,7 +51,7 @@ const h = async (event: {
5751
const fingerprint = event.queryStringParameters?.fingerprint
5852
if (fingerprint === undefined) {
5953
log.error(`Fingerprint cannot be empty`)
60-
metrics.addMetric('authorizer:badRequest', MetricUnits.Count, 1)
54+
track('authorizer:badRequest', MetricUnits.Count, 1)
6155
return deny
6256
}
6357
const res = await db.send(
@@ -79,12 +73,12 @@ const h = async (event: {
7973
const device = res.Items?.[0] !== undefined ? unmarshall(res.Items[0]) : null
8074
if (device === null) {
8175
log.error(`DeviceId is not found with`, { fingerprint })
82-
metrics.addMetric('authorizer:badFingerprint', MetricUnits.Count, 1)
76+
track('authorizer:badFingerprint', MetricUnits.Count, 1)
8377
return deny
8478
}
8579

8680
log.debug(`Connection request for fingerprint ${fingerprint} authorized.`)
87-
metrics.addMetric('authorizer:success', MetricUnits.Count, 1)
81+
track('authorizer:success', MetricUnits.Count, 1)
8882

8983
const { model, deviceId } = device
9084

lambda/fetchDeviceShadow.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
logMetrics,
3-
Metrics,
4-
MetricUnits,
5-
} from '@aws-lambda-powertools/metrics'
1+
import { logMetrics, MetricUnits } from '@aws-lambda-powertools/metrics'
62
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
73
import {
84
EventBridgeClient,
@@ -19,14 +15,12 @@ import {
1915
type WebsocketDeviceConnectionShadowInfo,
2016
} from '../websocket/connectionsRepository.js'
2117
import { createLock } from '../websocket/lock.js'
18+
import { metricsForComponent } from './metrics/metrics.js'
2219
import type { WebsocketPayload } from './publishToWebsocketClients.js'
2320
import { configureDeviceShadowFetcher } from './shadow/configureDeviceShadowFetcher.js'
2421
import { logger } from './util/logger.js'
2522

26-
const metrics = new Metrics({
27-
namespace: 'hello-nrfcloud-backend',
28-
serviceName: 'shadowFetcher',
29-
})
23+
const { track, metrics } = metricsForComponent('shadowFetcher')
3024

3125
const {
3226
websocketDeviceConnectionsTableName,
@@ -69,7 +63,7 @@ const h = async (): Promise<void> => {
6963
const deviceShadow = await getShadowFetcher()
7064
const connections = await connectionsRepo.getAll()
7165
log.info(`Found ${connections.length} active connections`)
72-
metrics.addMetric('connections', MetricUnits.Count, connections.length)
66+
track('connections', MetricUnits.Count, connections.length)
7367
const deviceConnectionsMap = connections.reduce(
7468
(map, connection) => ({
7569
...map,
@@ -90,7 +84,7 @@ const h = async (): Promise<void> => {
9084
const res = await deviceShadow(
9185
devices.map((device) => device.deviceId),
9286
)
93-
metrics.addMetric(
87+
track(
9488
'apiResponseTime',
9589
MetricUnits.Milliseconds,
9690
Date.now() - start,
@@ -113,7 +107,7 @@ const h = async (): Promise<void> => {
113107
isChanged: d.version !== deviceShadow.state.version,
114108
})
115109

116-
metrics.addMetric(
110+
track(
117111
'shadowVersionDelta',
118112
MetricUnits.Count,
119113
deviceShadow.state.version - (d.version ?? 0),
@@ -125,12 +119,12 @@ const h = async (): Promise<void> => {
125119
)
126120

127121
if (!isUpdated) {
128-
metrics.addMetric('shadowStale', MetricUnits.Count, 1)
122+
track('shadowStale', MetricUnits.Count, 1)
129123
continue
130124
}
131125

132-
metrics.addMetric('shadowUpdated', MetricUnits.Count, 1)
133-
metrics.addMetric(
126+
track('shadowUpdated', MetricUnits.Count, 1)
127+
track(
134128
'shadowAge',
135129
MetricUnits.Seconds,
136130
Math.round(Date.now() / 1000) -
@@ -145,7 +139,7 @@ const h = async (): Promise<void> => {
145139
message,
146140
)} from model ${model}: ${error}`,
147141
)
148-
metrics.addMetric('shadowConversionFailed', MetricUnits.Count, 1)
142+
track('shadowConversionFailed', MetricUnits.Count, 1)
149143
},
150144
})(model, deviceShadow.state)
151145

0 commit comments

Comments
 (0)