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-base): update metric exporter interfaces #2707

Merged
merged 15 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,38 @@

import { AggregationTemporality } from './AggregationTemporality';
import { MetricData } from './MetricData';
import {
ExportResult,
ExportResultCode,
} from '@opentelemetry/core';


// https:/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 {
export abstract class PushMetricExporter {
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
protected _shutdown = false;

abstract export(batch: MetricData[]): Promise<void>;
abstract export(batch: MetricData[]): Promise<ExportResult>;
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved

abstract forceFlush(): Promise<void>;

abstract getPreferredAggregationTemporality(): AggregationTemporality;

async shutdown(): Promise<void> {
if (this._shutdown) {
return;
}

// Setting _shutdown before flushing might prevent some exporters from flushing
// Waiting until flushing is complete might allow another flush to occur during shutdown
const flushPromise = this.forceFlush();
this._shutdown = true;
await flushPromise;
}
abstract shutdown(): Promise<void>;

isShutdown() {
return this._shutdown;
}
}

export class ConsoleMetricExporter extends MetricExporter {
export class ConsoleMetricExporter extends PushMetricExporter {
async export(_batch: MetricData[]) {
throw new Error('Method not implemented');
return new Promise<ExportResult>((_, reject) => {
reject({
code: ExportResultCode.FAILED,
error: new Error('Method not implemented')
});
});
}

getPreferredAggregationTemporality() {
Expand All @@ -58,4 +56,8 @@ export class ConsoleMetricExporter extends MetricExporter {

// nothing to do
async forceFlush() {}

async shutdown() {
this._shutdown = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

import * as api from '@opentelemetry/api';
import { MetricReader } from './MetricReader';
import { MetricExporter } from './MetricExporter';
import { PushMetricExporter } from './MetricExporter';
import { callWithTimeout, TimeoutError } from '../utils';

export type PeriodicExportingMetricReaderOptions = {
exporter: MetricExporter
exporter: PushMetricExporter
exportIntervalMillis?: number,
exportTimeoutMillis?: number
};
Expand All @@ -32,7 +32,7 @@ export type PeriodicExportingMetricReaderOptions = {
export class PeriodicExportingMetricReader extends MetricReader {
private _interval?: ReturnType<typeof setInterval>;

private _exporter: MetricExporter;
private _exporter: PushMetricExporter;

private readonly _exportInterval: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@

import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader';
import { AggregationTemporality } from '../../src/export/AggregationTemporality';
import { MetricExporter } from '../../src';
import { PushMetricExporter } from '../../src';
import { MetricData } from '../../src/export/MetricData';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { MetricProducer } from '../../src/export/MetricProducer';
import { TimeoutError } from '../../src/utils';
import { ExportResult, ExportResultCode } from '@opentelemetry/core';
import { assertRejects } from '../test-utils';

const MAX_32_BIT_INT = 2 ** 31 - 1;

class TestMetricExporter extends MetricExporter {
class TestMetricExporter extends PushMetricExporter {
public exportTime = 0;
public forceFlushTime = 0;
public throwException = false;
private _batches: MetricData[][] = [];

async export(batch: MetricData[]): Promise<void> {
async export(batch: MetricData[]): Promise<ExportResult> {
this._batches.push(batch);

if (this.throwException) {
throw new Error('Error during export');
}
await new Promise(resolve => setTimeout(resolve, this.exportTime));
return await new Promise(resolve => setTimeout(() => {
resolve({code: ExportResultCode.SUCCESS});
}, this.exportTime));
}

async shutdown(): Promise<void> {
if (this.isShutdown()) return;
const flushPromise = this.forceFlush();
this._shutdown = true;
await flushPromise;
}

async forceFlush(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ import * as sinon from 'sinon';
import { MeterProvider } from '../../src';
import { AggregationTemporality } from '../../src/export/AggregationTemporality';
import { MetricData, PointDataType } from '../../src/export/MetricData';
import { MetricExporter } from '../../src/export/MetricExporter';
import { PushMetricExporter } from '../../src/export/MetricExporter';
import { MeterProviderSharedState } from '../../src/state/MeterProviderSharedState';
import { MetricCollector } from '../../src/state/MetricCollector';
import { defaultInstrumentationLibrary, defaultResource, assertMetricData, assertPointData } from '../util';
import { TestMetricReader } from '../export/TestMetricReader';
import { ExportResult, ExportResultCode } from '@opentelemetry/core';

class TestMetricExporter extends MetricExporter {
class TestMetricExporter extends PushMetricExporter {
metricDataList: MetricData[] = [];
async export(batch: MetricData[]): Promise<void> {
async export(batch: MetricData[]): Promise<ExportResult> {
this.metricDataList.push(...batch);
return new Promise<ExportResult>((resolve, _) => {
resolve({code: ExportResultCode.SUCCESS});
});
}

async shutdown(): Promise<void> {}

async forceFlush(): Promise<void> {}

getPreferredAggregationTemporality(): AggregationTemporality {
Expand Down Expand Up @@ -63,7 +69,8 @@ describe('MetricCollector', () => {
});

describe('collect', () => {
function setupInstruments(exporter: MetricExporter) {

function setupInstruments(exporter: PushMetricExporter) {
const meterProvider = new MeterProvider({ resource: defaultResource });

const reader = new TestMetricReader(exporter.getPreferredAggregationTemporality());
Expand Down