Skip to content

Commit

Permalink
feat(api-metrics): add schemaUrl to meter creations (#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Nov 1, 2021
1 parent 357ec92 commit 61cf8c5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 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, _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 @@ -49,11 +49,16 @@ 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 || ''}`;
const key = `${name}@${version ?? ''}:${config?.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: config?.schemaUrl
}, Object.assign({}, this._config, 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
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 61cf8c5

Please sign in to comment.