Skip to content

Commit

Permalink
feat: enforce minimum max size
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Dec 20, 2022
1 parent b710d4f commit f40abac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
DataPointType,
ExponentialHistogramMetricData,
} from '../export/MetricData';
import { HrTime } from '@opentelemetry/api';
import { diag, HrTime } from '@opentelemetry/api';
import { InstrumentDescriptor, InstrumentType } from '../InstrumentDescriptor';
import { Maybe } from '../utils';
import { AggregationTemporality } from '../export/AggregationTemporality';
Expand Down Expand Up @@ -68,6 +68,7 @@ class HighLow {

export class ExponentialHistogramAccumulation implements Accumulation {
static DEFAULT_MAX_SIZE = 160;
static MIN_MAX_SIZE = 2;

constructor(
public startTime: HrTime = startTime,
Expand All @@ -81,7 +82,14 @@ export class ExponentialHistogramAccumulation implements Accumulation {
private _positive = new Buckets(),
private _negative = new Buckets(),
private _mapping: Mapping = LogarithmMapping.get(LogarithmMapping.MAX_SCALE)
) {}
) {
if (this._maxSize < ExponentialHistogramAccumulation.MIN_MAX_SIZE) {
diag.warn(`Exponential Histogram Max Size set to ${this._maxSize}, \
changing to the minimum size of: \
${ExponentialHistogramAccumulation.MIN_MAX_SIZE}`);
this._maxSize = ExponentialHistogramAccumulation.MIN_MAX_SIZE;
}
}

/**
* record updates a histogram with a single count
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-metrics/test/aggregator/ExponentialHistogram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,16 @@ describe('ExponentialHistogramAccumulation', () => {
assert.deepStrictEqual(pv.negative.bucketCounts, acc.negative().counts());
});
});

describe('min max size', () => {
it('auto-corrects to min max', () => {
const acc: any = new ExponentialHistogramAccumulation([0, 0], 0);
assert.strictEqual(
acc['_maxSize'],
ExponentialHistogramAccumulation.MIN_MAX_SIZE
);
});
});
});

describe('ExponentialHistogramAggregation', () => {
Expand Down

0 comments on commit f40abac

Please sign in to comment.