Skip to content
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
25 changes: 21 additions & 4 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PassThrough } from "stream";
import { DatadogTraceHeaders } from "./trace/context/extractor";
import { SpanContextWrapper } from "./trace/span-context-wrapper";
import { TraceSource } from "./trace/trace-context-service";
import { inflateSync } from "zlib";

jest.mock("./metrics/enhanced-metrics");

Expand Down Expand Up @@ -141,7 +142,11 @@ describe("datadog", () => {
process.env[apiKeyVar] = apiKey;

nock("https://api.datadoghq.com")
.post(`/api/v1/distribution_points?api_key=${apiKey}`, (request: any) => request.series[0].metric === "my-dist")
.post(
`/api/v1/distribution_points?api_key=${apiKey}`,
(request: any) =>
JSON.parse(inflateSync(Buffer.from(request, "hex")).toString()).series[0].metric === "my-dist",
)
.reply(200, {});

const wrapped = datadog(
Expand All @@ -163,7 +168,11 @@ describe("datadog", () => {
const apiKey = "101112";

nock("https://api.datadoghq.com")
.post(`/api/v1/distribution_points?api_key=${apiKey}`, (request: any) => request.series[0].metric === "my-dist")
.post(
`/api/v1/distribution_points?api_key=${apiKey}`,
(request: any) =>
JSON.parse(inflateSync(Buffer.from(request, "hex")).toString()).series[0].metric === "my-dist",
)
.reply(200, {});

const wrapped = datadog(
Expand All @@ -185,7 +194,11 @@ describe("datadog", () => {
process.env[siteEnvVar] = site;

nock("https://api.datadoghq.com")
.post(`/api/v1/distribution_points?api_key=${apiKey}`, (request: any) => request.series[0].metric === "my-dist")
.post(
`/api/v1/distribution_points?api_key=${apiKey}`,
(request: any) =>
JSON.parse(inflateSync(Buffer.from(request, "hex")).toString()).series[0].metric === "my-dist",
)
.reply(200, {});

const wrapped = datadog(
Expand All @@ -207,7 +220,11 @@ describe("datadog", () => {
process.env[siteEnvVar] = site;

nock("https://api.datadoghq.com")
.post(`/api/v1/distribution_points?api_key=${apiKey}`, (request: any) => request.series[0].metric === "my-dist")
.post(
`/api/v1/distribution_points?api_key=${apiKey}`,
(request: any) =>
JSON.parse(inflateSync(Buffer.from(request, "hex")).toString()).series[0].metric === "my-dist",
)
.reply(200, {});

const wrapped = datadog(
Expand Down
13 changes: 10 additions & 3 deletions src/metrics/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import nock from "nock";

import { APIClient } from "./api";
import { APIMetric } from "./model";
import { deflateSync } from "zlib";

const baseAPIURL = "https://www.example.com";

Expand Down Expand Up @@ -31,7 +32,9 @@ describe("APIClient", () => {
];

const scope = nock(baseAPIURL)
.post("/api/v1/distribution_points?api_key=api_key", JSON.stringify({ series: input }))
.post("/api/v1/distribution_points?api_key=api_key", deflateSync(JSON.stringify({ series: input })), {
reqheaders: { "content-encoding": "deflate" },
})
.reply(200);
const client = new APIClient("api_key", baseAPIURL);

Expand All @@ -41,7 +44,9 @@ describe("APIClient", () => {

it("throws an authentication error on authentication failure", async () => {
const scope = nock(baseAPIURL)
.post("/api/v1/distribution_points?api_key=bad_api_key", JSON.stringify({ series: [] }))
.post("/api/v1/distribution_points?api_key=bad_api_key", deflateSync(JSON.stringify({ series: [] })), {
reqheaders: { "content-encoding": "deflate" },
})
.reply(403);
const client = new APIClient("bad_api_key", baseAPIURL);
await expect(client.sendMetrics([])).rejects.toMatchInlineSnapshot(`"HTTP error code: 403"`);
Expand All @@ -50,7 +55,9 @@ describe("APIClient", () => {

it("throws an error on connection error failure", async () => {
const scope = nock(baseAPIURL)
.post("/api/v1/distribution_points?api_key=api_key", JSON.stringify({ series: [] }))
.post("/api/v1/distribution_points?api_key=api_key", deflateSync(JSON.stringify({ series: [] })), {
reqheaders: { "content-encoding": "deflate" },
})
.replyWithError("Connection closed");
const client = new APIClient("api_key", baseAPIURL);
await expect(client.sendMetrics([])).rejects.toMatchInlineSnapshot(`"Connection closed"`);
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class APIClient implements Client {
constructor(private apiKey: string, private baseAPIURL: string) {}

public async sendMetrics(metrics: APIMetric[]): Promise<void> {
const result = await post(this.getUrl("api/v1/distribution_points"), { series: metrics });
const result = await post(this.getUrl("api/v1/distribution_points"), { series: metrics }, {}, true);
if (result.success) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/metrics/listener.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AWSError, KMS, Request } from "aws-sdk";
import nock from "nock";
import mock from "mock-fs";

Expand Down
19 changes: 17 additions & 2 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ import https, { RequestOptions } from "https";
import http from "http";
import { URL } from "url";
import { logDebug } from "./log";
import { deflateSync } from "zlib";

type RequestResult = {
success: boolean;
statusCode?: number;
errorMessage?: string;
};

export function post<T>(url: URL, body: T, options?: Partial<RequestOptions>): Promise<RequestResult> {
export function post<T>(
url: URL,
body: T,
options?: Partial<RequestOptions>,
compressPayload?: boolean,
): Promise<RequestResult> {
const bodyJSON = JSON.stringify(body);
const buffer = Buffer.from(bodyJSON);
let buffer = Buffer.from(bodyJSON);

if (compressPayload) {
// Adding compression header
options = options || {}; // Define options object if not already defined
options.headers = { ...options.headers, "Content-Encoding": "deflate" };

buffer = deflateSync(buffer);
}

logDebug(`sending payload with body ${bodyJSON}`);
const requestOptions: RequestOptions = {
headers: { "content-type": "application/json" },
Expand Down