1
1
import { StatsD } from "hot-shots" ;
2
2
import { promisify } from "util" ;
3
3
import { logDebug , logError } from "../utils" ;
4
- import { APIClient } from "./api" ;
5
- import { flushExtension , isAgentRunning } from "./extension" ;
4
+ import { AGENT_URL , isAgentRunning } from "./extension" ;
6
5
import { KMSService } from "./kms-service" ;
7
6
import { writeMetricToStdout } from "./metric-log" ;
8
7
import { Distribution } from "./model" ;
9
- import { Processor } from "./processor " ;
8
+ import { URL } from "url " ;
10
9
11
10
const metricsBatchSendIntervalMS = 10000 ; // 10 seconds
11
+ const LOCAL_FLUSH_TIMEOUT_MS = 100 ;
12
+ const LOCAL_FLUSH_PATH = "/lambda/flush" ;
12
13
13
14
export interface MetricsConfig {
14
15
/**
@@ -56,7 +57,7 @@ export interface MetricsConfig {
56
57
}
57
58
58
59
export class MetricsListener {
59
- private currentProcessor ?: Promise < Processor > ;
60
+ private currentProcessor ?: Promise < any > ;
60
61
private apiKey : Promise < string > ;
61
62
private statsDClient ?: StatsD ;
62
63
private isAgentRunning ?: boolean = undefined ;
@@ -83,6 +84,7 @@ export class MetricsListener {
83
84
84
85
return ;
85
86
}
87
+
86
88
this . currentProcessor = this . createProcessor ( this . config , this . apiKey ) ;
87
89
}
88
90
@@ -120,16 +122,22 @@ export class MetricsListener {
120
122
logError ( "failed to flush metrics" , error as Error ) ;
121
123
}
122
124
}
125
+
123
126
try {
124
127
if ( this . isAgentRunning && this . config . localTesting ) {
125
- logDebug ( `Flushing Extension for local test` ) ;
126
- await flushExtension ( ) ;
128
+ const utils = require ( "../utils/request" ) ;
129
+ const url = new URL ( LOCAL_FLUSH_PATH , AGENT_URL ) ;
130
+ const result = await utils . post ( url , { } , { timeout : LOCAL_FLUSH_TIMEOUT_MS } ) ;
131
+ if ( ! result . success ) {
132
+ logError ( `Failed to flush extension. ${ result . errorMessage } ` ) ;
133
+ }
127
134
}
128
135
} catch ( error ) {
129
136
if ( error instanceof Error ) {
130
137
logError ( "failed to flush extension" , error as Error ) ;
131
138
}
132
139
}
140
+
133
141
this . currentProcessor = undefined ;
134
142
}
135
143
@@ -171,12 +179,17 @@ export class MetricsListener {
171
179
}
172
180
173
181
private async createProcessor ( config : MetricsConfig , apiKey : Promise < string > ) {
174
- const key = await apiKey ;
175
- const url = `https://api.${ config . siteURL } ` ;
176
- const apiClient = new APIClient ( key , url ) ;
177
- const processor = new Processor ( apiClient , metricsBatchSendIntervalMS , config . shouldRetryMetrics ) ;
178
- processor . startProcessing ( ) ;
179
- return processor ;
182
+ if ( ! this . isAgentRunning && ! this . config . logForwarding ) {
183
+ const APIClient = require ( "./api" ) . APIClient ;
184
+ const Processor = require ( "./processor" ) . Processor ;
185
+
186
+ const key = await apiKey ;
187
+ const url = `https://api.${ config . siteURL } ` ;
188
+ const apiClient = new APIClient ( key , url ) ;
189
+ const processor = new Processor ( apiClient , metricsBatchSendIntervalMS , config . shouldRetryMetrics ) ;
190
+ processor . startProcessing ( ) ;
191
+ return processor ;
192
+ }
180
193
}
181
194
182
195
private async getAPIKey ( config : MetricsConfig ) {
0 commit comments