Skip to content

Commit

Permalink
Merge pull request #48 from DataDog/stephenf/fix-async-metric-timestamps
Browse files Browse the repository at this point in the history
Fix timestamps for async metrics
  • Loading branch information
sfirrin authored Jan 22, 2020
2 parents 61b7890 + da17a76 commit 035b0c7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datadog-lambda-js",
"version": "0.13.0",
"version": "0.14.0",
"description": "Lambda client library that supports hybrid tracing in node js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions src/metrics/build-metric-log.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { buildMetricLog } from "./build-metric-log";

describe("buildMetricLog", () => {
jest.spyOn(Date, "now").mockImplementation(() => 1487076708123);
it("handles empty tag list", () => {
expect(buildMetricLog("my.test.metric", 1337, [])).toStrictEqual(
'{"e":1487076708.123,"m":"my.test.metric","t":[],"v":1337}\n',
);
});
it("writes timestamp in Unix seconds", () => {
expect(buildMetricLog("my.test.metric", 1337, ["region:us", "account:dev", "team:serverless"])).toStrictEqual(
'{"e":1487076708.123,"m":"my.test.metric","t":["region:us","account:dev","team:serverless"],"v":1337}\n',
);
});
});
10 changes: 10 additions & 0 deletions src/metrics/build-metric-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Builds the string representation of the metric that will be written to logs
export function buildMetricLog(name: string, value: number, tags: string[]) {
return `${JSON.stringify({
// Date.now() returns Unix time in milliseconds, we convert to seconds for DD API submission
e: Date.now() / 1000,
m: name,
t: tags,
v: value,
})}\n`;
}
2 changes: 1 addition & 1 deletion src/metrics/listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ describe("MetricsListener", () => {
listener.sendDistributionMetric("my-metric", 10, "tag:a", "tag:b");
await listener.onCompleteInvocation();

expect(spy).toHaveBeenCalledWith(`{"e":1487076708000,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`);
expect(spy).toHaveBeenCalledWith(`{"e":1487076708,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`);
});
});
10 changes: 2 additions & 8 deletions src/metrics/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { promisify } from "util";

import { logDebug, logError } from "../utils";
import { APIClient } from "./api";
import { buildMetricLog } from "./build-metric-log";
import { KMSService } from "./kms-service";
import { Distribution } from "./model";
import { Processor } from "./processor";
Expand Down Expand Up @@ -86,14 +87,7 @@ export class MetricsListener {
if (this.config.logForwarding) {
// We use process.stdout.write, because console.log will prepend metadata to the start
// of the log that log forwarder doesn't know how to read.
process.stdout.write(
`${JSON.stringify({
e: Date.now(),
m: name,
t: tags,
v: value,
})}\n`,
);
process.stdout.write(buildMetricLog(name, value, tags));
return;
}
const dist = new Distribution(name, [{ timestamp: new Date(), value }], ...tags);
Expand Down

0 comments on commit 035b0c7

Please sign in to comment.