-
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.
- Loading branch information
1 parent
f5801d2
commit 431b21d
Showing
4 changed files
with
107 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
48 changes: 48 additions & 0 deletions
48
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,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 | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
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,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 }, | ||
]); | ||
}); | ||
}); | ||
}); |