@@ -5,9 +5,10 @@ import { LogLevel, setLogLevel } from "../utils";
55import { EXTENSION_URL } from "./extension" ;
66
77import { MetricsListener } from "./listener" ;
8- import StatsDClient from "hot-shots " ;
8+ import { LambdaDogStatsD } from "./dogstatsd " ;
99import { Context } from "aws-lambda" ;
10- jest . mock ( "hot-shots" ) ;
10+
11+ jest . mock ( "./dogstatsd" ) ;
1112
1213jest . mock ( "@aws-sdk/client-secrets-manager" , ( ) => {
1314 return {
@@ -17,6 +18,9 @@ jest.mock("@aws-sdk/client-secrets-manager", () => {
1718 } ;
1819} ) ;
1920
21+ const MOCK_TIME_SECONDS = 1487076708 ;
22+ const MOCK_TIME_MS = 1487076708000 ;
23+
2024const siteURL = "example.com" ;
2125
2226class MockKMS {
@@ -56,6 +60,7 @@ describe("MetricsListener", () => {
5660
5761 expect ( nock . isDone ( ) ) . toBeTruthy ( ) ;
5862 } ) ;
63+
5964 it ( "uses encrypted kms key if it's the only value available" , async ( ) => {
6065 nock ( "https://api.example.com" ) . post ( "/api/v1/distribution_points?api_key=kms-api-key-decrypted" ) . reply ( 200 , { } ) ;
6166
@@ -184,7 +189,7 @@ describe("MetricsListener", () => {
184189
185190 it ( "logs metrics when logForwarding is enabled" , async ( ) => {
186191 const spy = jest . spyOn ( process . stdout , "write" ) ;
187- jest . spyOn ( Date , "now " ) . mockImplementation ( ( ) => 1487076708000 ) ;
192+ jest . spyOn ( Date . prototype , "getTime " ) . mockReturnValue ( MOCK_TIME_MS ) ;
188193 const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
189194 const listener = new MetricsListener ( kms as any , {
190195 apiKey : "api-key" ,
@@ -202,22 +207,23 @@ describe("MetricsListener", () => {
202207 listener . sendDistributionMetric ( "my-metric" , 10 , false , "tag:a" , "tag:b" ) ;
203208 await listener . onCompleteInvocation ( ) ;
204209
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` ) ;
206211 } ) ;
212+
207213 it ( "always sends metrics to statsD when extension is enabled, ignoring logForwarding=true" , async ( ) => {
208214 const flushScope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . reply ( 200 ) ;
209215 mock ( {
210216 "/opt/extensions/datadog-agent" : Buffer . from ( [ 0 ] ) ,
211217 } ) ;
212218 const distributionMock = jest . fn ( ) ;
213- ( StatsDClient as any ) . mockImplementation ( ( ) => {
219+ ( LambdaDogStatsD as any ) . mockImplementation ( ( ) => {
214220 return {
215221 distribution : distributionMock ,
216222 close : ( callback : any ) => callback ( undefined ) ,
217223 } ;
218224 } ) ;
219225
220- jest . spyOn ( Date , "now " ) . mockImplementation ( ( ) => 1487076708000 ) ;
226+ jest . spyOn ( Date . prototype , "getTime " ) . mockReturnValue ( MOCK_TIME_MS ) ;
221227
222228 const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
223229 const listener = new MetricsListener ( kms as any , {
@@ -236,25 +242,26 @@ describe("MetricsListener", () => {
236242 listener . sendDistributionMetric ( "my-metric" , 10 , false , "tag:a" , "tag:b" ) ;
237243 await listener . onCompleteInvocation ( ) ;
238244 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" ] ) ;
240246 } ) ;
241247
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 ) ;
243250 const flushScope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . reply ( 200 ) ;
244251 mock ( {
245252 "/opt/extensions/datadog-agent" : Buffer . from ( [ 0 ] ) ,
246253 } ) ;
247254 const apiScope = nock ( "https://api.example.com" ) . post ( "/api/v1/distribution_points?api_key=api-key" ) . reply ( 200 , { } ) ;
248255
249256 const distributionMock = jest . fn ( ) ;
250- ( StatsDClient as any ) . mockImplementation ( ( ) => {
257+ ( LambdaDogStatsD as any ) . mockImplementation ( ( ) => {
251258 return {
252259 distribution : distributionMock ,
253260 close : ( callback : any ) => callback ( undefined ) ,
254261 } ;
255262 } ) ;
256263
257- const metricTimeOneMinuteAgo = new Date ( Date . now ( ) - 60000 ) ;
264+ const metricTimeOneMinuteAgo = new Date ( MOCK_TIME_MS - 60000 ) ;
258265 const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
259266 const listener = new MetricsListener ( kms as any , {
260267 apiKey : "api-key" ,
@@ -280,12 +287,15 @@ describe("MetricsListener", () => {
280287 "tag:a" ,
281288 "tag:b" ,
282289 ) ;
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" ) ;
284291 await listener . onCompleteInvocation ( ) ;
285292
286293 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+ ] ) ;
289299 } ) ;
290300
291301 it ( "does not send historical metrics from over 4 hours ago to the API" , async ( ) => {
@@ -316,7 +326,6 @@ describe("MetricsListener", () => {
316326
317327 it ( "logs metrics when logForwarding is enabled with custom timestamp" , async ( ) => {
318328 const spy = jest . spyOn ( process . stdout , "write" ) ;
319- // jest.spyOn(Date, "now").mockImplementation(() => 1487076708000);
320329 const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
321330 const listener = new MetricsListener ( kms as any , {
322331 apiKey : "api-key" ,
@@ -328,7 +337,6 @@ describe("MetricsListener", () => {
328337 localTesting : false ,
329338 siteURL,
330339 } ) ;
331- // jest.useFakeTimers();
332340
333341 await listener . onStartInvocation ( { } ) ;
334342 listener . sendDistributionMetricWithDate ( "my-metric" , 10 , new Date ( 1584983836 * 1000 ) , false , "tag:a" , "tag:b" ) ;
0 commit comments