Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: lazy load internal request module #519

Merged
merged 18 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration_tests/snapshots/logs/sync-metrics_node16.log
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed APIGat
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms (init: XXXX ms) Memory Used: XXXX MB
START
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request
{
"e": XXXX,
"m": "aws.lambda.enhanced.invocations",
Expand All @@ -34,10 +35,10 @@ START
],
"v": 1
}
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms Memory Used: XXXX MB
START
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SQS request
{
"e": XXXX,
"m": "aws.lambda.enhanced.invocations",
Expand All @@ -53,6 +54,5 @@ START
],
"v": 1
}
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SQS request
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms Memory Used: XXXX MB
2 changes: 1 addition & 1 deletion integration_tests/snapshots/logs/sync-metrics_node18.log
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed APIGat
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms (init: XXXX ms) Memory Used: XXXX MB
START
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request
{
"e": XXXX,
"m": "aws.lambda.enhanced.invocations",
Expand All @@ -34,7 +35,6 @@ START
],
"v": 1
}
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms Memory Used: XXXX MB
START
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/snapshots/logs/sync-metrics_node20.log
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed APIGat
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms (init: XXXX ms) Memory Used: XXXX MB
START
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request
{
"e": XXXX,
"m": "aws.lambda.enhanced.invocations",
Expand All @@ -35,9 +34,11 @@ XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS re
],
"v": 1
}
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms Memory Used: XXXX MB
START
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SQS request
{
"e": XXXX,
"m": "aws.lambda.enhanced.invocations",
Expand All @@ -53,6 +54,5 @@ START
],
"v": 1
}
XXXX-XX-XX XX:XX:XX.XXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SQS request
HTTP POST https://api.datadoghq.com/api/v1/distribution_points?api_key=XXXX TraceHeaders: ["x-datadog-parent-id:XXXX","x-datadog-sampling-priority:-1","x-datadog-trace-id:XXXX"]
END Duration: XXXX ms Memory Used: XXXX MB
3 changes: 2 additions & 1 deletion src/metrics/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import querystring from "querystring";
import { URL } from "url";

import { logDebug, post } from "../utils";
import { logDebug } from "../utils";
import { post } from "../utils/request";
import { APIMetric } from "./model";

const API_KEY_QUERY_PARAM = "api_key";
Expand Down
8 changes: 4 additions & 4 deletions src/metrics/extension.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import nock from "nock";

import { isExtensionRunning, flushExtension, EXTENSION_URL } from "./extension";
import { isExtensionRunning, EXTENSION_URL, flushExtension } from "./extension";
import mock from "mock-fs";

describe("isExtensionRunning", () => {
Expand All @@ -22,15 +21,16 @@ describe("isExtensionRunning", () => {
expect(ran).toBeFalsy();
});
});

describe("flushExtension", () => {
it("calls flush on the extension", async () => {
const scope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).reply(200);
await flushExtension();
await flushExtension(true);
expect(scope.isDone()).toBeTruthy();
});
it("catches error when flush doesn't respond", async () => {
const scope = nock(EXTENSION_URL).post("/lambda/flush", JSON.stringify({})).replyWithError("Unavailable");
await flushExtension();
await flushExtension(true);
expect(scope.isDone()).toBeTruthy();
});
});
37 changes: 24 additions & 13 deletions src/metrics/extension.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { URL } from "url";
import { post, logDebug, logError } from "../utils";
import { logDebug, logError } from "../utils";
import fs from "fs";

export const EXTENSION_URL = "http://127.0.0.1:8124";
const EXTENSION_PATH = "/opt/extensions/datadog-agent";
const LOCAL_FLUSH_PATH = "/lambda/flush";
const LOCAL_FLUSH_TIMEOUT_MS = 100;
const LOCAL_FLUSH_PATH = "/lambda/flush";

export async function isExtensionRunning() {
const extensionExists = await fileExists(EXTENSION_PATH);
if (!extensionExists) {
logDebug(`Extension Layer is not present`);
return false;
}
return true;
}

