@@ -138,6 +138,51 @@ describe("MetricsListener", () => {
138
138
expect ( distributionMock ) . toHaveBeenCalledWith ( "my-metric" , 10 , undefined , [ "tag:a" , "tag:b" ] ) ;
139
139
} ) ;
140
140
141
+ it ( "only sends metrics with timestamps to the API when the extension is enabled" , async ( ) => {
142
+ const flushScope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . reply ( 200 ) ;
143
+ mock ( {
144
+ "/opt/extensions/datadog-agent" : Buffer . from ( [ 0 ] ) ,
145
+ } ) ;
146
+ nock ( "https://api.example.com" ) . post ( "/api/v1/distribution_points?api_key=api-key" ) . reply ( 200 , { } ) ;
147
+
148
+ const distributionMock = jest . fn ( ) ;
149
+ ( StatsDClient as any ) . mockImplementation ( ( ) => {
150
+ return {
151
+ distribution : distributionMock ,
152
+ close : ( callback : any ) => callback ( undefined ) ,
153
+ } ;
154
+ } ) ;
155
+
156
+ jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => 1487076708000 ) ;
157
+
158
+ const metricTimeOneMinuteAgo = new Date ( Date . now ( ) - 60000 ) ;
159
+ const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
160
+ const listener = new MetricsListener ( kms as any , {
161
+ apiKey : "api-key" ,
162
+ apiKeyKMS : "" ,
163
+ enhancedMetrics : false ,
164
+ logForwarding : false ,
165
+ shouldRetryMetrics : false ,
166
+ localTesting : true ,
167
+ siteURL,
168
+ } ) ;
169
+
170
+ await listener . onStartInvocation ( { } ) ;
171
+ listener . sendDistributionMetricWithDate (
172
+ "my-metric-with-a-timestamp" ,
173
+ 10 ,
174
+ metricTimeOneMinuteAgo ,
175
+ false ,
176
+ "tag:a" ,
177
+ "tag:b" ,
178
+ ) ;
179
+ listener . sendDistributionMetric ( "my-metric-without-a-timestamp" , 10 , false , "tag:a" , "tag:b" ) ;
180
+ await listener . onCompleteInvocation ( ) ;
181
+ expect ( flushScope . isDone ( ) ) . toBeTruthy ( ) ;
182
+ expect ( nock . isDone ( ) ) . toBeTruthy ( ) ;
183
+ expect ( distributionMock ) . toHaveBeenCalledWith ( "my-metric-without-a-timestamp" , 10 , undefined , [ "tag:a" , "tag:b" ] ) ;
184
+ } ) ;
185
+
141
186
it ( "logs metrics when logForwarding is enabled with custom timestamp" , async ( ) => {
142
187
const spy = jest . spyOn ( process . stdout , "write" ) ;
143
188
// jest.spyOn(Date, "now").mockImplementation(() => 1487076708000);
0 commit comments