Skip to content

Commit

Permalink
feat: add ConsoleMetricExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Nov 8, 2019
1 parent f5801d2 commit 431b21d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/opentelemetry-metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@
"devDependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^12.6.9",
"@types/sinon": "^7.0.13",
"codecov": "^3.5.0",
"gts": "^1.1.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"rimraf": "^3.0.0",
"sinon": "^7.5.0",
"ts-mocha": "^6.0.0",
"ts-node": "^8.3.0",
"tslint-consistent-codestyle": "^1.15.1",
Expand Down
48 changes: 48 additions & 0 deletions packages/opentelemetry-metrics/src/export/ConsoleMetricExporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*!
* Copyright 2019, 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 { MetricExporter, ReadableMetric } from './types';
import { ExportResult } from '@opentelemetry/base';

/**
* This is implementation of {@link MetricExporter} that prints metrics data to
* the console. This class can be used for diagnostic purposes.
*/
export class ConsoleMetricExporter implements MetricExporter {
export(
metrics: ReadableMetric[],
resultCallback: (result: ExportResult) => void
): void {
for (const metric of metrics) {
const descriptor = metric.descriptor;
const timeseries = metric.timeseries;
console.log({
name: descriptor.name,
description: descriptor.description,
});
for (const ts of timeseries) {
for (const point of ts.points) {
console.log({ labels: ts.labelValues, value: point.value });
}
}
}
return resultCallback(ExportResult.SUCCESS);
}

shutdown(): void {
// By default does nothing
}
}
1 change: 1 addition & 0 deletions packages/opentelemetry-metrics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
export * from './Handle';
export * from './Meter';
export * from './Metric';
export * from './export/ConsoleMetricExporter';
export * from './export/types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*!
* Copyright 2019, 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 assert from 'assert';
import * as sinon from 'sinon';
import { ConsoleMetricExporter, Meter, GaugeMetric } from '../../src';

describe('ConsoleMetricExporter', () => {
let consoleExporter: ConsoleMetricExporter;
let previousConsoleLog: any;

beforeEach(() => {
previousConsoleLog = console.log;
console.log = () => {};
consoleExporter = new ConsoleMetricExporter();
});

afterEach(() => {
console.log = previousConsoleLog;
});

describe('.export()', () => {
it('should export information about metrics', () => {
const spyConsole = sinon.spy(console, 'log');

const meter = new Meter();
meter.addExporter(consoleExporter);
const gauge = meter.createGauge('gauge', {
description: 'a test description',
labelKeys: ['key1'],
}) as GaugeMetric;
const handle = gauge.getHandle(['labelValue1']);
handle.set(10);
const [descriptor, timeseries] = spyConsole.args;
assert.deepStrictEqual(descriptor, [
{ description: 'a test description', name: 'gauge' },
]);
assert.deepStrictEqual(timeseries, [
{ labels: [{ value: 'labelValue1' }], value: 10 },
]);
});
});
});

0 comments on commit 431b21d

Please sign in to comment.