-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add ConsoleMetricExporter #3120
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
Merged
legendecas
merged 17 commits into
open-telemetry:main
from
weyert:add-console-metrics-exporter
Aug 8, 2022
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e8cda44
feat: add ConsoleMetricExporter
tapico-weyert 333019e
docs: update CHANGELOG.md
tapico-weyert bd40c61
style: resolve linting issues
tapico-weyert 60ef869
fix: improve logging for metrics console exporter
tapico-weyert a2b47a8
fix: improve code for metrics console exporter
tapico-weyert 7f24efc
Merge branch 'main' into add-console-metrics-exporter
weyert 3f7fc67
fix: ensure correct ConsoleMetricExporter gets exported in package
tapico-weyert dc98897
Merge branch 'main' into add-console-metrics-exporter
weyert c201175
Merge branch 'main' into add-console-metrics-exporter
weyert b37c0ca
fix: remove unused import statement
tapico-weyert af4c7d4
Merge branch 'main' into add-console-metrics-exporter
weyert 53c44eb
Merge branch 'main' into add-console-metrics-exporter
weyert 0590c4e
fix: ensure FAILED is returned when export() is called while exportin…
tapico-weyert fedcd7b
test: improve the ConsoleMetricExporter tests
tapico-weyert c41a12d
test: remove unnecessary `exporter.shutdown()`
tapico-weyert d02f6c5
Merge branch 'main' into add-console-metrics-exporter
weyert 76d933f
Merge branch 'main' into add-console-metrics-exporter
weyert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
62 changes: 62 additions & 0 deletions
62
experimental/packages/opentelemetry-sdk-metrics-base/src/export/ConsoleMetricExporter.ts
This file contains hidden or 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,62 @@ | ||
| /* | ||
| * 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) { | ||
| // If the exporter is shutting down, by spec, we need to return FAILED as export result | ||
| setImmediate(resultCallback, { code: ExportResultCode.FAILED }); | ||
| return; | ||
| } | ||
|
|
||
| return ConsoleMetricExporter._sendMetrics(metrics, resultCallback); | ||
| } | ||
|
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 scopeMetrics of metrics.scopeMetrics) { | ||
| for (const metric of scopeMetrics.metrics) { | ||
| console.dir({ | ||
| descriptor: metric.descriptor, | ||
| dataPointType: metric.dataPointType, | ||
| dataPoints: metric.dataPoints | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| done({ code: ExportResultCode.SUCCESS }); | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
...imental/packages/opentelemetry-sdk-metrics-base/test/export/ConsoleMetricExporter.test.ts
This file contains hidden or 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,108 @@ | ||
| /* | ||
| * 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<void> { | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| /* 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 meterReader.shutdown(); | ||
| }); | ||
|
|
||
| it('should export information about metric', 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'); | ||
|
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(consoleMetric.dataPoints[0].attributes.key1 === 'attributeValue1', 'ensure metric attributes exists'); | ||
|
|
||
| assert.ok(spyExport.calledOnce); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably good to include a reason