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(console-metric-exporter): add temporality configuration #3387

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All notable changes to experimental packages in this project will be documented
* feat(detectors): add browser detector module [#3292](https://github.com/open-telemetry/opentelemetry-js/pull/3292) @abinet18
* deps: remove unused proto-loader dependencies and update grpc-js and proto-loader versions [#3337](https://github.com/open-telemetry/opentelemetry-js/pull/3337) @seemk
* feat(metrics-exporters): configure temporality via environment variable [#3305](https://github.com/open-telemetry/opentelemetry-js/pull/3305) @pichlermarc
* feat(console-metric-exporter): add temporality configuration [#3387](https://github.com/open-telemetry/opentelemetry-js/pull/3387) @pichlermarc

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ExportResult, ExportResultCode } from '@opentelemetry/core';
import {
ExportResult,
ExportResultCode
} from '@opentelemetry/core';
import { InstrumentType } from '../InstrumentDescriptor';
import { AggregationTemporality } from './AggregationTemporality';
import { ResourceMetrics } from './MetricData';
import { PushMetricExporter } from './MetricExporter';
import {
AggregationTemporalitySelector,
DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR,
} from './AggregationSelector';

interface ConsoleMetricExporterOptions {
temporalitySelector?: AggregationTemporalitySelector
}

/* eslint-disable no-console */
export class ConsoleMetricExporter implements PushMetricExporter {
protected _shutdown = false;
protected _temporalitySelector: AggregationTemporalitySelector;

constructor(options?: ConsoleMetricExporterOptions) {
this._temporalitySelector = options?.temporalitySelector ?? DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR;
}

export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void {
if (this._shutdown) {
Expand All @@ -38,7 +54,7 @@ export class ConsoleMetricExporter implements PushMetricExporter {
}

selectAggregationTemporality(_instrumentType: InstrumentType): AggregationTemporality {
return AggregationTemporality.CUMULATIVE;
return this._temporalitySelector(_instrumentType);
}

shutdown(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import { MeterProvider } from '../../src/MeterProvider';
import { defaultResource } from '../util';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { assertAggregationTemporalitySelector } from './utils';
import {
DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR
} from '../../src/export/AggregationSelector';
import {
AggregationTemporality,
InstrumentType
} from '../../src';


async function waitForNumberOfExports(exporter: sinon.SinonSpy<[metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void], void>, numberOfExports: number): Promise<void> {
Expand All @@ -38,71 +46,108 @@ async function waitForNumberOfExports(exporter: sinon.SinonSpy<[metrics: Resourc

/* eslint-disable no-console */
describe('ConsoleMetricExporter', () => {
let previousConsoleDir: any;
let exporter: ConsoleMetricExporter;
let meterProvider: MeterProvider;
let meterReader: PeriodicExportingMetricReader;
let meter: metrics.Meter;

beforeEach(() => {
previousConsoleDir = console.dir;
console.dir = () => {};

exporter = new ConsoleMetricExporter();
meterProvider = new MeterProvider({ resource: defaultResource });
meter = meterProvider.getMeter('ConsoleMetricExporter', '1.0.0');
meterReader = new PeriodicExportingMetricReader({
exporter: exporter,
exportIntervalMillis: 100,
exportTimeoutMillis: 100
describe('export', () => {
let previousConsoleDir: any;
let exporter: ConsoleMetricExporter;
let meterProvider: MeterProvider;
let meterReader: PeriodicExportingMetricReader;
let meter: metrics.Meter;

beforeEach(() => {
previousConsoleDir = console.dir;
console.dir = () => {};

exporter = new ConsoleMetricExporter();
meterProvider = new MeterProvider({ resource: defaultResource });
meter = meterProvider.getMeter('ConsoleMetricExporter', '1.0.0');
meterReader = new PeriodicExportingMetricReader({
exporter: exporter,
exportIntervalMillis: 100,
exportTimeoutMillis: 100
});
meterProvider.addMetricReader(meterReader);
});
meterProvider.addMetricReader(meterReader);
});

afterEach(async () => {
console.dir = previousConsoleDir;
afterEach(async () => {
console.dir = previousConsoleDir;

await meterReader.shutdown();
});

it('should export information about metric', async () => {
const counter = meter.createCounter('counter_total', {
description: 'a test description',
});
const counterAttribute = { key1: 'attributeValue1' };
counter.add(10, counterAttribute);
counter.add(10, counterAttribute);

const histogram = meter.createHistogram('histogram', { description: 'a histogram' });
histogram.record(10);
histogram.record(100);
histogram.record(1000);

const spyConsole = sinon.spy(console, 'dir');
const spyExport = sinon.spy(exporter, 'export');

await waitForNumberOfExports(spyExport, 1);
const resourceMetrics = spyExport.args[0];
const firstResourceMetric = resourceMetrics[0];
const consoleArgs = spyConsole.args[0];
const consoleMetric = consoleArgs[0];
const keys = Object.keys(consoleMetric).sort().join(',');

await meterReader.shutdown();
const expectedKeys = [
'dataPointType',
'dataPoints',
'descriptor',
].join(',');

assert.ok(firstResourceMetric.resource.attributes.resourceKey === 'my-resource', 'resourceKey');
assert.ok(keys === expectedKeys, 'expectedKeys');
assert.ok(consoleMetric.descriptor.name === 'counter_total', 'name');
assert.ok(consoleMetric.descriptor.description === 'a test description', 'description');
assert.ok(consoleMetric.descriptor.type === 'COUNTER', 'type');
assert.ok(consoleMetric.descriptor.unit === '', 'unit');
assert.ok(consoleMetric.descriptor.valueType === 1, 'valueType');
assert.ok(consoleMetric.dataPoints[0].attributes.key1 === 'attributeValue1', 'ensure metric attributes exists');

assert.ok(spyExport.calledOnce);
});
Copy link
Member Author

@pichlermarc pichlermarc Nov 7, 2022

Choose a reason for hiding this comment

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

This code here is not new and has only been moved into a new describe block, as the setup is not needed for the tests that I added.

});

it('should export information about metric', async () => {
const counter = meter.createCounter('counter_total', {
description: 'a test description',
describe('constructor', () => {
it('with no arguments should select cumulative temporality', () => {
const exporter = new ConsoleMetricExporter();
assertAggregationTemporalitySelector(exporter, DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR);
});

it('with empty options should select cumulative temporality', () => {
const exporter = new ConsoleMetricExporter({});
assertAggregationTemporalitySelector(exporter, DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR);
});

it('with cumulative preference should select cumulative temporality', () => {
const exporter = new ConsoleMetricExporter({ temporalitySelector: _ => AggregationTemporality.CUMULATIVE });
assertAggregationTemporalitySelector(exporter, _ => AggregationTemporality.CUMULATIVE);
});

it('with mixed preference should select matching temporality', () => {
// use delta-ish example as a representation of a commonly used "mixed" preference.
const selector = (instrumentType: InstrumentType) => {
switch (instrumentType) {
case InstrumentType.COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
case InstrumentType.HISTOGRAM:
case InstrumentType.OBSERVABLE_GAUGE:
return AggregationTemporality.DELTA;
case InstrumentType.UP_DOWN_COUNTER:
case InstrumentType.OBSERVABLE_UP_DOWN_COUNTER:
return AggregationTemporality.CUMULATIVE;
}
};
const exporter = new ConsoleMetricExporter({ temporalitySelector: selector });
assertAggregationTemporalitySelector(exporter, selector);
});
const counterAttribute = { key1: 'attributeValue1' };
counter.add(10, counterAttribute);
counter.add(10, counterAttribute);

const histogram = meter.createHistogram('histogram', { description: 'a histogram' });
histogram.record(10);
histogram.record(100);
histogram.record(1000);

const spyConsole = sinon.spy(console, 'dir');
const spyExport = sinon.spy(exporter, 'export');

await waitForNumberOfExports(spyExport, 1);
const resourceMetrics = spyExport.args[0];
const firstResourceMetric = resourceMetrics[0];
const consoleArgs = spyConsole.args[0];
const consoleMetric = consoleArgs[0];
const keys = Object.keys(consoleMetric).sort().join(',');

const expectedKeys = [
'dataPointType',
'dataPoints',
'descriptor',
].join(',');

assert.ok(firstResourceMetric.resource.attributes.resourceKey === 'my-resource', 'resourceKey');
assert.ok(keys === expectedKeys, 'expectedKeys');
assert.ok(consoleMetric.descriptor.name === 'counter_total', 'name');
assert.ok(consoleMetric.descriptor.description === 'a test description', 'description');
assert.ok(consoleMetric.descriptor.type === 'COUNTER', 'type');
assert.ok(consoleMetric.descriptor.unit === '', 'unit');
assert.ok(consoleMetric.descriptor.valueType === 1, 'valueType');
assert.ok(consoleMetric.dataPoints[0].attributes.key1 === 'attributeValue1', 'ensure metric attributes exists');

assert.ok(spyExport.calledOnce);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
AggregationSelector,
AggregationTemporalitySelector,
InstrumentType,
MetricReader
MetricReader,
PushMetricExporter
} from '../../src';
import * as assert from 'assert';

Expand All @@ -36,9 +37,9 @@ const instrumentTypes = [
* @param reader
* @param expectedSelector
*/
export function assertAggregationSelector(reader: MetricReader, expectedSelector: AggregationSelector) {
export function assertAggregationSelector(reader: MetricReader | PushMetricExporter, expectedSelector: AggregationSelector) {
for (const instrumentType of instrumentTypes) {
assert.strictEqual(reader.selectAggregation(instrumentType),
assert.strictEqual(reader.selectAggregation?.(instrumentType),
expectedSelector(instrumentType),
`incorrect aggregation selection for ${InstrumentType[instrumentType]}`);
}
Expand All @@ -49,9 +50,9 @@ export function assertAggregationSelector(reader: MetricReader, expectedSelector
* @param reader
* @param expectedSelector
*/
export function assertAggregationTemporalitySelector(reader: MetricReader, expectedSelector: AggregationTemporalitySelector) {
export function assertAggregationTemporalitySelector(reader: MetricReader | PushMetricExporter, expectedSelector: AggregationTemporalitySelector) {
for (const instrumentType of instrumentTypes) {
assert.strictEqual(reader.selectAggregationTemporality(instrumentType),
assert.strictEqual(reader.selectAggregationTemporality?.(instrumentType),
expectedSelector(instrumentType),
`incorrect aggregation temporality selection for ${InstrumentType[instrumentType]}`);
}
Expand Down