@@ -5,9 +5,10 @@ import { LogLevel, setLogLevel } from "../utils";
5
5
import { EXTENSION_URL } from "./extension" ;
6
6
7
7
import { MetricsListener } from "./listener" ;
8
- import StatsDClient from "hot-shots " ;
8
+ import { LambdaDogStatsD } from "./dogstatsd " ;
9
9
import { Context } from "aws-lambda" ;
10
- jest . mock ( "hot-shots" ) ;
10
+
11
+ jest . mock ( "./dogstatsd" ) ;
11
12
12
13
jest . mock ( "@aws-sdk/client-secrets-manager" , ( ) => {
13
14
return {
@@ -17,6 +18,9 @@ jest.mock("@aws-sdk/client-secrets-manager", () => {
17
18
} ;
18
19
} ) ;
19
20
21
+ const MOCK_TIME_SECONDS = 1487076708 ;
22
+ const MOCK_TIME_MS = 1487076708000 ;
23
+
20
24
const siteURL = "example.com" ;
21
25
22
26
class MockKMS {
@@ -56,6 +60,7 @@ describe("MetricsListener", () => {
56
60
57
61
expect ( nock . isDone ( ) ) . toBeTruthy ( ) ;
58
62
} ) ;
63
+
59
64
it ( "uses encrypted kms key if it's the only value available" , async ( ) => {
60
65
nock ( "https://api.example.com" ) . post ( "/api/v1/distribution_points?api_key=kms-api-key-decrypted" ) . reply ( 200 , { } ) ;
61
66
@@ -184,7 +189,7 @@ describe("MetricsListener", () => {
184
189
185
190
it ( "logs metrics when logForwarding is enabled" , async ( ) => {
186
191
const spy = jest . spyOn ( process . stdout , "write" ) ;
187
- jest . spyOn ( Date , "now " ) . mockImplementation ( ( ) => 1487076708000 ) ;
192
+ jest . spyOn ( Date . prototype , "getTime " ) . mockReturnValue ( MOCK_TIME_MS ) ;
188
193
const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
189
194
const listener = new MetricsListener ( kms as any , {
190
195
apiKey : "api-key" ,
@@ -202,22 +207,23 @@ describe("MetricsListener", () => {
202
207
listener . sendDistributionMetric ( "my-metric" , 10 , false , "tag:a" , "tag:b" ) ;
203
208
await listener . onCompleteInvocation ( ) ;
204
209
205
- expect ( spy ) . toHaveBeenCalledWith ( `{"e":1487076708 ,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n` ) ;
210
+ expect ( spy ) . toHaveBeenCalledWith ( `{"e":${ MOCK_TIME_SECONDS } ,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n` ) ;
206
211
} ) ;
212
+
207
213
it ( "always sends metrics to statsD when extension is enabled, ignoring logForwarding=true" , async ( ) => {
208
214
const flushScope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . reply ( 200 ) ;
209
215
mock ( {
210
216
"/opt/extensions/datadog-agent" : Buffer . from ( [ 0 ] ) ,
211
217
} ) ;
212
218
const distributionMock = jest . fn ( ) ;
213
- ( StatsDClient as any ) . mockImplementation ( ( ) => {
219
+ ( LambdaDogStatsD as any ) . mockImplementation ( ( ) => {
214
220
return {
215
221
distribution : distributionMock ,
216
222
close : ( callback : any ) => callback ( undefined ) ,
217
223
} ;
218
224
} ) ;
219
225
220
- jest . spyOn ( Date , "now " ) . mockImplementation ( ( ) => 1487076708000 ) ;
226
+ jest . spyOn ( Date . prototype , "getTime " ) . mockReturnValue ( MOCK_TIME_MS ) ;
221
227
222
228
const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
223
229
const listener = new MetricsListener ( kms as any , {
@@ -236,25 +242,26 @@ describe("MetricsListener", () => {
236
242
listener . sendDistributionMetric ( "my-metric" , 10 , false , "tag:a" , "tag:b" ) ;
237
243
await listener . onCompleteInvocation ( ) ;
238
244
expect ( flushScope . isDone ( ) ) . toBeTruthy ( ) ;
239
- expect ( distributionMock ) . toHaveBeenCalledWith ( "my-metric" , 10 , undefined , [ "tag:a" , "tag:b" ] ) ;
245
+ expect ( distributionMock ) . toHaveBeenCalledWith ( "my-metric" , 10 , MOCK_TIME_SECONDS , [ "tag:a" , "tag:b" ] ) ;
240
246
} ) ;
241
247
242
- it ( "only sends metrics with timestamps to the API when the extension is enabled" , async ( ) => {
248
+ it ( "sends metrics with timestamps to statsD (not API!) when the extension is enabled" , async ( ) => {
249
+ jest . spyOn ( Date . prototype , "getTime" ) . mockReturnValue ( MOCK_TIME_MS ) ;
243
250
const flushScope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . reply ( 200 ) ;
244
251
mock ( {
245
252
"/opt/extensions/datadog-agent" : Buffer . from ( [ 0 ] ) ,
246
253
} ) ;
247
254
const apiScope = nock ( "https://api.example.com" ) . post ( "/api/v1/distribution_points?api_key=api-key" ) . reply ( 200 , { } ) ;
248
255
249
256
const distributionMock = jest . fn ( ) ;
250
- ( StatsDClient as any ) . mockImplementation ( ( ) => {
257
+ ( LambdaDogStatsD as any ) . mockImplementation ( ( ) => {
251
258
return {
252
259
distribution : distributionMock ,
253
260
close : ( callback : any ) => callback ( undefined ) ,
254
261
} ;
255
262
} ) ;
256
263
257
- const metricTimeOneMinuteAgo = new Date ( Date . now ( ) - 60000 ) ;
264
+ const metricTimeOneMinuteAgo = new Date ( MOCK_TIME_MS - 60000 ) ;
258
265
const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
259
266
const listener = new MetricsListener ( kms as any , {
260
267
apiKey : "api-key" ,
@@ -280,12 +287,15 @@ describe("MetricsListener", () => {
280
287
"tag:a" ,
281
288
"tag:b" ,
282
289
) ;
283
- listener . sendDistributionMetric ( "my-metric-without -a-timestamp" , 10 , false , "tag:a" , "tag:b" ) ;
290
+ listener . sendDistributionMetric ( "my-metric-with -a-timestamp" , 10 , false , "tag:a" , "tag:b" ) ;
284
291
await listener . onCompleteInvocation ( ) ;
285
292
286
293
expect ( flushScope . isDone ( ) ) . toBeTruthy ( ) ;
287
- expect ( apiScope . isDone ( ) ) . toBeTruthy ( ) ;
288
- expect ( distributionMock ) . toHaveBeenCalledWith ( "my-metric-without-a-timestamp" , 10 , undefined , [ "tag:a" , "tag:b" ] ) ;
294
+ expect ( apiScope . isDone ( ) ) . toBeFalsy ( ) ;
295
+ expect ( distributionMock ) . toHaveBeenCalledWith ( "my-metric-with-a-timestamp" , 10 , MOCK_TIME_SECONDS , [
296
+ "tag:a" ,
297
+ "tag:b" ,
298
+ ] ) ;
289
299
} ) ;
290
300
291
301
it ( "does not send historical metrics from over 4 hours ago to the API" , async ( ) => {
@@ -316,7 +326,6 @@ describe("MetricsListener", () => {
316
326
317
327
it ( "logs metrics when logForwarding is enabled with custom timestamp" , async ( ) => {
318
328
const spy = jest . spyOn ( process . stdout , "write" ) ;
319
- // jest.spyOn(Date, "now").mockImplementation(() => 1487076708000);
320
329
const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
321
330
const listener = new MetricsListener ( kms as any , {
322
331
apiKey : "api-key" ,
@@ -328,7 +337,6 @@ describe("MetricsListener", () => {
328
337
localTesting : false ,
329
338
siteURL,
330
339
} ) ;
331
- // jest.useFakeTimers();
332
340
333
341
await listener . onStartInvocation ( { } ) ;
334
342
listener . sendDistributionMetricWithDate ( "my-metric" , 10 , new Date ( 1584983836 * 1000 ) , false , "tag:a" , "tag:b" ) ;
0 commit comments