Skip to content

Commit

Permalink
feat(meter): allow custom batcher #932 (#933)
Browse files Browse the repository at this point in the history
Co-authored-by: Mayur Kale <mayurkale@google.com>
  • Loading branch information
vmarchaud and mayurkale22 committed Apr 7, 2020
1 parent 41509f0 commit 1b62c51
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Meter implements types.Meter {
*/
constructor(config: MeterConfig = DEFAULT_CONFIG) {
this._logger = config.logger || new ConsoleLogger(config.logLevel);
this._batcher = new UngroupedBatcher();
this._batcher = config.batcher ?? new UngroupedBatcher();
this._resource = config.resource || Resource.createTelemetrySDKResource();
// start the push controller
const exporter = config.exporter || new NoopExporter();
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-metrics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { LogLevel } from '@opentelemetry/core';
import { Logger, ValueType } from '@opentelemetry/api';
import { MetricExporter } from './export/types';
import { Resource } from '@opentelemetry/resources';
import { Batcher } from './export/Batcher';

/** Options needed for SDK metric creation. */
export interface MetricOptions {
Expand Down Expand Up @@ -68,6 +69,9 @@ export interface MeterConfig {

/** Resource associated with metric telemetry */
resource?: Resource;

/** Metric batcher. */
batcher?: Batcher;
}

/** Default Meter configuration. */
Expand Down
21 changes: 21 additions & 0 deletions packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Distribution,
ObserverMetric,
MetricRecord,
Aggregator,
} from '../src';
import * as types from '@opentelemetry/api';
import { NoopLogger, hrTime, hrTimeToNanoseconds } from '@opentelemetry/core';
Expand All @@ -36,6 +37,7 @@ import {
import { ValueType } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { hashLabels } from '../src/Utils';
import { Batcher } from '../src/export/Batcher';

describe('Meter', () => {
let meter: Meter;
Expand Down Expand Up @@ -538,8 +540,27 @@ describe('Meter', () => {
assert.strictEqual(value, 10);
});
});

it('should allow custom batcher', () => {
const customMeter = new MeterProvider().getMeter('custom-batcher', '*', {
batcher: new CustomBatcher(),
});
assert.throws(() => {
const measure = customMeter.createMeasure('myMeasure');
measure.bind({}).record(1);
}, /aggregatorFor method not implemented/);
});
});

class CustomBatcher extends Batcher {
process(record: MetricRecord): void {
throw new Error('process method not implemented.');
}
aggregatorFor(metricKind: MetricKind): Aggregator {
throw new Error('aggregatorFor method not implemented.');
}
}

function ensureMetric(metric: MetricRecord) {
assert.ok(metric.aggregator instanceof ObserverAggregator);
assert.ok(
Expand Down

0 comments on commit 1b62c51

Please sign in to comment.