Skip to content

Commit

Permalink
Merge branch 'main' into metrics-ff/bind
Browse files Browse the repository at this point in the history
# Conflicts:
#	experimental/packages/opentelemetry-sdk-metrics-base/test/Meter.test.ts
  • Loading branch information
legendecas committed Nov 2, 2021
2 parents 933b0cc + 4e311bd commit e012fe9
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This is the JavaScript version of [OpenTelemetry](https://opentelemetry.io/), a

| API Version | Core version | Experimental Packages | Contrib Version |
| ----------- |--------------| --------------------- |-------------------------|
| 1.0.x | 1.x | 0.26.x | ------ |
| 1.0.x | 1.x | 0.26.x | 0.26.x |
| 1.0.x | 0.26.x | ----- | ------ |
| 1.0.x | 0.25.x | ----- | ------ |
| 1.0.x | 0.24.x | ----- | 0.24.x |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Meter } from './types/Meter';
import { Meter, MeterOptions } from './types/Meter';
import { MeterProvider } from './types/MeterProvider';
import { NOOP_METER } from './NoopMeter';

Expand All @@ -23,7 +23,7 @@ import { NOOP_METER } from './NoopMeter';
* for all calls to `getMeter`
*/
export class NoopMeterProvider implements MeterProvider {
getMeter(_name?: string, _version?: string): Meter {
getMeter(_name: string, _version?: string, _options?: MeterOptions): Meter {
return NOOP_METER;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Meter } from '../types/Meter';
import { Meter, MeterOptions } from '../types/Meter';
import { MeterProvider } from '../types/MeterProvider';
import { NOOP_METER_PROVIDER } from '../NoopMeterProvider';
import {
Expand Down Expand Up @@ -73,8 +73,8 @@ export class MetricsAPI {
/**
* Returns a meter from the global meter provider.
*/
public getMeter(name: string, version?: string): Meter {
return this.getMeterProvider().getMeter(name, version);
public getMeter(name: string, version?: string, options?: MeterOptions): Meter {
return this.getMeterProvider().getMeter(name, version, options);
}

/** Remove the global meter provider */
Expand Down
10 changes: 10 additions & 0 deletions experimental/packages/opentelemetry-api-metrics/src/types/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ import {
} from './Metric';
import { ObservableResult } from './ObservableResult';

/**
* An interface describes additional metadata of a meter.
*/
export interface MeterOptions {
/**
* The schemaUrl of the meter or instrumentation library
*/
schemaUrl?: string;
}

/**
* An interface to allow the recording metrics.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
* limitations under the License.
*/

import { Meter } from './Meter';
import { Meter, MeterOptions } from './Meter';

/**
* A registry for creating named {@link Meter}s.
*/
export interface MeterProvider {
/**
* Returns a Meter, creating one if one with the given name and version is
* not already created.
* Returns a Meter, creating one if one with the given name, version, and
* schemaUrl pair is not already created.
*
* @param name The name of the meter or instrumentation library.
* @param version The version of the meter or instrumentation library.
* @param options The options of the meter or instrumentation library.
* @returns Meter A Meter with the given name and version
*/
getMeter(name: string, version?: string): Meter;
getMeter(name: string, version?: string, options?: MeterOptions): Meter;
}
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 @@ -362,25 +361,15 @@ describe('PrometheusExporter', () => {
counter.add(20, { counterKey1: 'labelValue2' });
counter.add(30, { counterKey1: 'labelValue3' });
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,12 +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 || ''}`;
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 }, config || this._config)
new Meter({
name,
version,
// @ts-expect-error ts(2345) TODO: upgrade @opentelemetry/core InstrumentationLibrary definition
schemaUrl: options?.schemaUrl
}, this._config)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Processor } from './export/Processor';
import { MetricExporter } from './export/types';

/** MeterConfig provides an interface for configuring a Meter. */
export interface MeterConfig {
export interface MeterConfig extends api.MeterOptions {
/** Metric exporter. */
exporter?: MetricExporter;

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') as HistogramMetric;
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.record(1);
}, /aggregatorFor method not implemented/);
});
});

describe('shutdown()', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ShimWrapped extends Function {
export interface InstrumentationLibrary {
readonly name: string;
readonly version?: string;
readonly schemaUrl?: string;
}

/** Defines an error handler function */
Expand Down

0 comments on commit e012fe9

Please sign in to comment.