Skip to content

Commit

Permalink
feat(api-metrics): add schemaUrl to meter creations
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Oct 11, 2021
1 parent ed0ba06 commit cb9de22
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 26 deletions.
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, _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, options?: MeterOptions): Meter {
return this.getMeterProvider().getMeter(name, options);
}

/** Remove the global meter provider */
Expand Down
14 changes: 14 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,20 @@ import {
} from './Metric';
import { ObserverResult } from './ObserverResult';

/**
* An interface describes additional metadata of a meter.
*/
export interface MeterOptions {
/**
* The version of the meter or instrumentation library
*/
version?: string;
/**
* 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,19 @@
* 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, options?: MeterOptions): Meter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const meterProvider = new metrics.MeterProvider({
}),
});

const meter = meterProvider.getMeter('default', '0.0.1');
const meter = meterProvider.getMeter('default', { version: '0.0.1' });

const traceIdArr = [
31,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const meterProvider = new metrics.MeterProvider({
}),
});

const meter = meterProvider.getMeter('default', '0.0.1');
const meter = meterProvider.getMeter('default', { version: '0.0.1' });

if (typeof Buffer === 'undefined') {
(window as any).Buffer = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const meterProvider = new metrics.MeterProvider({
}),
});

const meter = meterProvider.getMeter('default', '0.0.1');
const meter = meterProvider.getMeter('default', { version: '0.0.1' });

export function mockCounter(): metrics.Metric<metrics.BoundCounter> & Counter {
const name = 'int-counter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ describe('PrometheusExporter', () => {
meterProvider = new MeterProvider({
interval: Math.pow(2, 31) - 1,
});
meter = meterProvider.getMeter('test-prometheus', '1', {
meter = meterProvider.getMeter('test-prometheus', {
version: '1',
exporter,
});
done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export abstract class InstrumentationAbstract<T = any>

this._tracer = trace.getTracer(instrumentationName, instrumentationVersion);

this._meter = metrics.getMeter(instrumentationName, instrumentationVersion);
this._meter = metrics.getMeter(instrumentationName, {
version: instrumentationVersion,
});
}

/* Api to wrap instrumented method */
Expand All @@ -77,7 +79,9 @@ export abstract class InstrumentationAbstract<T = any>
public setMeterProvider(meterProvider: MeterProvider) {
this._meter = meterProvider.getMeter(
this.instrumentationName,
this.instrumentationVersion
{
version: this.instrumentationVersion,
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,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, config?: MeterConfig): Meter {
const key = `${name}@${config?.version ?? ''}:${config?.schemaUrl ?? ''}`;
if (!this._meters.has(key)) {
this._meters.set(
key,
new Meter({ name, version }, config || this._config)
new Meter({
name,
version: config?.version,
// @ts-expect-error ts(2345) TODO: define the types in @opentelemetry/core
schemaUrl: config?.schemaUrl
}, config || 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 @@ -1366,7 +1366,8 @@ describe('Meter', () => {
});

it('should allow custom processor', () => {
const customMeter = new MeterProvider().getMeter('custom-processor', '*', {
const customMeter = new MeterProvider().getMeter('custom-processor', {
version: '*',
processor: new CustomProcessor(),
});
assert.throws(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ describe('MeterProvider', () => {

it('should return the same Meter instance with same name & version', () => {
const provider = new MeterProvider();
const meter1 = provider.getMeter('meter1', 'ver1');
const meter2 = provider.getMeter('meter1', 'ver1');
const meter1 = provider.getMeter('meter1', { version: 'ver1' });
const meter2 = provider.getMeter('meter1', { version: 'ver1' });
assert.deepEqual(meter1, meter2);
});

it('should return different Meter instance with different name or version', () => {
const provider = new MeterProvider();

const meter1 = provider.getMeter('meter1', 'ver1');
const meter1 = provider.getMeter('meter1', { version: 'ver1' });
const meter2 = provider.getMeter('meter1');
assert.notEqual(meter1, meter2);

const meter3 = provider.getMeter('meter2', 'ver2');
const meter4 = provider.getMeter('meter3', 'ver2');
const meter3 = provider.getMeter('meter2', { version: 'ver2' });
const meter4 = provider.getMeter('meter3', { version: 'ver2' });
assert.notEqual(meter3, meter4);
});
});
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 cb9de22

Please sign in to comment.