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: add ConsoleMetricExporter #3120

Merged
merged 17 commits into from
Aug 8, 2022
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
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feature(add-console-metrics-exporter): add ConsoleMetricExporter [#3120](https://github.com/open-telemetry/opentelemetry-js/pull/3120) @weyert

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ExportResult, ExportResultCode } from '@opentelemetry/core';
import { InstrumentType } from '../InstrumentDescriptor';
import { AggregationTemporality } from './AggregationTemporality';
import { ResourceMetrics, DataPointType } from './MetricData';
import { PushMetricExporter } from './MetricExporter';

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

export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void {
return ConsoleMetricExporter._sendMetrics(metrics, resultCallback);
}
weyert marked this conversation as resolved.
Show resolved Hide resolved

async forceFlush() {}
weyert marked this conversation as resolved.
Show resolved Hide resolved

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

shutdown(): Promise<void> {
this._shutdown = true;
return Promise.resolve();
}

private static _sendMetrics(metrics: ResourceMetrics, done: (result: ExportResult) => void): void {
for (const libraryMetrics of metrics.scopeMetrics) {
weyert marked this conversation as resolved.
Show resolved Hide resolved
for (const metric of libraryMetrics.metrics) {
console.dir(metric.descriptor);
console.dir(DataPointType[metric.dataPointType]);
for (const dataPoint of metric.dataPoints) {
console.dir(dataPoint);
weyert marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
done({ code: ExportResultCode.SUCCESS });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as metrics from '@opentelemetry/api-metrics';
import { ExportResult } from '@opentelemetry/core';
import { ConsoleMetricExporter } from '../../src/export/ConsoleMetricExporter';
import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader';
import { ResourceMetrics } from '../../src/export/MetricData';
import { MeterProvider } from '../../src/MeterProvider';
import { defaultResource } from '../util';
import * as assert from 'assert';
import * as sinon from 'sinon';


async function waitForNumberOfExports(exporter: sinon.SinonSpy<[metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void], void>, numberOfExports: number): Promise<ResourceMetrics[]> {
if (numberOfExports <= 0) {
throw new Error('numberOfExports must be greater than or equal to 0');
}

let totalExports = 0;
while (totalExports < numberOfExports) {
await new Promise(resolve => setTimeout(resolve, 20));
totalExports = exporter.callCount;
}

return [];
weyert marked this conversation as resolved.
Show resolved Hide resolved
}

/* 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
});
meterProvider.addMetricReader(meterReader);
});

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

await exporter.shutdown();
weyert marked this conversation as resolved.
Show resolved Hide resolved
await meterReader.shutdown();
});

it('should export information about span', 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(',');

const expectedKeys = [
'description',
'name',
'type',
'unit',
'valueType',
].join(',');

assert.ok(firstResourceMetric.resource.attributes.resourceKey === 'my-resource', 'resourceKey');
weyert marked this conversation as resolved.
Show resolved Hide resolved
assert.ok(keys === expectedKeys, 'expectedKeys');
assert.ok(consoleMetric.name === 'counter_total', 'name');
assert.ok(consoleMetric.description === 'a test description', 'description');
assert.ok(consoleMetric.type === 'COUNTER', 'type');
assert.ok(consoleMetric.unit === '', 'unit');
assert.ok(consoleMetric.valueType === 1, 'valueType');

assert.ok(spyExport.calledOnce);
});
});