Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -8,6 +8,7 @@ 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
* feature(prometheus-serialiser): export the unit block when unit is set in metric descriptor [#3066](https://github.com/open-telemetry/opentelemetry-js/pull/3041) @weyert

### :bug: (Bug Fix)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 } 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 {
if (this._shutdown) {
setImmediate(resultCallback, { code: ExportResultCode.SUCCESS });
Comment thread
weyert marked this conversation as resolved.
Outdated
return;
}

return ConsoleMetricExporter._sendMetrics(metrics, resultCallback);
}
Comment thread
weyert marked this conversation as resolved.

forceFlush(): Promise<void> {
return Promise.resolve();
}

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) {
Comment thread
weyert marked this conversation as resolved.
Outdated
for (const metric of libraryMetrics.metrics) {
console.dir({
descriptor: metric.descriptor,
dataPointType: metric.dataPointType,
dataPoints: metric.dataPoints
});
}
}
done({ code: ExportResultCode.SUCCESS });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { AggregationTemporality } from './AggregationTemporality';
import { ResourceMetrics } from './MetricData';
import {
ExportResult,
ExportResultCode,
} from '@opentelemetry/core';
import { InstrumentType } from '../InstrumentDescriptor';

Expand Down Expand Up @@ -54,30 +53,3 @@ export interface PushMetricExporter {
*/
shutdown(): Promise<void>;
}

export class ConsoleMetricExporter implements PushMetricExporter {
protected _shutdown = true;
private _aggregationTemporality: AggregationTemporality;

constructor(aggregationTemporality?: AggregationTemporality) {
this._aggregationTemporality = aggregationTemporality ?? AggregationTemporality.CUMULATIVE;
}

export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void) {
return resultCallback({
code: ExportResultCode.FAILED,
error: new Error('Method not implemented')
});
}

selectAggregationTemporality(_instrumentType: InstrumentType) {
return this._aggregationTemporality;
}

// nothing to do
async forceFlush() {}

async shutdown() {
this._shutdown = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './export/MetricProducer';
export * from './export/MetricReader';
export * from './export/PeriodicExportingMetricReader';
export * from './export/InMemoryMetricExporter';
export * from './export/ConsoleMetricExporter';
export { InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor';
export * from './Meter';
export * from './MeterProvider';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 [];
Comment thread
weyert marked this conversation as resolved.
Outdated
}

/* 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();
Comment thread
weyert marked this conversation as resolved.
Outdated
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 = [
'dataPointType',
'dataPoints',
'descriptor',
].join(',');

assert.ok(firstResourceMetric.resource.attributes.resourceKey === 'my-resource', 'resourceKey');
Comment thread
weyert marked this conversation as resolved.
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(spyExport.calledOnce);
});
});