Skip to content

Commit 2decf3f

Browse files
authored
Merge 4ed69aa into 3ae6bc3
2 parents 3ae6bc3 + 4ed69aa commit 2decf3f

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/metrics/extension.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import nock from "nock";
22

3-
import { isAgentRunning, flushExtension, AGENT_URL } from "./extension";
3+
import { isExtensionRunning, flushExtension, EXTENSION_URL } from "./extension";
44
import mock from "mock-fs";
55

6-
describe("isAgentRunning", () => {
6+
describe("isExtensionRunning", () => {
77
afterEach(() => {
88
mock.restore();
99
});
10-
it("returns true when agent exists and responds", async () => {
10+
it("returns true when extension exists and responds", async () => {
1111
mock({
1212
"/opt/extensions/datadog-agent": Buffer.from([0]),
1313
});
14-
const ran = await isAgentRunning();
14+
const ran = await isExtensionRunning();
1515
expect(ran).toBeTruthy();
1616
});
17-
it("returns false when agent doesn't exist", async () => {
17+
it("returns false when extension doesn't exist", async () => {
1818
mock({});
19-
const scope = nock(AGENT_URL).get("/lambda/hello").replyWithError("Unreachable");
20-
const ran = await isAgentRunning();
19+
const scope = nock(EXTENSION_URL).get("/lambda/hello").replyWithError("Unreachable");
20+
const ran = await isExtensionRunning();
2121
expect(scope.isDone()).toBeFalsy();
2222
expect(ran).toBeFalsy();
2323
});
2424
});
2525
describe("flushExtension", () => {
26-
it("calls flush on the agent", async () => {
27-
const scope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).reply(200);
26+
it("calls flush on the extension", async () => {
27+
const scope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).reply(200);
2828
await flushExtension();
2929
expect(scope.isDone()).toBeTruthy();
3030
});
3131
it("catches error when flush doesn't respond", async () => {
32-
const scope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).replyWithError("Unavailable");
32+
const scope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).replyWithError("Unavailable");
3333
await flushExtension();
3434
expect(scope.isDone()).toBeTruthy();
3535
});

src/metrics/extension.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { URL } from "url";
22
import { post, logDebug, logError } from "../utils";
33
import fs from "fs";
44

5-
export const AGENT_URL = "http://127.0.0.1:8124";
6-
const FLUSH_PATH = "/lambda/flush";
5+
export const EXTENSION_URL = "http://127.0.0.1:8124";
76
const EXTENSION_PATH = "/opt/extensions/datadog-agent";
8-
const AGENT_TIMEOUT_MS = 100;
7+
const LOCAL_FLUSH_PATH = "/lambda/flush";
8+
const LOCAL_FLUSH_TIMEOUT_MS = 100;
99

10-
export async function isAgentRunning() {
10+
export async function isExtensionRunning() {
1111
const extensionExists = await fileExists(EXTENSION_PATH);
1212
if (!extensionExists) {
13-
logDebug(`Agent isn't present in sandbox`);
13+
logDebug(`Extension Layer is not present`);
1414
return false;
1515
}
1616
return true;
1717
}
1818

1919
export async function flushExtension(): Promise<boolean> {
20-
const url = new URL(FLUSH_PATH, AGENT_URL);
21-
const result = await post(url, {}, { timeout: AGENT_TIMEOUT_MS });
20+
const url = new URL(LOCAL_FLUSH_PATH, EXTENSION_URL);
21+
const result = await post(url, {}, { timeout: LOCAL_FLUSH_TIMEOUT_MS });
2222
if (!result.success) {
2323
logError(`Failed to flush extension. ${result.errorMessage}`);
2424
return false;

src/metrics/listener.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import nock from "nock";
22
import mock from "mock-fs";
33

44
import { LogLevel, setLogLevel } from "../utils";
5-
import { AGENT_URL } from "./extension";
5+
import { EXTENSION_URL } from "./extension";
66

77
import { MetricsListener } from "./listener";
88
import StatsDClient from "hot-shots";
@@ -105,7 +105,7 @@ describe("MetricsListener", () => {
105105
expect(spy).toHaveBeenCalledWith(`{"e":1487076708,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`);
106106
});
107107
it("always sends metrics to statsD when extension is enabled, ignoring logForwarding=true", async () => {
108-
const flushScope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).reply(200);
108+
const flushScope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).reply(200);
109109
mock({
110110
"/opt/extensions/datadog-agent": Buffer.from([0]),
111111
});

src/metrics/listener.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { StatsD } from "hot-shots";
22
import { promisify } from "util";
33
import { logDebug, logError } from "../utils";
44
import { APIClient } from "./api";
5-
import { flushExtension, isAgentRunning } from "./extension";
5+
import { flushExtension, isExtensionRunning } from "./extension";
66
import { KMSService } from "./kms-service";
77
import { writeMetricToStdout } from "./metric-log";
88
import { Distribution } from "./model";
@@ -59,20 +59,20 @@ export class MetricsListener {
5959
private currentProcessor?: Promise<Processor>;
6060
private apiKey: Promise<string>;
6161
private statsDClient?: StatsD;
62-
private isAgentRunning?: boolean = undefined;
62+
private isExtensionRunning?: boolean = undefined;
6363

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

6969
public async onStartInvocation(_: any) {
70-
if (this.isAgentRunning === undefined) {
71-
this.isAgentRunning = await isAgentRunning();
72-
logDebug(`Extension present: ${this.isAgentRunning}`);
70+
if (this.isExtensionRunning === undefined) {
71+
this.isExtensionRunning = await isExtensionRunning();
72+
logDebug(`Extension present: ${this.isExtensionRunning}`);
7373
}
7474

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

7878
this.statsDClient = new StatsD({ host: "127.0.0.1", closingFlushInterval: 1 });
@@ -121,7 +121,7 @@ export class MetricsListener {
121121
}
122122
}
123123
try {
124-
if (this.isAgentRunning && this.config.localTesting) {
124+
if (this.isExtensionRunning && this.config.localTesting) {
125125
logDebug(`Flushing Extension for local test`);
126126
await flushExtension();
127127
}
@@ -140,7 +140,7 @@ export class MetricsListener {
140140
forceAsync: boolean,
141141
...tags: string[]
142142
) {
143-
if (this.isAgentRunning) {
143+
if (this.isExtensionRunning) {
144144
this.statsDClient?.distribution(name, value, undefined, tags);
145145
return;
146146
}

0 commit comments

Comments
 (0)