Skip to content

Commit 035b0c7

Browse files
authored
Merge pull request #48 from DataDog/stephenf/fix-async-metric-timestamps
Fix timestamps for async metrics
2 parents 61b7890 + da17a76 commit 035b0c7

5 files changed

+29
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "datadog-lambda-js",
3-
"version": "0.13.0",
3+
"version": "0.14.0",
44
"description": "Lambda client library that supports hybrid tracing in node js",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/metrics/build-metric-log.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { buildMetricLog } from "./build-metric-log";
2+
3+
describe("buildMetricLog", () => {
4+
jest.spyOn(Date, "now").mockImplementation(() => 1487076708123);
5+
it("handles empty tag list", () => {
6+
expect(buildMetricLog("my.test.metric", 1337, [])).toStrictEqual(
7+
'{"e":1487076708.123,"m":"my.test.metric","t":[],"v":1337}\n',
8+
);
9+
});
10+
it("writes timestamp in Unix seconds", () => {
11+
expect(buildMetricLog("my.test.metric", 1337, ["region:us", "account:dev", "team:serverless"])).toStrictEqual(
12+
'{"e":1487076708.123,"m":"my.test.metric","t":["region:us","account:dev","team:serverless"],"v":1337}\n',
13+
);
14+
});
15+
});

src/metrics/build-metric-log.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Builds the string representation of the metric that will be written to logs
2+
export function buildMetricLog(name: string, value: number, tags: string[]) {
3+
return `${JSON.stringify({
4+
// Date.now() returns Unix time in milliseconds, we convert to seconds for DD API submission
5+
e: Date.now() / 1000,
6+
m: name,
7+
t: tags,
8+
v: value,
9+
})}\n`;
10+
}

src/metrics/listener.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ describe("MetricsListener", () => {
9898
listener.sendDistributionMetric("my-metric", 10, "tag:a", "tag:b");
9999
await listener.onCompleteInvocation();
100100

101-
expect(spy).toHaveBeenCalledWith(`{"e":1487076708000,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`);
101+
expect(spy).toHaveBeenCalledWith(`{"e":1487076708,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`);
102102
});
103103
});

src/metrics/listener.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { promisify } from "util";
22

33
import { logDebug, logError } from "../utils";
44
import { APIClient } from "./api";
5+
import { buildMetricLog } from "./build-metric-log";
56
import { KMSService } from "./kms-service";
67
import { Distribution } from "./model";
78
import { Processor } from "./processor";
@@ -86,14 +87,7 @@ export class MetricsListener {
8687
if (this.config.logForwarding) {
8788
// We use process.stdout.write, because console.log will prepend metadata to the start
8889
// of the log that log forwarder doesn't know how to read.
89-
process.stdout.write(
90-
`${JSON.stringify({
91-
e: Date.now(),
92-
m: name,
93-
t: tags,
94-
v: value,
95-
})}\n`,
96-
);
90+
process.stdout.write(buildMetricLog(name, value, tags));
9791
return;
9892
}
9993
const dist = new Distribution(name, [{ timestamp: new Date(), value }], ...tags);

0 commit comments

Comments
 (0)