forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metric-reader): add metric-reader (open-telemetry#2681)
- Loading branch information
1 parent
a16b544
commit 5e327e5
Showing
7 changed files
with
658 additions
and
25 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
95 changes: 95 additions & 0 deletions
95
...ental/packages/opentelemetry-sdk-metrics-base/src/export/PeriodicExportingMetricReader.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,95 @@ | ||
/* | ||
* 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 api from '@opentelemetry/api'; | ||
import { MetricReader } from './MetricReader'; | ||
import { MetricExporter } from './MetricExporter'; | ||
import { callWithTimeout, TimeoutError } from '../utils'; | ||
|
||
export type PeriodicExportingMetricReaderOptions = { | ||
exporter: MetricExporter | ||
exportIntervalMillis?: number, | ||
exportTimeoutMillis?: number | ||
} | ||
|
||
/** | ||
* {@link MetricReader} which collects metrics based on a user-configurable time interval, and passes the metrics to | ||
* the configured {@link MetricExporter} | ||
*/ | ||
export class PeriodicExportingMetricReader extends MetricReader { | ||
private _interval?: ReturnType<typeof setInterval>; | ||
|
||
private _exporter: MetricExporter; | ||
|
||
private readonly _exportInterval: number; | ||
|
||
private readonly _exportTimeout: number; | ||
|
||
constructor(options: PeriodicExportingMetricReaderOptions) { | ||
super(options.exporter.getPreferredAggregationTemporality()); | ||
|
||
if (options.exportIntervalMillis !== undefined && options.exportIntervalMillis <= 0) { | ||
throw Error('exportIntervalMillis must be greater than 0'); | ||
} | ||
|
||
if (options.exportTimeoutMillis !== undefined && options.exportTimeoutMillis <= 0) { | ||
throw Error('exportTimeoutMillis must be greater than 0'); | ||
} | ||
|
||
if (options.exportTimeoutMillis !== undefined && | ||
options.exportIntervalMillis !== undefined && | ||
options.exportIntervalMillis < options.exportTimeoutMillis) { | ||
throw Error('exportIntervalMillis must be greater than or equal to exportTimeoutMillis'); | ||
} | ||
|
||
this._exportInterval = options.exportIntervalMillis ?? 60000; | ||
this._exportTimeout = options.exportTimeoutMillis ?? 30000; | ||
this._exporter = options.exporter; | ||
} | ||
|
||
private async _runOnce(): Promise<void> { | ||
const metrics = await this.collect({}); | ||
await this._exporter.export(metrics); | ||
} | ||
|
||
protected override onInitialized(): void { | ||
// start running the interval as soon as this reader is initialized and keep handle for shutdown. | ||
this._interval = setInterval(async () => { | ||
try { | ||
await callWithTimeout(this._runOnce(), this._exportTimeout); | ||
} catch (err) { | ||
if (err instanceof TimeoutError) { | ||
api.diag.error('Export took longer than %s milliseconds and timed out.', this._exportTimeout); | ||
return; | ||
} | ||
|
||
api.diag.error('Unexpected error during export: %s', err); | ||
} | ||
}, this._exportInterval); | ||
} | ||
|
||
protected async onForceFlush(): Promise<void> { | ||
await this._exporter.forceFlush(); | ||
} | ||
|
||
protected async onShutdown(): Promise<void> { | ||
if (this._interval) { | ||
clearInterval(this._interval); | ||
} | ||
|
||
await this._exporter.shutdown(); | ||
} | ||
} |
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
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
Oops, something went wrong.