Skip to content

Commit

Permalink
feat: add InMemoryMetricExporter
Browse files Browse the repository at this point in the history
Introduces the `InMemoryMetricExporter`-class which collects metrics and stores it in memory
  • Loading branch information
weyert committed Jun 15, 2022
1 parent 5f9ef51 commit ea10c05
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './export/MetricExporter';
export * from './export/MetricProducer';
export * from './export/MetricReader';
export * from './export/PeriodicExportingMetricReader';
export * from './export/InMemoryMetricExporter';
export { InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor';
export * from './Meter';
export * from './MeterProvider';
Expand Down
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')
})
})

0 comments on commit ea10c05

Please sign in to comment.