Skip to content

Commit

Permalink
Merge branch 'wip-metrics-sdk' of github.com:dyladan/opentelemetry-js…
Browse files Browse the repository at this point in the history
… into wip-metrics-sdk
  • Loading branch information
dyladan committed Nov 15, 2021
2 parents 753d0f9 + 892c92b commit f2d79aa
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as api from '@opentelemetry/api';
import * as metrics from '@opentelemetry/api-metrics';
import { Meter } from './Meter';

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

export enum InstrumentType {
COUNTER = 'COUNTER',
HISTOGRAM = 'HISTOGRAM',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import * as api from '@opentelemetry/api'
import { Attributes } from '@opentelemetry/api-metrics'

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

export type Measurement = {
value: number;
// TODO use common attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { Counter, Histogram, UpDownCounter } from './Instruments';
import { Measurement } from './Measurement';
import { MeterProvider } from './MeterProvider';

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

export class Meter implements metrics.Meter {
// instrumentation library required by spec to be on meter
// spec requires provider config changes to apply to previously created meters, achieved by holding a reference to the provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export class MeterProvider {
/**
* Flush all buffered data and shut down the MeterProvider and all exporters and metric readers.
* Returns a promise which is resolved when all flushes are complete.
*
* TODO: return errors to caller somehow?
*/
async shutdown(): Promise<void> {
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#shutdown
Expand All @@ -87,12 +89,24 @@ export class MeterProvider {
// TODO add a timeout - spec leaves it up the the SDK if this is configurable
this._shutdown = true;

await this._forceFlush();
// Shut down all exporters and readers.
// Log all Errors.
for (const exporter of this._metricExporters) {
try {
await exporter.shutdown();
} catch (e) {
if (e instanceof Error) {
api.diag.error(`Error shutting down: ${e.message}`)
}
}
}
}

/**
* Notifies all exporters and metric readers to flush any buffered data.
* Returns a promise which is resolved when all flushes are complete.
*
* TODO: return errors to caller somehow?
*/
async forceFlush(): Promise<void> {
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#forceflush
Expand All @@ -105,21 +119,12 @@ export class MeterProvider {
return;
}

await this._forceFlush();
}

/**
* A private implementation of force flush which doesn't check if the function is shut down
*/
private async _forceFlush() {
// Shut down all exporters and readers.
// Catch and log all errors
for (const exporter of [...this._metricExporters, ...this._metricReaders]) {
try {
await exporter.shutdown();
await exporter.forceFlush();
} catch (e) {
if (e instanceof Error) {
api.diag.error(`Error shutting down: ${e.message}`)
api.diag.error(`Error flushing: ${e.message}`)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

// TODO should this just be an interface and exporters can implement their own shutdown?
export abstract class MetricExporter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import { MetricExporter } from '.';

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

export class MetricReader {
private _shutdown = false;

Expand Down

0 comments on commit f2d79aa

Please sign in to comment.