-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ConsoleMetricExporter (#500)
* feat: add ConsoleMetricExporter * fix: review comments * fix: linting
- Loading branch information
1 parent
fcb0f1a
commit 39732fa
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/opentelemetry-metrics/src/export/ConsoleMetricExporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*! | ||
* 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) { | ||
const labels = descriptor.labelKeys | ||
.map((k, i) => [k, ts.labelValues[i]]) | ||
.reduce( | ||
(p, c) => ({ | ||
...p, | ||
[c[0] as string]: typeof c[1] === 'string' ? c[1] : c[1].value, | ||
}), | ||
{} | ||
); | ||
for (const point of ts.points) { | ||
console.log({ labels, value: point.value }); | ||
} | ||
} | ||
} | ||
return resultCallback(ExportResult.SUCCESS); | ||
} | ||
|
||
shutdown(): void { | ||
// By default does nothing | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/opentelemetry-metrics/test/export/ConsoleMetricExporter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*! | ||
* 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', 'key2'], | ||
}) as GaugeMetric; | ||
const handle = gauge.getHandle( | ||
meter.labels({ key1: 'labelValue1', key2: 'labelValue2' }) | ||
); | ||
handle.set(10); | ||
const [descriptor, timeseries] = spyConsole.args; | ||
assert.deepStrictEqual(descriptor, [ | ||
{ description: 'a test description', name: 'gauge' }, | ||
]); | ||
assert.deepStrictEqual(timeseries, [ | ||
{ | ||
labels: { key1: 'labelValue1', key2: 'labelValue2' }, | ||
value: 10, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |