1
1
import { StatsD } from "hot-shots" ;
2
2
import { promisify } from "util" ;
3
3
import { logDebug , logError } from "../utils" ;
4
- import { AGENT_URL , isAgentRunning } from "./extension" ;
4
+ import { EXTENSION_URL , isExtensionRunning } from "./extension" ;
5
5
import { KMSService } from "./kms-service" ;
6
6
import { writeMetricToStdout } from "./metric-log" ;
7
7
import { Distribution } from "./model" ;
8
8
import { URL } from "url" ;
9
9
10
- const metricsBatchSendIntervalMS = 10000 ; // 10 seconds
10
+ const METRICS_BATCH_SEND_INTERVAL = 10000 ; // 10 seconds
11
11
const LOCAL_FLUSH_TIMEOUT_MS = 100 ;
12
12
const LOCAL_FLUSH_PATH = "/lambda/flush" ;
13
13
@@ -60,20 +60,20 @@ export class MetricsListener {
60
60
private currentProcessor ?: Promise < any > ;
61
61
private apiKey : Promise < string > ;
62
62
private statsDClient ?: StatsD ;
63
- private isAgentRunning ?: boolean = undefined ;
63
+ private isExtensionRunning ?: boolean = undefined ;
64
64
65
65
constructor ( private kmsClient : KMSService , private config : MetricsConfig ) {
66
66
this . apiKey = this . getAPIKey ( config ) ;
67
67
this . config = config ;
68
68
}
69
69
70
70
public async onStartInvocation ( _ : any ) {
71
- if ( this . isAgentRunning === undefined ) {
72
- this . isAgentRunning = await isAgentRunning ( ) ;
73
- logDebug ( `Extension present: ${ this . isAgentRunning } ` ) ;
71
+ if ( this . isExtensionRunning === undefined ) {
72
+ this . isExtensionRunning = await isExtensionRunning ( ) ;
73
+ logDebug ( `Extension present: ${ this . isExtensionRunning } ` ) ;
74
74
}
75
75
76
- if ( this . isAgentRunning ) {
76
+ if ( this . isExtensionRunning ) {
77
77
logDebug ( `Using StatsD client` ) ;
78
78
79
79
this . statsDClient = new StatsD ( { host : "127.0.0.1" , closingFlushInterval : 1 } ) ;
@@ -123,21 +123,7 @@ export class MetricsListener {
123
123
}
124
124
}
125
125
126
- try {
127
- if ( this . isAgentRunning && this . config . localTesting ) {
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
- }
134
- }
135
- } catch ( error ) {
136
- if ( error instanceof Error ) {
137
- logError ( "failed to flush extension" , error as Error ) ;
138
- }
139
- }
140
-
126
+ this . _localFlush ( ) ;
141
127
this . currentProcessor = undefined ;
142
128
}
143
129
@@ -148,7 +134,7 @@ export class MetricsListener {
148
134
forceAsync : boolean ,
149
135
...tags : string [ ]
150
136
) {
151
- if ( this . isAgentRunning ) {
137
+ if ( this . isExtensionRunning ) {
152
138
this . statsDClient ?. distribution ( name , value , undefined , tags ) ;
153
139
return ;
154
140
}
@@ -179,14 +165,14 @@ export class MetricsListener {
179
165
}
180
166
181
167
private async createProcessor ( config : MetricsConfig , apiKey : Promise < string > ) {
182
- if ( ! this . isAgentRunning && ! this . config . logForwarding ) {
168
+ if ( ! this . isExtensionRunning && ! this . config . logForwarding ) {
183
169
const APIClient = require ( "./api" ) . APIClient ;
184
170
const Processor = require ( "./processor" ) . Processor ;
185
171
186
172
const key = await apiKey ;
187
173
const url = `https://api.${ config . siteURL } ` ;
188
174
const apiClient = new APIClient ( key , url ) ;
189
- const processor = new Processor ( apiClient , metricsBatchSendIntervalMS , config . shouldRetryMetrics ) ;
175
+ const processor = new Processor ( apiClient , METRICS_BATCH_SEND_INTERVAL , config . shouldRetryMetrics ) ;
190
176
processor . startProcessing ( ) ;
191
177
return processor ;
192
178
}
@@ -206,4 +192,21 @@ export class MetricsListener {
206
192
}
207
193
return "" ;
208
194
}
195
+
196
+ private async _localFlush ( ) {
197
+ try {
198
+ if ( this . isExtensionRunning && this . config . localTesting ) {
199
+ const utils = require ( "../utils/request" ) ;
200
+ const url = new URL ( LOCAL_FLUSH_PATH , EXTENSION_URL ) ;
201
+ const result = await utils . post ( url , { } , { timeout : LOCAL_FLUSH_TIMEOUT_MS } ) ;
202
+ if ( ! result . success ) {
203
+ logError ( `Failed to flush extension. ${ result . errorMessage } ` ) ;
204
+ }
205
+ }
206
+ } catch ( error ) {
207
+ if ( error instanceof Error ) {
208
+ logError ( "Failed to flush extension" , error ) ;
209
+ }
210
+ }
211
+ }
209
212
}
0 commit comments