Skip to content

Commit

Permalink
DEV: fix TTL on termination and default dimensions on emit()
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Jun 24, 2024
1 parent f5ac357 commit 76dc5ca
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 219 deletions.
11 changes: 10 additions & 1 deletion dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class CustomMetrics {
if (value == undefined || value == null) {
throw new Error('Invalid metric value');
}
if (dimensionsList.length == 0) {
dimensionsList = [{}];
}
value = Number(value);
if (isNaN(value)) {
throw new Error(`Value to emit is not valid`);
Expand Down Expand Up @@ -224,7 +227,13 @@ class CustomMetrics {
}
async flushElt(elt) {
let point = { count: elt.count, sum: elt.sum, timestamp: elt.timestamp };
await this.emitDimensionedMetric(elt.namespace, elt.metric, point, elt.dimensions, { timestamp: elt.timestamp });
let timestamp = Date.now() / 1000;
if (timestamp > elt.timestamp) {
timestamp = elt.timestamp;
}
await this.emitDimensionedMetric(elt.namespace, elt.metric, point, elt.dimensions, {
timestamp: timestamp * 1000
});
}
getBufferKey(namespace, metricName, dimensions) {
return `${namespace}|${metricName}|${JSON.stringify(dimensions)}`;
Expand Down
11 changes: 10 additions & 1 deletion dist/mjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export class CustomMetrics {
if (value == undefined || value == null) {
throw new Error('Invalid metric value');
}
if (dimensionsList.length == 0) {
dimensionsList = [{}];
}
value = Number(value);
if (isNaN(value)) {
throw new Error(`Value to emit is not valid`);
Expand Down Expand Up @@ -232,7 +235,13 @@ export class CustomMetrics {
}
async flushElt(elt) {
let point = { count: elt.count, sum: elt.sum, timestamp: elt.timestamp };
await this.emitDimensionedMetric(elt.namespace, elt.metric, point, elt.dimensions, { timestamp: elt.timestamp });
let timestamp = Date.now() / 1000;
if (timestamp > elt.timestamp) {
timestamp = elt.timestamp;
}
await this.emitDimensionedMetric(elt.namespace, elt.metric, point, elt.dimensions, {
timestamp: timestamp * 1000
});
}
getBufferKey(namespace, metricName, dimensions) {
return `${namespace}|${metricName}|${JSON.stringify(dimensions)}`;
Expand Down
Loading

0 comments on commit 76dc5ca

Please sign in to comment.