Skip to content

Commit

Permalink
Merge 4ed69aa into 3ae6bc3
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanista authored Mar 20, 2024
2 parents 3ae6bc3 + 4ed69aa commit 2decf3f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
20 changes: 10 additions & 10 deletions src/metrics/extension.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Expand Down
14 changes: 7 additions & 7 deletions src/metrics/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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;
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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]),
});
Expand Down
16 changes: 8 additions & 8 deletions src/metrics/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -59,20 +59,20 @@ export class MetricsListener {
private currentProcessor?: Promise<Processor>;
private apiKey: Promise<string>;
private statsDClient?: StatsD;
private isAgentRunning?: boolean = undefined;
private isExtensionRunning?: boolean = undefined;

constructor(private kmsClient: KMSService, private config: MetricsConfig) {
this.apiKey = this.getAPIKey(config);
this.config = config;
}

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 });
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 2decf3f

Please sign in to comment.