Skip to content

Commit 2a43b0b

Browse files
committed
lazy load request to remove 1 MB dep
1 parent b16ef2f commit 2a43b0b

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/metrics/listener.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { StatsD } from "hot-shots";
22
import { promisify } from "util";
33
import { logDebug, logError } from "../utils";
4-
import { APIClient } from "./api";
5-
import { flushExtension, isAgentRunning } from "./extension";
4+
import { AGENT_URL, isAgentRunning } from "./extension";
65
import { KMSService } from "./kms-service";
76
import { writeMetricToStdout } from "./metric-log";
87
import { Distribution } from "./model";
9-
import { Processor } from "./processor";
8+
import { URL } from "url";
109

1110
const metricsBatchSendIntervalMS = 10000; // 10 seconds
11+
const LOCAL_FLUSH_TIMEOUT_MS = 100;
12+
const LOCAL_FLUSH_PATH = "/lambda/flush";
1213

1314
export interface MetricsConfig {
1415
/**
@@ -56,7 +57,7 @@ export interface MetricsConfig {
5657
}
5758

5859
export class MetricsListener {
59-
private currentProcessor?: Promise<Processor>;
60+
private currentProcessor?: Promise<any>;
6061
private apiKey: Promise<string>;
6162
private statsDClient?: StatsD;
6263
private isAgentRunning?: boolean = undefined;
@@ -83,6 +84,7 @@ export class MetricsListener {
8384

8485
return;
8586
}
87+
8688
this.currentProcessor = this.createProcessor(this.config, this.apiKey);
8789
}
8890

@@ -120,16 +122,22 @@ export class MetricsListener {
120122
logError("failed to flush metrics", error as Error);
121123
}
122124
}
125+
123126
try {
124127
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+
}
127134
}
128135
} catch (error) {
129136
if (error instanceof Error) {
130137
logError("failed to flush extension", error as Error);
131138
}
132139
}
140+
133141
this.currentProcessor = undefined;
134142
}
135143

@@ -171,12 +179,17 @@ export class MetricsListener {
171179
}
172180

173181
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+
}
180193
}
181194

182195
private async getAPIKey(config: MetricsConfig) {

0 commit comments

Comments
 (0)