-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces the `InMemoryMetricExporter`-class which collects metrics and stores it in memory
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
experimental/packages/opentelemetry-sdk-metrics-base/src/export/InMemoryMetricExporter.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,78 @@ | ||
/* | ||
* 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 { ExportResultCode } from "@opentelemetry/core" | ||
import { ExportResult } from "@opentelemetry/core" | ||
import { InstrumentType } from "../InstrumentDescriptor" | ||
import { AggregationTemporality } from "./AggregationTemporality" | ||
import { ResourceMetrics } from "./MetricData" | ||
import { PushMetricExporter } from "./MetricExporter" | ||
|
||
/** | ||
* TODO | ||
*/ | ||
export class InMemoryMetricExporter implements PushMetricExporter { | ||
protected _shutdown = false | ||
protected _aggregationTemporality: AggregationTemporality | ||
private _metrics: ResourceMetrics[] = [] | ||
|
||
constructor(aggregationTemporality: AggregationTemporality) { | ||
this._aggregationTemporality = aggregationTemporality | ||
} | ||
|
||
/** | ||
* @inheritedDoc | ||
*/ | ||
export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void { | ||
// Avoid storing metrics when exporter is shutdown | ||
if (this. _shutdown) { | ||
process.nextTick(() => resultCallback({ code: ExportResultCode.FAILED })); | ||
return | ||
} | ||
|
||
this._metrics.push(metrics) | ||
process.nextTick(() => resultCallback({ code: ExportResultCode.SUCCESS })); | ||
} | ||
|
||
/** | ||
* Returns all the collected scope metrics | ||
* @returns ScopeMetric[] | ||
*/ | ||
public getMetrics(): ResourceMetrics[] { | ||
return this._metrics | ||
} | ||
|
||
/** | ||
* Clear the collected metrics | ||
*/ | ||
public clear(): void { | ||
this._metrics = [] | ||
} | ||
|
||
async forceFlush() { | ||
this._metrics = [] | ||
return Promise.resolve() | ||
} | ||
|
||
selectAggregationTemporality(_instrumentType: InstrumentType): AggregationTemporality { | ||
return this._aggregationTemporality | ||
} | ||
|
||
shutdown(): Promise<void> { | ||
this._shutdown = true | ||
return Promise.resolve() | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...mental/packages/opentelemetry-sdk-metrics-base/test/export/InMemoryMetricExporter.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,25 @@ | ||
/* | ||
* 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 assert from 'assert'; | ||
import { AggregationTemporality } from "../../src/export/AggregationTemporality" | ||
import { InMemoryMetricExporter } from "../../src/export/InMemoryMetricExporter" | ||
|
||
describe('InMemoryMetricExporter', () => { | ||
describe('clear metrics on request', () => { | ||
const exporter = new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE) | ||
assert.ok(exporter, 'exporter exists') | ||
}) | ||
}) |