Skip to content
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(sdk-metrics): added synchronous gauge to SDK #4565

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@ and AnyValueMap types [#4575](https://github.com/open-telemetry/opentelemetry-js

### :rocket: (Enhancement)

* feat(metrics): added synchronous gauge to SDK [#4565](https://github.com/open-telemetry/opentelemetry-js/pull/4565) @clintonb
* feat(opentelemetry-instrumentation-xhr): optionally ignore network events [#4571](https://github.com/open-telemetry/opentelemetry-js/pull/4571/) @mustafahaddara
* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
* perf(instrumentation-http): remove obvious temp allocations [#4576](https://github.com/open-telemetry/opentelemetry-js/pull/4576) @Samuron
Expand Down
Expand Up @@ -41,6 +41,7 @@
switch (instrumentType) {
case InstrumentType.COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
case InstrumentType.GAUGE:
case InstrumentType.HISTOGRAM:
case InstrumentType.OBSERVABLE_GAUGE:
return AggregationTemporality.DELTA;
Expand All @@ -57,6 +58,7 @@
case InstrumentType.COUNTER:
case InstrumentType.HISTOGRAM:
return AggregationTemporality.DELTA;
case InstrumentType.GAUGE:

Check warning on line 61 in experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts

View check run for this annotation

Codecov / codecov/patch

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts#L61

Added line #L61 was not covered by tests
case InstrumentType.UP_DOWN_COUNTER:
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/InstrumentDescriptor.ts
Expand Up @@ -23,6 +23,7 @@ import { equalsCaseInsensitive } from './utils';
*/
export enum InstrumentType {
COUNTER = 'COUNTER',
GAUGE = 'GAUGE',
HISTOGRAM = 'HISTOGRAM',
UP_DOWN_COUNTER = 'UP_DOWN_COUNTER',
OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER',
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk-metrics/src/Instruments.ts
Expand Up @@ -36,6 +36,7 @@ import {
AsyncWritableMetricStorage,
WritableMetricStorage,
} from './state/WritableMetricStorage';
import { Gauge } from './types';

export class SyncInstrument {
constructor(
Expand Down Expand Up @@ -110,6 +111,18 @@ export class CounterInstrument extends SyncInstrument implements Counter {
}
}

/**
* The class implements {@link Gauge} interface.
*/
export class GaugeInstrument extends SyncInstrument implements Gauge {
/**
* Records a measurement.
*/
record(value: number, attributes?: MetricAttributes, ctx?: Context): void {
this._record(value, attributes, ctx);
}
}

/**
* The class implements {@link Histogram} interface.
*/
Expand Down
20 changes: 20 additions & 0 deletions packages/sdk-metrics/src/Meter.ts
Expand Up @@ -25,27 +25,47 @@ import {
ObservableUpDownCounter,
BatchObservableCallback,
Observable,
Attributes,
} from '@opentelemetry/api';
import {
createInstrumentDescriptor,
InstrumentType,
} from './InstrumentDescriptor';
import {
CounterInstrument,
GaugeInstrument,
HistogramInstrument,
ObservableCounterInstrument,
ObservableGaugeInstrument,
ObservableUpDownCounterInstrument,
UpDownCounterInstrument,
} from './Instruments';
import { MeterSharedState } from './state/MeterSharedState';
import { Gauge } from './types';

/**
* This class implements the {@link IMeter} interface.
*/
export class Meter implements IMeter {
constructor(private _meterSharedState: MeterSharedState) {}

/**
* Create a {@link Gauge} instrument.
clintonb marked this conversation as resolved.
Show resolved Hide resolved
* @experimental
*/
createGauge<AttributesTypes extends Attributes = Attributes>(
name: string,
options?: MetricOptions
): Gauge<AttributesTypes> {
const descriptor = createInstrumentDescriptor(
name,
InstrumentType.GAUGE,
options
);
const storage = this._meterSharedState.registerMetricStorage(descriptor);
return new GaugeInstrument(storage, descriptor);
}

/**
* Create a {@link Histogram} instrument.
*/
Expand Down
Expand Up @@ -580,6 +580,7 @@ export class ExponentialHistogramAggregator

// determine if instrument allows negative values.
const allowsNegativeValues =
descriptor.type === InstrumentType.GAUGE ||
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/aggregator/Histogram.ts
Expand Up @@ -231,6 +231,7 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {

// determine if instrument allows negative values.
const allowsNegativeValues =
descriptor.type === InstrumentType.GAUGE ||
descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
descriptor.type === InstrumentType.OBSERVABLE_GAUGE ||
descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER;
Expand Down
16 changes: 16 additions & 0 deletions packages/sdk-metrics/src/types.ts
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Context, MetricAttributes } from '@opentelemetry/api';

export type CommonReaderOptions = {
timeoutMillis?: number;
Expand All @@ -23,3 +24,18 @@ export type CollectionOptions = CommonReaderOptions;
export type ShutdownOptions = CommonReaderOptions;

export type ForceFlushOptions = CommonReaderOptions;

/**
* @experimental
*
* This is intentionally not using the API's type as it's only available from @opentelemetry/api 1.9.0 and up.
* In SDK 2.0 we'll be able to bump the minimum API version and remove this workaround.
*/
export interface Gauge<
AttributesTypes extends MetricAttributes = MetricAttributes,
> {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: AttributesTypes, context?: Context): void;
}
1 change: 1 addition & 0 deletions packages/sdk-metrics/src/view/Aggregation.ts
Expand Up @@ -182,6 +182,7 @@ export class DefaultAggregation extends Aggregation {
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER: {
return SUM_AGGREGATION;
}
case InstrumentType.GAUGE:
case InstrumentType.OBSERVABLE_GAUGE: {
return LAST_VALUE_AGGREGATION;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk-metrics/test/Meter.test.ts
Expand Up @@ -19,6 +19,7 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import {
CounterInstrument,
GaugeInstrument,
HistogramInstrument,
ObservableCounterInstrument,
ObservableGaugeInstrument,
Expand Down Expand Up @@ -88,6 +89,18 @@ describe('Meter', () => {
});
});

describe('createGauge', () => {
testWithNames('Gauge', name => {
const meterSharedState = new MeterSharedState(
new MeterProviderSharedState(defaultResource),
defaultInstrumentationScope
);
const meter = new Meter(meterSharedState);
const Gauge = meter.createGauge(name);
assert(Gauge instanceof GaugeInstrument);
});
});

describe('createObservableCounter', () => {
testWithNames('ObservableCounter', name => {
const meterSharedState = new MeterSharedState(
Expand Down
Expand Up @@ -160,6 +160,7 @@ describe('ConsoleMetricExporter', () => {
switch (instrumentType) {
case InstrumentType.COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
case InstrumentType.GAUGE:
case InstrumentType.HISTOGRAM:
case InstrumentType.OBSERVABLE_GAUGE:
return AggregationTemporality.DELTA;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-metrics/test/export/utils.ts
Expand Up @@ -29,6 +29,7 @@ const instrumentTypes = [
InstrumentType.UP_DOWN_COUNTER,
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER,
InstrumentType.HISTOGRAM,
InstrumentType.GAUGE,
InstrumentType.OBSERVABLE_GAUGE,
];

Expand Down