Skip to content

Commit

Permalink
Merge branch 'main' into split-exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
rauno56 committed Nov 2, 2021
2 parents 353d038 + 4e311bd commit e068039
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ describe('PrometheusExporter', () => {
exporter = new PrometheusExporter({}, () => {
meterProvider = new MeterProvider({
interval: Math.pow(2, 31) - 1,
});
meter = meterProvider.getMeter('test-prometheus', '1', {
exporter,
});
meter = meterProvider.getMeter('test-prometheus', '1');
done();
});
});
Expand Down Expand Up @@ -363,25 +362,15 @@ describe('PrometheusExporter', () => {
counter.bind({ counterKey1: 'labelValue2' }).add(20);
counter.bind({ counterKey1: 'labelValue3' }).add(30);
meterProvider.shutdown().then(() => {
// exporter has been shut down along with meter provider.
http
.get('http://localhost:9464/metrics', res => {
res.on('data', chunk => {
const body = chunk.toString();
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter_total a test description',
'# TYPE counter_total counter',
`counter_total{counterKey1="labelValue1"} 10 ${mockedHrTimeMs}`,
`counter_total{counterKey1="labelValue2"} 20 ${mockedHrTimeMs}`,
`counter_total{counterKey1="labelValue3"} 30 ${mockedHrTimeMs}`,
'',
]);

done();
});
errorHandler(done)(new Error('unreachable'));
})
.on('error', errorHandler(done));
.on('error', err => {
assert(`${err}`.match('ECONNREFUSED'));
done();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ export class MeterProvider implements api.MeterProvider {
*
* @returns Meter A Meter with the given name and version
*/
getMeter(name: string, version?: string, config?: MeterConfig): Meter {
const key = `${name}@${version ?? ''}:${config?.schemaUrl ?? ''}`;
getMeter(name: string, version?: string, options?: api.MeterOptions): Meter {
const key = `${name}@${version ?? ''}:${options?.schemaUrl ?? ''}`;
if (!this._meters.has(key)) {
this._meters.set(
key,
new Meter({
name,
version,
// @ts-expect-error ts(2345) TODO: upgrade @opentelemetry/core InstrumentationLibrary definition
schemaUrl: config?.schemaUrl
}, Object.assign({}, this._config, config))
schemaUrl: options?.schemaUrl
}, this._config)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
Aggregator,
CounterMetric,
Histogram,
LastValue,
LastValueAggregator,
Meter,
MeterProvider,
Metric,
MetricDescriptor,
MetricKind,
MetricRecord,
Sum,
Expand All @@ -40,7 +38,6 @@ import {
import { BatchObserver } from '../src/BatchObserver';
import { BatchObserverResult } from '../src/BatchObserverResult';
import { SumAggregator } from '../src/export/aggregators';
import { Processor } from '../src/export/Processor';
import { ObservableCounterMetric } from '../src/ObservableCounterMetric';
import { ObservableUpDownCounterMetric } from '../src/ObservableUpDownCounterMetric';
import { hashLabels } from '../src/Utils';
Expand Down Expand Up @@ -1364,28 +1361,8 @@ describe('Meter', () => {
assert.strictEqual(value, 10);
});
});

it('should allow custom processor', () => {
const customMeter = new MeterProvider().getMeter('custom-processor', '*', {
processor: new CustomProcessor(),
});
assert.throws(() => {
const histogram = customMeter.createHistogram('myHistogram');
histogram.bind({}).record(1);
}, /aggregatorFor method not implemented/);
});
});

class CustomProcessor extends Processor {
process(record: MetricRecord): void {
throw new Error('process method not implemented.');
}

aggregatorFor(metricKind: MetricDescriptor): Aggregator {
throw new Error('aggregatorFor method not implemented.');
}
}

function ensureMetric(metric: MetricRecord, name?: string, value?: LastValue) {
assert.ok(metric.aggregator instanceof LastValueAggregator);
const lastValue = metric.aggregator.toPoint().value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

import * as assert from 'assert';
import * as sinon from 'sinon';
import { MeterProvider, Meter, CounterMetric } from '../src';
import {
MeterProvider,
Meter,
CounterMetric,
MetricRecord,
MetricDescriptor,
Aggregator,
Processor,
} from '../src';

describe('MeterProvider', () => {
afterEach(() => {
Expand Down Expand Up @@ -74,6 +82,27 @@ describe('MeterProvider', () => {
const meter4 = provider.getMeter('meter3', 'ver2');
assert.notEqual(meter3, meter4);
});

it('should allow custom processor', () => {
class CustomProcessor extends Processor {
process(record: MetricRecord): void {
throw new Error('process method not implemented.');
}

aggregatorFor(metricKind: MetricDescriptor): Aggregator {
throw new Error('aggregatorFor method not implemented.');
}
}

const meter = new MeterProvider({
processor: new CustomProcessor(),
}).getMeter('custom-processor', '*');

assert.throws(() => {
const histogram = meter.createHistogram('myHistogram');
histogram.bind({}).record(1);
}, /aggregatorFor method not implemented/);
});
});

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

0 comments on commit e068039

Please sign in to comment.