-
Notifications
You must be signed in to change notification settings - Fork 954
feat(api): Add delegating no-op meter provider #4858
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
4e0fc68
9bcfe12
2c8b476
fa9d8dc
7fafc7a
55fe566
21f75c1
9fbd52d
35b02b4
41c1af4
16ddbbb
25a2082
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
hectorhdzg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) {} | ||
|
|
||
| /** | ||
| * @see {@link Meter.createGauge} | ||
| */ | ||
| createGauge(_name: string, _options?: MetricOptions): Gauge { | ||
| return this._getMeter().createGauge(_name, _options); | ||
| } | ||
|
||
|
|
||
| /** | ||
| * @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; | ||
| } | ||
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, would it be required to export this? 🤔 It seems to me that it'd perfectly fine to keep this internal in case we want to change something about it in the future.