Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(meter): allow custom batcher #932 #933

Merged
merged 2 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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