export async function flushExtension(): Promise<boolean> {
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}`);
logDebug(`Extension Layer is not present.`);
return false;
}
return true;
Expand All @@ -32,3 +22,24 @@ function fileExists(filename: string): Promise<boolean> {
.then(() => true)
.catch(() => false);
}

export async function flushExtension(localTesting = false) {
if (localTesting) {
try {
const { post } = require("../utils/request");
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 true;
} catch (error) {
if (error instanceof Error) {
logError("Failed to flush extension", error);
}
}
}

return false;
}
4 changes: 2 additions & 2 deletions src/metrics/listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("MetricsListener", () => {
siteURL,
});

listener.onStartInvocation({});
await listener.onStartInvocation({});
listener.sendDistributionMetric("my-metric", 10, false, "tag:a", "tag:b");
await expect(listener.onCompleteInvocation()).resolves.toEqual(undefined);
});
Expand Down Expand Up @@ -153,7 +153,7 @@ describe("MetricsListener", () => {
});
// jest.useFakeTimers();

listener.onStartInvocation({});
await listener.onStartInvocation({});
listener.sendDistributionMetricWithDate("my-metric", 10, new Date(1584983836 * 1000), false, "tag:a", "tag:b");
await listener.onCompleteInvocation();

Expand Down
38 changes: 19 additions & 19 deletions src/metrics/listener.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { StatsD } from "hot-shots";
import { promisify } from "util";
import { logDebug, logError } from "../utils";
import { APIClient } from "./api";
import { flushExtension, isExtensionRunning } from "./extension";
import { KMSService } from "./kms-service";
import { writeMetricToStdout } from "./metric-log";
import { Distribution } from "./model";
import { Processor } from "./processor";

const metricsBatchSendIntervalMS = 10000; // 10 seconds
const METRICS_BATCH_SEND_INTERVAL = 10000; // 10 seconds

export interface MetricsConfig {
/**
Expand Down Expand Up @@ -56,7 +54,7 @@ export interface MetricsConfig {
}

export class MetricsListener {
private currentProcessor?: Promise<Processor>;
private currentProcessor?: Promise<any>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, this has to stay as it, so we don't import and load the modules that add 1MB of memory allocation.

private apiKey: Promise<string>;
private statsDClient?: StatsD;
private isExtensionRunning?: boolean = undefined;
Expand All @@ -83,6 +81,7 @@ export class MetricsListener {

return;
}

this.currentProcessor = this.createProcessor(this.config, this.apiKey);
}

Expand Down Expand Up @@ -120,15 +119,11 @@ export class MetricsListener {
logError("failed to flush metrics", error as Error);
}
}
try {
if (this.isExtensionRunning && this.config.localTesting) {
logDebug(`Flushing Extension for local test`);
await flushExtension();
}
} catch (error) {
if (error instanceof Error) {
logError("failed to flush extension", error as Error);
}

// Flush only when testing extension locally.
// Passing config flag so we can lazy load the request module.
if (this.isExtensionRunning) {
await flushExtension(this.config.localTesting);
}
this.currentProcessor = undefined;
}
Expand Down Expand Up @@ -171,12 +166,17 @@ export class MetricsListener {
}

private async createProcessor(config: MetricsConfig, apiKey: Promise<string>) {
const key = await apiKey;
const url = `https://api.${config.siteURL}`;
const apiClient = new APIClient(key, url);
const processor = new Processor(apiClient, metricsBatchSendIntervalMS, config.shouldRetryMetrics);
processor.startProcessing();
return processor;
if (!this.isExtensionRunning && !this.config.logForwarding) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

const { APIClient } = require("./api");
const { Processor } = require("./processor");

const key = await apiKey;
const url = `https://api.${config.siteURL}`;
const apiClient = new APIClient(key, url);
const processor = new Processor(apiClient, METRICS_BATCH_SEND_INTERVAL, config.shouldRetryMetrics);
processor.startProcessing();
return processor;
}
}

private async getAPIKey(config: MetricsConfig) {
Expand Down
2 changes: 2 additions & 0 deletions src/trace/patch-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function unpatchHttp() {
}

function patchMethod(mod: typeof http | typeof https, method: "get" | "request", contextService: TraceContextService) {
if (mod[method].__wrapped !== undefined) return; // Only patch once
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was missed, required so no repeated logs appear.


shimmer.wrap(mod, method, (original) => {
const fn = (arg1: any, arg2: any, arg3: any) => {
[arg1, arg2, arg3] = addTraceContextToArgs(contextService, arg1, arg2, arg3);
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export { didFunctionColdStart, getSandboxInitTags, setSandboxInit, isProactiveIn
export { wrap, promisifiedHandler } from "./handler";
export { Timer } from "./timer";
export { logError, logDebug, Logger, setLogLevel, setLogger, LogLevel } from "./log";
export { get, post } from "./request";
export { tagObject } from "./tag-object";
Loading