-
Notifications
You must be signed in to change notification settings - Fork 983
[prototype] feat(api): add wrapMeter() for experimental metrics API features #4622
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
Changes from 5 commits
66dcf58
49dab6e
95e22f4
b630553
017a5d5
abb2e36
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,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']; | ||
|
Comment on lines
+81
to
+102
Member
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. 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?
Member
Author
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. 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 |
||
|
|
||
| /** | ||
| * 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); | ||
| } | ||
| 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(); |
Uh oh!
There was an error while loading. Please reload this page.