-
Notifications
You must be signed in to change notification settings - Fork 838
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
base: main
Are you sure you want to change the base?
Changes from all commits
4e0fc68
9bcfe12
2c8b476
fa9d8dc
7fafc7a
55fe566
21f75c1
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 | ||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all of these could be private, right? 🤔 |
||
) {} | ||
|
||
/** | ||
* @see {@link Meter.createGauge} | ||
*/ | ||
createGauge(_name: string, _options?: MetricOptions): Gauge { | ||
return this._getMeter().createGauge(_name, _options); | ||
} | ||
Comment on lines
+51
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the major bump is necessary for this (just as with logs and traces). The types we export from the API are what we guarantee will be there. If there's extra properties on the registered SDK and those are available on the API too in plain JavaScript, then that was never guaranteed to also be there when returned by the API. We can merge this to a separate branch to experiment with delegating instruments, but I think that could also be done here on this branch, as the PR won't really address the whole problem (instruments created before SDK registration being no-op) as it stands right now. |
||
|
||
/** | ||
* @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.