From dbc4ca416191ed354379f249629100bf706176b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 20 Mar 2024 00:09:28 -0400 Subject: [PATCH 1/4] rename `agent` to `extension` --- src/metrics/extension.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/metrics/extension.ts b/src/metrics/extension.ts index 67bcf0d0..c93b0b96 100644 --- a/src/metrics/extension.ts +++ b/src/metrics/extension.ts @@ -2,12 +2,12 @@ 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`); @@ -17,8 +17,8 @@ export async function isAgentRunning() { } 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; From 1cd1b89d8511d25b8a897d09d1c8080657e8d08c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 20 Mar 2024 00:09:37 -0400 Subject: [PATCH 2/4] update imports --- src/metrics/listener.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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; } From 8193896d8198ab766920949ce791b356b1f529af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 20 Mar 2024 00:09:44 -0400 Subject: [PATCH 3/4] update tests --- src/metrics/extension.spec.ts | 14 +++++++------- src/metrics/listener.spec.ts | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/metrics/extension.spec.ts b/src/metrics/extension.spec.ts index 25286f66..378a1924 100644 --- a/src/metrics/extension.spec.ts +++ b/src/metrics/extension.spec.ts @@ -1,9 +1,9 @@ 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(); }); @@ -11,25 +11,25 @@ describe("isAgentRunning", () => { 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 () => { 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); + 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/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]), }); From 4ed69aaa5aff120d4f501219f2d38807fda5ecff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jordan=20gonz=C3=A1lez?= <30836115+duncanista@users.noreply.github.com> Date: Wed, 20 Mar 2024 00:11:50 -0400 Subject: [PATCH 4/4] more renaming --- src/metrics/extension.spec.ts | 6 +++--- src/metrics/extension.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/metrics/extension.spec.ts b/src/metrics/extension.spec.ts index 378a1924..b3e5801a 100644 --- a/src/metrics/extension.spec.ts +++ b/src/metrics/extension.spec.ts @@ -7,14 +7,14 @@ 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 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(EXTENSION_URL).get("/lambda/hello").replyWithError("Unreachable"); const ran = await isExtensionRunning(); @@ -23,7 +23,7 @@ describe("isExtensionRunning", () => { }); }); describe("flushExtension", () => { - it("calls flush on the agent", async () => { + 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(); diff --git a/src/metrics/extension.ts b/src/metrics/extension.ts index c93b0b96..4266d28d 100644 --- a/src/metrics/extension.ts +++ b/src/metrics/extension.ts @@ -10,7 +10,7 @@ const LOCAL_FLUSH_TIMEOUT_MS = 100; 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;