diff --git a/src/metrics/extension.spec.ts b/src/metrics/extension.spec.ts index 25286f66..b3e5801a 100644 --- a/src/metrics/extension.spec.ts +++ b/src/metrics/extension.spec.ts @@ -1,35 +1,35 @@ import nock from "nock"; -import { isAgentRunning, flushExtension, AGENT_URL } from "./extension"; +import { isExtensionRunning, flushExtension, EXTENSION_URL } from "./extension"; import mock from "mock-fs"; -describe("isAgentRunning", () => { +describe("isExtensionRunning", () => { afterEach(() => { mock.restore(); }); - it("returns true when agent exists and responds", async () => { + it("returns true when extension exists and responds", async () => { mock({ "/opt/extensions/datadog-agent": Buffer.from([0]), }); - const ran = await isAgentRunning(); + const ran = await isExtensionRunning(); expect(ran).toBeTruthy(); }); - it("returns false when agent doesn't exist", async () => { + it("returns false when extension doesn't exist", async () => { mock({}); - const scope = nock(AGENT_URL).get("/lambda/hello").replyWithError("Unreachable"); - const ran = await isAgentRunning(); + const scope = nock(EXTENSION_URL).get("/lambda/hello").replyWithError("Unreachable"); + const ran = await isExtensionRunning(); expect(scope.isDone()).toBeFalsy(); expect(ran).toBeFalsy(); }); }); describe("flushExtension", () => { - it("calls flush on the agent", async () => { - const scope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).reply(200); + it("calls flush on the extension", async () => { + const scope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).reply(200); await flushExtension(); expect(scope.isDone()).toBeTruthy(); }); it("catches error when flush doesn't respond", async () => { - const scope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).replyWithError("Unavailable"); + const scope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).replyWithError("Unavailable"); await flushExtension(); expect(scope.isDone()).toBeTruthy(); }); diff --git a/src/metrics/extension.ts b/src/metrics/extension.ts index 67bcf0d0..4266d28d 100644 --- a/src/metrics/extension.ts +++ b/src/metrics/extension.ts @@ -2,23 +2,23 @@ import { URL } from "url"; import { post, logDebug, logError } from "../utils"; import fs from "fs"; -export const AGENT_URL = "http://127.0.0.1:8124"; -const FLUSH_PATH = "/lambda/flush"; +export const EXTENSION_URL = "http://127.0.0.1:8124"; const EXTENSION_PATH = "/opt/extensions/datadog-agent"; -const AGENT_TIMEOUT_MS = 100; +const LOCAL_FLUSH_PATH = "/lambda/flush"; +const LOCAL_FLUSH_TIMEOUT_MS = 100; -export async function isAgentRunning() { +export async function isExtensionRunning() { const extensionExists = await fileExists(EXTENSION_PATH); if (!extensionExists) { - logDebug(`Agent isn't present in sandbox`); + logDebug(`Extension Layer is not present`); return false; } return true; } export async function flushExtension(): Promise { - const url = new URL(FLUSH_PATH, AGENT_URL); - const result = await post(url, {}, { timeout: AGENT_TIMEOUT_MS }); + const url = new URL(LOCAL_FLUSH_PATH, EXTENSION_URL); + const result = await post(url, {}, { timeout: LOCAL_FLUSH_TIMEOUT_MS }); if (!result.success) { logError(`Failed to flush extension. ${result.errorMessage}`); return false; diff --git a/src/metrics/listener.spec.ts b/src/metrics/listener.spec.ts index eaf31575..233ad044 100644 --- a/src/metrics/listener.spec.ts +++ b/src/metrics/listener.spec.ts @@ -2,7 +2,7 @@ import nock from "nock"; import mock from "mock-fs"; import { LogLevel, setLogLevel } from "../utils"; -import { AGENT_URL } from "./extension"; +import { EXTENSION_URL } from "./extension"; import { MetricsListener } from "./listener"; import StatsDClient from "hot-shots"; @@ -105,7 +105,7 @@ describe("MetricsListener", () => { expect(spy).toHaveBeenCalledWith(`{"e":1487076708,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`); }); it("always sends metrics to statsD when extension is enabled, ignoring logForwarding=true", async () => { - const flushScope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).reply(200); + const flushScope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).reply(200); mock({ "/opt/extensions/datadog-agent": Buffer.from([0]), }); diff --git a/src/metrics/listener.ts b/src/metrics/listener.ts index 076e7b3e..d75b1023 100644 --- a/src/metrics/listener.ts +++ b/src/metrics/listener.ts @@ -2,7 +2,7 @@ import { StatsD } from "hot-shots"; import { promisify } from "util"; import { logDebug, logError } from "../utils"; import { APIClient } from "./api"; -import { flushExtension, isAgentRunning } from "./extension"; +import { flushExtension, isExtensionRunning } from "./extension"; import { KMSService } from "./kms-service"; import { writeMetricToStdout } from "./metric-log"; import { Distribution } from "./model"; @@ -59,7 +59,7 @@ export class MetricsListener { private currentProcessor?: Promise; private apiKey: Promise; private statsDClient?: StatsD; - private isAgentRunning?: boolean = undefined; + private isExtensionRunning?: boolean = undefined; constructor(private kmsClient: KMSService, private config: MetricsConfig) { this.apiKey = this.getAPIKey(config); @@ -67,12 +67,12 @@ export class MetricsListener { } public async onStartInvocation(_: any) { - if (this.isAgentRunning === undefined) { - this.isAgentRunning = await isAgentRunning(); - logDebug(`Extension present: ${this.isAgentRunning}`); + if (this.isExtensionRunning === undefined) { + this.isExtensionRunning = await isExtensionRunning(); + logDebug(`Extension present: ${this.isExtensionRunning}`); } - if (this.isAgentRunning) { + if (this.isExtensionRunning) { logDebug(`Using StatsD client`); this.statsDClient = new StatsD({ host: "127.0.0.1", closingFlushInterval: 1 }); @@ -121,7 +121,7 @@ export class MetricsListener { } } try { - if (this.isAgentRunning && this.config.localTesting) { + if (this.isExtensionRunning && this.config.localTesting) { logDebug(`Flushing Extension for local test`); await flushExtension(); } @@ -140,7 +140,7 @@ export class MetricsListener { forceAsync: boolean, ...tags: string[] ) { - if (this.isAgentRunning) { + if (this.isExtensionRunning) { this.statsDClient?.distribution(name, value, undefined, tags); return; }