-
Notifications
You must be signed in to change notification settings - Fork 737
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
[prototype] feat(api): add wrapMeter() for experimental metrics API features #4622
Closed
pichlermarc
wants to merge
6
commits into
open-telemetry:main
from
dynatrace-oss-contrib:feat/api-synchronous-gauge
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66dcf58
feat(instrumentation): added synchronous gauge
clintonb 49dab6e
fixup! feat(instrumentation): added synchronous gauge
clintonb 95e22f4
fixup! feat(instrumentation): added synchronous gauge
clintonb b630553
feat(api): add wrapMeter() for experimental metrics API features
pichlermarc 017a5d5
Merge branch 'main' into feat/api-synchronous-gauge
pichlermarc abb2e36
Update api/package.json
pichlermarc File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,129 @@ | ||
/* | ||
* 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 } from '../../metrics/Meter'; | ||
import { MetricAttributes, MetricOptions } from '../../metrics/Metric'; | ||
import { Context } from '../../context/types'; | ||
import { NOOP_GAUGE_METRIC } from './Gauge'; | ||
|
||
/** | ||
* @experimental | ||
* | ||
* Records only the last value that is added to it, discards any others. | ||
*/ | ||
export interface Gauge< | ||
AttributesTypes extends MetricAttributes = MetricAttributes, | ||
> { | ||
/** | ||
* @experimental | ||
* Records a measurement. | ||
*/ | ||
record(value: number, attributes?: AttributesTypes, context?: Context): void; | ||
} | ||
|
||
/** | ||
* Steps for adding new experimental Meter API features: | ||
* - implement the specification in the `@opentelemetry/sdk-metrics` package, do NOT use any new types | ||
* - SDKs may peer-depend on old API versions where these types are not yet available | ||
* - we MUST duplicate any new types across API and SDK unless we drop support for older API versions | ||
* - dropping support for old API versions MUST be done in SDK Major versions | ||
* - failure to do this will result in failing builds for users that update the SDK but not the API | ||
* - add the new functionality to the {@link IExperimentalMeter} interface | ||
* - implement the functionality in the {@link ExperimentalMeter} | ||
* - if the underlying {@link Meter} used as an {@link IExperimentalMeter} throws an error, return a No-Op implementation. | ||
* | ||
* Users may now use {@link wrapMeter} to get access to experimental SDK functionality via the API | ||
* | ||
* To stabilize a feature: | ||
* - move the function interface from `IExperimentalMeter` to `Meter` | ||
* - replace the implemented method with a property of the same type as the one from `Meter`, move any auxiliary types to stable | ||
* - In ExperimentalMeter | ||
* - remove the implementation that falls back to No-Op on errors. | ||
* - assign the function that was moved to `Meter` to the property defined in the last step | ||
* - Implement the no-op case in {@link NoopMeter} | ||
* - ensure any `@experimental` annotations are removed | ||
*/ | ||
|
||
/** | ||
* @experimental | ||
* | ||
* Meter that offers experimental functionality IF that functionality is implemented by the SDK. | ||
* MAY return a no-op otherwise. Stable features continue to work as expected. | ||
*/ | ||
export interface IExperimentalMeter extends Meter { | ||
/** | ||
* @experimental Will be added to {@link Meter} in a future version when the specification is marked stable | ||
* | ||
* Creates and returns a new `Gauge`. | ||
* @param name the name of the metric. | ||
* @param [options] the metric options. | ||
*/ | ||
createGauge<AttributesTypes extends MetricAttributes = MetricAttributes>( | ||
name: string, | ||
options?: MetricOptions | ||
): Gauge<AttributesTypes>; | ||
} | ||
|
||
class ExperimentalMeter implements IExperimentalMeter { | ||
private _meter: Meter; | ||
constructor(meter: Meter) { | ||
this._meter = meter; | ||
this.addBatchObservableCallback = | ||
meter.addBatchObservableCallback.bind(meter); | ||
this.createCounter = meter.createCounter.bind(meter); | ||
this.createObservableGauge = meter.createObservableGauge.bind(meter); | ||
this.createHistogram = meter.createHistogram.bind(meter); | ||
this.createObservableCounter = meter.createObservableCounter.bind(meter); | ||
this.createObservableUpDownCounter = | ||
meter.createObservableUpDownCounter.bind(meter); | ||
this.createUpDownCounter = meter.createUpDownCounter.bind(meter); | ||
this.removeBatchObservableCallback = | ||
meter.removeBatchObservableCallback.bind(meter); | ||
} | ||
addBatchObservableCallback: Meter['addBatchObservableCallback']; | ||
createCounter: Meter['createCounter']; | ||
createObservableGauge: Meter['createObservableGauge']; | ||
createHistogram: Meter['createHistogram']; | ||
createObservableCounter: Meter['createObservableCounter']; | ||
createUpDownCounter: Meter['createUpDownCounter']; | ||
createObservableUpDownCounter: Meter['createObservableUpDownCounter']; | ||
removeBatchObservableCallback: Meter['removeBatchObservableCallback']; | ||
|
||
/** | ||
* Creates and returns a new `Gauge`. | ||
* @param name the name of the metric. | ||
* @param [options] the metric options. | ||
*/ | ||
createGauge(name: string, options?: MetricOptions): Gauge { | ||
try { | ||
return (this._meter as ExperimentalMeter).createGauge(name, options); | ||
} catch (e) { | ||
return NOOP_GAUGE_METRIC; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @experimental | ||
* | ||
* Wraps {@link Meter} so that it offers experimental functionality IF that functionality is implemented by the | ||
* registered SDK. MAY return a no-op instrument if the functionality is not implemented by the SDK. | ||
* Stable features continue to work as expected. | ||
* | ||
* @param meter | ||
*/ | ||
export function wrapMeter(meter: Meter): IExperimentalMeter { | ||
return new ExperimentalMeter(meter); | ||
} |
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,31 @@ | ||
/* | ||
* 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 { Gauge } from './ExperimentalMeter'; | ||
import { MetricAttributes } from '../../metrics/Metric'; | ||
import { NoopMetric } from '../../metrics/NoopMeter'; | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export class NoopGaugeMetric extends NoopMetric implements Gauge { | ||
record(_value: number, _attributes: MetricAttributes): void {} | ||
} | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export const NOOP_GAUGE_METRIC = new NoopGaugeMetric(); |
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
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.
This seems like a lot of duplication and large strings. I doubt this is very friendly to minification. Isn't there a way to keep the meter mostly the same and put new methods on it without a whole new class?
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.
The strings actually don't end up in the final bundle as they're only used for the types. The rest of the code, however is not the most efficient in terms of bundle-size, you're right.
Since the API we were trying to add here was marked as stable, I'll close this PR and re-focus my efforts on #4669