Skip to content

Commit 1903994

Browse files
committed
update imports and move method to private function
1 parent 2d668b2 commit 1903994

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/metrics/listener.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { StatsD } from "hot-shots";
22
import { promisify } from "util";
33
import { logDebug, logError } from "../utils";
4-
import { AGENT_URL, isAgentRunning } from "./extension";
4+
import { EXTENSION_URL, isExtensionRunning } from "./extension";
55
import { KMSService } from "./kms-service";
66
import { writeMetricToStdout } from "./metric-log";
77
import { Distribution } from "./model";
88
import { URL } from "url";
99

10-
const metricsBatchSendIntervalMS = 10000; // 10 seconds
10+
const METRICS_BATCH_SEND_INTERVAL = 10000; // 10 seconds
1111
const LOCAL_FLUSH_TIMEOUT_MS = 100;
1212
const LOCAL_FLUSH_PATH = "/lambda/flush";
1313

@@ -60,20 +60,20 @@ export class MetricsListener {
6060
private currentProcessor?: Promise<any>;
6161
private apiKey: Promise<string>;
6262
private statsDClient?: StatsD;
63-
private isAgentRunning?: boolean = undefined;
63+
private isExtensionRunning?: boolean = undefined;
6464

6565
constructor(private kmsClient: KMSService, private config: MetricsConfig) {
6666
this.apiKey = this.getAPIKey(config);
6767
this.config = config;
6868
}
6969

7070
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}`);
7474
}
7575

76-
if (this.isAgentRunning) {
76+
if (this.isExtensionRunning) {
7777
logDebug(`Using StatsD client`);
7878

7979
this.statsDClient = new StatsD({ host: "127.0.0.1", closingFlushInterval: 1 });
@@ -123,21 +123,7 @@ export class MetricsListener {
123123
}
124124
}
125125

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();
141127
this.currentProcessor = undefined;
142128
}
143129

@@ -148,7 +134,7 @@ export class MetricsListener {
148134
forceAsync: boolean,
149135
...tags: string[]
150136
) {
151-
if (this.isAgentRunning) {
137+
if (this.isExtensionRunning) {
152138
this.statsDClient?.distribution(name, value, undefined, tags);
153139
return;
154140
}
@@ -179,14 +165,14 @@ export class MetricsListener {
179165
}
180166

181167
private async createProcessor(config: MetricsConfig, apiKey: Promise<string>) {
182-
if (!this.isAgentRunning && !this.config.logForwarding) {
168+
if (!this.isExtensionRunning && !this.config.logForwarding) {
183169
const APIClient = require("./api").APIClient;
184170
const Processor = require("./processor").Processor;
185171

186172
const key = await apiKey;
187173
const url = `https://api.${config.siteURL}`;
188174
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);
190176
processor.startProcessing();
191177
return processor;
192178
}
@@ -206,4 +192,21 @@ export class MetricsListener {
206192
}
207193
return "";
208194
}
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+
}
209212
}

0 commit comments

Comments
 (0)