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(api): Add delegating no-op meter provider #4858

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :boom: Breaking Change

* feat(api) Add delegating no-op meter provider [#4858](https://github.com/open-telemetry/opentelemetry-js/pull/4858) @hectorhdzg

### :rocket: (Enhancement)

### :bug: (Bug Fix)
Expand Down
17 changes: 14 additions & 3 deletions api/src/api/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import { Meter, MeterOptions } from '../metrics/Meter';
import { MeterProvider } from '../metrics/MeterProvider';
import { NOOP_METER_PROVIDER } from '../metrics/NoopMeterProvider';
import {
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { DiagAPI } from './diag';
import { ProxyMeterProvider } from '../metrics/ProxyMeterProvider';

const API_NAME = 'metrics';

Expand All @@ -32,6 +32,8 @@ const API_NAME = 'metrics';
export class MetricsAPI {
private static _instance?: MetricsAPI;

private _proxyMeterProvider = new ProxyMeterProvider();

/** Empty private constructor prevents end users from constructing a new instance of the API */
private constructor() {}

Expand All @@ -49,14 +51,22 @@ export class MetricsAPI {
* Returns true if the meter provider was successfully registered, else false.
*/
public setGlobalMeterProvider(provider: MeterProvider): boolean {
return registerGlobal(API_NAME, provider, DiagAPI.instance());
const success = registerGlobal(
API_NAME,
this._proxyMeterProvider,
DiagAPI.instance()
);
if (success) {
this._proxyMeterProvider.setDelegate(provider);
}
return success;
}

/**
* Returns the global meter provider.
*/
public getMeterProvider(): MeterProvider {
return getGlobal(API_NAME) || NOOP_METER_PROVIDER;
return getGlobal(API_NAME) || this._proxyMeterProvider;
}

/**
Expand All @@ -73,5 +83,6 @@ export class MetricsAPI {
/** Remove the global meter provider */
public disable(): void {
unregisterGlobal(API_NAME, DiagAPI.instance());
this._proxyMeterProvider = new ProxyMeterProvider();
}
}
2 changes: 2 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export {
export type { DiagAPI } from './api/diag';

// Metrics APIs
export { ProxyMeter, MeterDelegator } from './metrics/ProxyMeter';
export { ProxyMeterProvider } from './metrics/ProxyMeterProvider';
export { createNoopMeter } from './metrics/NoopMeter';
export { MeterOptions, Meter } from './metrics/Meter';
export { MeterProvider } from './metrics/MeterProvider';
Expand Down
156 changes: 156 additions & 0 deletions api/src/metrics/ProxyMeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Meter, MeterOptions } from './Meter';
import { NoopMeter } from './NoopMeter';
import {
BatchObservableCallback,
Counter,
Histogram,
MetricOptions,
ObservableCounter,
ObservableGauge,
ObservableUpDownCounter,
UpDownCounter,
Observable,
Gauge,
} from './Metric';

const NOOP_METER = new NoopMeter();

/**
* Proxy meter provided by the proxy meter provider
*/
export class ProxyMeter implements Meter {
// When a real implementation is provided, this will be it
private _delegate?: Meter;

constructor(
private _provider: MeterDelegator,
public readonly name: string,
public readonly version?: string,
public readonly options?: MeterOptions
) {}

/**
* @see {@link Meter.createGauge}
*/
createGauge(_name: string, _options?: MetricOptions): Gauge {
return this._getMeter().createGauge(_name, _options);
}
Comment on lines +51 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a certain difficulty in the case of the metrics API vs the trace/logs APIs - there's another layer that needs to be taken into account, which is the instruments.

In Traces, the (no-op) span is an object which is only kept for the time that the span is active. So one span being no-op is not too problematic. For logs, the record is passed straight to the logger so it's also not a problem there. In metrics however, an instrument is instantiated early and is kept over the lifetime of the app. That means that when a user gets a Meter, they usually also create the Instruments already. A later-updated ProxyMeter would therefore be of very limited help as the instruments would also need to be proxies in order to give a smooth experience.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look @pichlermarc, I wanted to fix #4112, so this is still relevant for us, I understand this change may need a major version update in the api package, maybe this is something I can move to a different branch and start experimenting with Instruments proxies or similar concept, what do you think?


/**
* @see {@link Meter.createUpDownCounter}
*/
createHistogram(_name: string, _options?: MetricOptions): Histogram {
return this._getMeter().createHistogram(_name, _options);
}

/**
* @see {@link Meter.createUpDownCounter}
*/
createCounter(_name: string, _options?: MetricOptions): Counter {
return this._getMeter().createCounter(_name, _options);
}

/**
* @see {@link Meter.createUpDownCounter}
*/
createUpDownCounter(_name: string, _options?: MetricOptions): UpDownCounter {
return this._getMeter().createUpDownCounter(_name, _options);
}

/**
* @see {@link Meter.createObservableGauge}
*/
createObservableGauge(
_name: string,
_options?: MetricOptions
): ObservableGauge {
return this._getMeter().createObservableGauge(_name, _options);
}

/**
* @see {@link Meter.createObservableCounter}
*/
createObservableCounter(
_name: string,
_options?: MetricOptions
): ObservableCounter {
return this._getMeter().createObservableCounter(_name, _options);
}

/**
* @see {@link Meter.createObservableUpDownCounter}
*/
createObservableUpDownCounter(
_name: string,
_options?: MetricOptions
): ObservableUpDownCounter {
return this._getMeter().createObservableUpDownCounter(_name, _options);
}

/**
* @see {@link Meter.addBatchObservableCallback}
*/
addBatchObservableCallback(
_callback: BatchObservableCallback,
_observables: Observable[]
): void {
this._getMeter().addBatchObservableCallback(_callback, _observables);
}

/**
* @see {@link Meter.removeBatchObservableCallback}
*/
removeBatchObservableCallback(
_callback: BatchObservableCallback,
_observables: Observable[]
): void {
this._getMeter().removeBatchObservableCallback(_callback, _observables);
}

/**
* Try to get a meter from the proxy meter provider.
* If the proxy meter provider has no delegate, return a noop meter.
*/
private _getMeter() {
if (this._delegate) {
return this._delegate;
}

const meter = this._provider.getDelegateMeter(
this.name,
this.version,
this.options
);

if (!meter) {
return NOOP_METER;
}

this._delegate = meter;
return this._delegate;
}
}

export interface MeterDelegator {
getDelegateMeter(
name: string,
version?: string,
options?: MeterOptions
): Meter | undefined;
}
63 changes: 63 additions & 0 deletions api/src/metrics/ProxyMeterProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { MeterProvider } from './MeterProvider';
import { ProxyMeter } from './ProxyMeter';
import { NoopMeterProvider } from './NoopMeterProvider';
import { Meter, MeterOptions } from './Meter';

const NOOP_METER_PROVIDER = new NoopMeterProvider();

/**
* Meter provider which provides {@link ProxyMeter}s.
*
* Before a delegate is set, meters provided are NoOp.
* When a delegate is set, meters are provided from the delegate.
* When a delegate is set after meters have already been provided,
* all meters already provided will use the provided delegate implementation.
*/
export class ProxyMeterProvider implements MeterProvider {
private _delegate?: MeterProvider;

/**
* Get a {@link ProxyMeter}
*/
getMeter(name: string, version?: string, options?: MeterOptions): Meter {
return (
this.getDelegateMeter(name, version, options) ??
new ProxyMeter(this, name, version, options)
);
}

getDelegate(): MeterProvider {
return this._delegate ?? NOOP_METER_PROVIDER;
}

/**
* Set the delegate meter provider
*/
setDelegate(delegate: MeterProvider) {
this._delegate = delegate;
}

getDelegateMeter(
name: string,
version?: string,
options?: MeterOptions
): Meter | undefined {
return this._delegate?.getMeter(name, version, options);
}
}
Loading