Skip to content

Commit

Permalink
TEST: add trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Jul 10, 2024
1 parent 751a6ac commit a195f77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
13 changes: 13 additions & 0 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,23 @@ class CustomMetrics {
let dimensions = this.makeDimensionString(dim);
let old = await this.getMetric(owner, namespace, metricName, dimensions, options.log);
metric = this.upgradeMetric(old);
await this.putMetric(metric, options);
}
return metric;
}
upgradeMetric(old) {
let required = false;
if (this.spans.length == old.spans.length) {
for (let [index, span] of Object.entries(old.spans)) {
if (span.period != this.spans[index].period ||
span.samples != this.spans[index].samples) {
required = true;
}
}
if (!required) {
return old;
}
}
let timestamp = old.spans[0].end || Math.floor(Date.now() / 1000);
let metric = this.initMetric(old.owner, old.namespace, old.metric, old.dimensions, timestamp);
for (let span of old.spans) {
Expand Down
13 changes: 13 additions & 0 deletions dist/mjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,23 @@ export class CustomMetrics {
let dimensions = this.makeDimensionString(dim);
let old = await this.getMetric(owner, namespace, metricName, dimensions, options.log);
metric = this.upgradeMetric(old);
await this.putMetric(metric, options);
}
return metric;
}
upgradeMetric(old) {
let required = false;
if (this.spans.length == old.spans.length) {
for (let [index, span] of Object.entries(old.spans)) {
if (span.period != this.spans[index].period ||
span.samples != this.spans[index].samples) {
required = true;
}
}
if (!required) {
return old;
}
}
let timestamp = old.spans[0].end || Math.floor(Date.now() / 1000);
let metric = this.initMetric(old.owner, old.namespace, old.metric, old.dimensions, timestamp);
for (let span of old.spans) {
Expand Down
6 changes: 0 additions & 6 deletions test/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const MoreSpans: SpanDef[] = [
test('Upgrade Spans', async () => {
let metrics = new CustomMetrics({client, table, log: true})
let timestamp = new Date(2000, 0, 1).getTime()
console.log("TS", timestamp)

let metric
for (let i = 0; i < 140; i++) {
Expand All @@ -38,23 +37,19 @@ test('Upgrade Spans', async () => {
expect(metric.spans[1].points.length).toBe(12)
expect(metric.spans[2].points.length).toBe(1)
expect(metric.spans[3].points.length).toBe(0)
dump(metric)

// Test upgrade
metrics = new CustomMetrics({client, table, log: true, spans: MoreSpans})
metric = await metrics.upgrade('test/upgrade', 'UpMetric', [])
dump(metric)
expect(metric).toBeDefined()
expect(metric.spans.length).toBe(9)
expect(metric.spans[0].points.length).toBe(0)
expect(metric.spans[1].points.length).toBe(10)
expect(metric.spans[2].points.length).toBe(0)
expect(metric.spans[3].points.length).toBe(9)

// Test downgrade
metrics = new CustomMetrics({client, table, log: true, spans: LessSpans})
metric = await metrics.emit('test/upgrade', 'UpMetric', 7, [], {upgrade: true})
dump(metric)
expect(metric).toBeDefined()
expect(metric.spans.length).toBe(2)
expect(metric.spans[0].points.length).toBe(1)
Expand All @@ -67,6 +62,5 @@ test('Upgrade Spans', async () => {
// Test already upgraded
metrics = new CustomMetrics({client, table, log: true, spans: LessSpans})
metric = await metrics.emit('test/upgrade', 'UpMetric', 7, [], {upgrade: true})
dump(metric)
expect(metric).toBeDefined()
})

0 comments on commit a195f77

Please sign in to comment.