-
Notifications
You must be signed in to change notification settings - Fork 804
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
hectorhdzg
wants to merge
7
commits into
open-telemetry:main
Choose a base branch
from
hectorhdzg:hectorhdzg/delmetprov
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+490
−13
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e0fc68
Delegating no-op provider
hectorhdzg 9bcfe12
Merge branch 'main' into hectorhdzg/delmetprov
hectorhdzg 2c8b476
add create gauge methods
hectorhdzg fa9d8dc
Remove otlp protos
hectorhdzg 7fafc7a
Add changelog
hectorhdzg 55fe566
Update API changelog
hectorhdzg 21f75c1
Merge branch 'main' into hectorhdzg/delmetprov
hectorhdzg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
/** | ||
* @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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.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.
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 comment
The 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.