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

docs(sdk-metrics): update document on public interfaces #3075

Merged
merged 6 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions experimental/examples/prometheus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {

const attributes = { pid: process.pid, environment: 'staging' };

let counter = 0;
const observableCounter = meter.createObservableCounter('observable_requests', {
description: 'Example of an ObservableCounter',
});
observableCounter.addCallback(observableResult => {
observableResult.observe(counter, attributes);
});

// Record metrics
setInterval(() => {
counter++;
requestCounter.add(1, attributes);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
}, 1000);
28 changes: 10 additions & 18 deletions experimental/packages/opentelemetry-sdk-metrics-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@
[![NPM Published Version][npm-img]][npm-url]
[![Apache License][license-image]][license-image]

OpenTelemetry metrics allow a user to collect data and export it to a metrics backend like [Prometheus](https://prometheus.io/).
OpenTelemetry metrics module contains the foundation for all metrics SDKs of [opentelemetry-js](https://github.com/open-telemetry/opentelemetry-js).

## Work In Progress
Used standalone, this module provides methods for manual instrumentation of code, offering full control over recording metrics for client-side JavaScript (browser) and Node.js.

The OpenTelemetry SDK in this directory is undergoing drastic changes. If you need to use metrics, we recommend you use [version `0.27.0`](https://github.com/open-telemetry/opentelemetry-js/tree/experimental/v0.27.0/experimental/packages/opentelemetry-sdk-metrics-base).
It does **not** provide automated instrumentation of known libraries, host environment metrics out-of-the-box.
legendecas marked this conversation as resolved.
Show resolved Hide resolved

## Installation

```bash
npm install --save "@opentelemetry/sdk-metrics-base@~0.27.0"
npm install --save @opentelemetry/api-metrics
npm install --save @opentelemetry/sdk-metrics-base
```

## Usage

Please see the [version `0.27.0` README](https://github.com/open-telemetry/opentelemetry-js/tree/experimental/v0.27.0/experimental/packages/opentelemetry-sdk-metrics-base#usage).

TODO: Add usage information for updated SDK

## Installation of the Latest experimental version

```bash
npm install --save @opentelemetry/sdk-metrics-base
```

## Usage of the Latest experimental version
## Usage

The basic setup of the SDK can be seen as followings:

Expand Down Expand Up @@ -66,12 +57,13 @@ opentelemetry.getMeter('default')
async function batchObservableCallback(batchObservableResult) {
// ... do async stuff
batchObservableResult.observe(observableCounter, 1, { attributeKey: 'attribute-value' });

// This is been dropped since the observable is not associated with the callback at registration.
batchObservableResult.observe(otherObservable, 2);
}
```

## Example

See [examples/prometheus](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/examples/prometheus) for an end-to-end example, including exporting metrics.

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export enum InstrumentType {
OBSERVABLE_UP_DOWN_COUNTER = 'OBSERVABLE_UP_DOWN_COUNTER',
}

/**
* An interface describing the instrument.
*/
export interface InstrumentDescriptor {
readonly name: string;
readonly description: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export type ViewOptions = {
};

export type SelectorOptions = {
/**
* Selects instrument by their fields.
*/
instrument?: {
/**
* The type of the Instrument(s).
Expand All @@ -70,6 +73,9 @@ export type SelectorOptions = {
*/
name?: string,
}
/**
* Selects instrument by fields of their meter.
*/
meter?: {
/**
* The name of the Meter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,40 @@ export interface ResourceMetrics {
scopeMetrics: ScopeMetrics[];
}

/**
* Represents the collection result of the metrics. If there are any
* non-critical errors in the collection, like throwing in a single observable
* callback, these errors are aggregated in the {@link CollectionResult.errors}
* array and other successfully collected metrics are returned.
*/
export interface CollectionResult {
/**
* Collected metrics.
*/
resourceMetrics: ResourceMetrics;
/**
* Arbitrary JavaScript exception values.
dyladan marked this conversation as resolved.
Show resolved Hide resolved
*/
errors: unknown[];
}

/**
* The aggregated point data type.
*/
export enum DataPointType {
/**
* Singular data point. It can be the result of Sum and LastValue
* aggregation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't document here which aggregations return which values in case a new aggregation is added later. I would document on the aggregator which DataPointType it returns.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Singular data point. It can be the result of Sum and LastValue
* aggregation.
* A singular metric data point has only a single numeric value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Updated :)

*/
SINGULAR,
/**
* Histogram data point. It can be the result of Histogram aggregation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Histogram data point. It can be the result of Histogram aggregation.
* A histogram data point contains a histogram summary of collected values with a list of explicit bucket boundaries and summary statistics such as min, max, count, and sum of all collected values.

*/
HISTOGRAM,
/**
* Exponential histogram data point. It can be the result of Exponential
* histogram aggregation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Exponential histogram data point. It can be the result of Exponential
* histogram aggregation.
* An exponential histogram data point contains a histogram summary of collected values where bucket boundaries are automatically calculated using an exponential function, and summary statistics such as min, max, count, and sum of all collected values.

*/
EXPONENTIAL_HISTOGRAM,
}

Expand All @@ -101,7 +124,8 @@ export interface DataPoint<T> {
*/
readonly attributes: MetricAttributes;
/**
* The value for this DataPoint.
* The value for this DataPoint. The type of the value is indicated by the
* {@link DataPointType}.
*/
readonly value: T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,33 @@ import {
} from '@opentelemetry/core';
import { InstrumentType } from '../InstrumentDescriptor';


// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#metricexporter

/**
* An interface that allows different metric services to export recorded data
* in their own format.
*
* To export data this MUST be register to the Metrics SDK with a MetricReader.
legendecas marked this conversation as resolved.
Show resolved Hide resolved
*/
export interface PushMetricExporter {

/**
* Called to export sampled {@link ResourceMetrics}s.
legendecas marked this conversation as resolved.
Show resolved Hide resolved
* @param metrics the metric data to be exported.
*/
export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void;

/**
* Ensure that the export of any metrics the exporter has received is
* completed before the returned promise is settled.
*/
forceFlush(): Promise<void>;

/**
* Select the {@link AggregationTemporality} for the given
* {@link InstrumentType} for this exporter.
*/
selectAggregationTemporality(instrumentType: InstrumentType): AggregationTemporality;

/** Stops the exporter. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns a promise which resolves when the last started export is completed. Further calls to export will not cause data to be exported.

shutdown(): Promise<void>;

}

export class ConsoleMetricExporter implements PushMetricExporter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@
import { CollectionResult } from './MetricData';

export interface MetricCollectOptions {
/**
* Timeout for the SDK to perform the involved asynchronous callback
* functions.
*
* If the callback functions are failed to finish the observation in time,
legendecas marked this conversation as resolved.
Show resolved Hide resolved
* their results are discarded and an error is appended in the
* {@link CollectionResult.errors}.
*/
timeoutMillis?: number;
}

/**
* This is a public interface that represent an export state of a MetricReader.
*/
export interface MetricProducer {
/**
* Collects the metrics from the SDK. If there are asynchronous Instruments
* involved, their callback functions will be triggered.
*/
collect(options?: MetricCollectOptions): Promise<CollectionResult>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { callWithTimeout } from '../utils';
import { InstrumentType } from '../InstrumentDescriptor';
import { CollectionOptions, ForceFlushOptions, ShutdownOptions } from '../types';

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#metricreader

/**
* A registered reader of metrics that, when linked to a {@link MetricProducer}, offers global
* control over metrics.
Expand All @@ -49,7 +47,8 @@ export abstract class MetricReader {
}

/**
* Get the default {@link AggregationTemporality} for the given {@link InstrumentType}
* Select the {@link AggregationTemporality} for the given
* {@link InstrumentType} for this reader.
*/
abstract selectAggregationTemporality(instrumentType: InstrumentType): AggregationTemporality;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class PeriodicExportingMetricReader extends MetricReader {
await this._exporter.shutdown();
}

/**
* @inheritdoc
*/
selectAggregationTemporality(instrumentType: InstrumentType): AggregationTemporality {
return this._exporter.selectAggregationTemporality(instrumentType);
}
Expand Down