Skip to content

Commit

Permalink
Add PullController
Browse files Browse the repository at this point in the history
  • Loading branch information
Sobuno committed Oct 1, 2021
1 parent d15127c commit f734068
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
20 changes: 14 additions & 6 deletions experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Resource } from '@opentelemetry/resources';
import { BatchObserver } from './BatchObserver';
import { BaseBoundInstrument } from './BoundInstrument';
import { CounterMetric } from './CounterMetric';
import { PushController } from './export/Controller';
import { PushController, Controller, PullController } from './export/Controller';
import { NoopExporter } from './export/NoopExporter';
import { Processor, UngroupedProcessor } from './export/Processor';
import { Metric } from './Metric';
Expand All @@ -31,6 +31,7 @@ import { UpDownCounterMetric } from './UpDownCounterMetric';
import { UpDownSumObserverMetric } from './UpDownSumObserverMetric';
import { ValueObserverMetric } from './ValueObserverMetric';
import { ValueRecorderMetric } from './ValueRecorderMetric';
import { MetricExporter } from './export/types';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const merge = require('lodash.merge');

Expand All @@ -43,7 +44,7 @@ export class Meter implements api.Meter {
private readonly _processor: Processor;
private readonly _resource: Resource;
private readonly _instrumentationLibrary: InstrumentationLibrary;
private readonly _controller: PushController;
private readonly _controller: Controller;
private _isShutdown = false;
private _shuttingDownPromise: Promise<void> = Promise.resolve();

Expand All @@ -59,10 +60,17 @@ export class Meter implements api.Meter {
this._resource =
mergedConfig.resource || Resource.empty();
this._instrumentationLibrary = instrumentationLibrary;
// start the push controller
const exporter = mergedConfig.exporter || new NoopExporter();
const interval = mergedConfig.interval;
this._controller = new PushController(this, exporter, interval);
const exporter: MetricExporter = mergedConfig.exporter || new NoopExporter();

// start the pull or push controller, depending on if the exporter defines the optional function registerPullController
if(typeof exporter.registerPullController === 'function') {
const pullController = new PullController(this, exporter);
this._controller = pullController;
exporter.registerPullController(pullController);
} else {
const interval = mergedConfig.interval;
this._controller = new PushController(this, exporter, interval);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const DEFAULT_EXPORT_INTERVAL = 60_000;

export class Controller {}

/** Controller organizes a periodic push of metric data. */
/** PushController organizes a periodic push of metric data. */
export class PushController extends Controller {
private _timer: NodeJS.Timeout;

Expand Down Expand Up @@ -67,3 +67,35 @@ export class PushController extends Controller {
});
}
}

/** PullController pulls metric data whenever the exporter requests it. */
export class PullController extends Controller {
constructor(
private readonly _meter: Meter,
private readonly _exporter: MetricExporter,
) {
super();
}

shutdown(): Promise<void> {
return this._collect();
}

public async _collect(): Promise<void> {
await this._meter.collect();
return new Promise(resolve => {
this._exporter.export(
this._meter.getProcessor().checkPointSet(),
result => {
if (result.code !== ExportResultCode.SUCCESS) {
globalErrorHandler(
result.error ??
new Error('PullController: export failed in _collect')
);
}
resolve();
}
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@opentelemetry/api-metrics';
import { ExportResult, InstrumentationLibrary } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { PullController } from './Controller';

/** The kind of metric. */
export enum MetricKind {
Expand Down Expand Up @@ -104,6 +105,11 @@ export interface MetricExporter {
resultCallback: (result: ExportResult) => void
): void;

/** Registers a {@Link PullController}. If this function is defined, the MetricsExporter will use a {@Link PullController} instead of a {@Link PushController} */
registerPullController?(
pullController: PullController
): void;

/** Stops the exporter. */
shutdown(): Promise<void>;
}
Expand Down

0 comments on commit f734068

Please sign in to comment.