Skip to content
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

feat(sdk-metrics-base) Add pull controller #2517

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is collect public? I don't see it called anywhere. Is it expected to be called by exporters? If so, isn't it a little odd that calling the controller causes the controller to call the exporter? Seems like an unnecessarily convoluted export chain.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was to keep implementing a push-based versus pull-based exporter very similar, only difference being the added function registerPullCollector and having the exporter invoke the collect function itself

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you looked at implementations in other languages like python and go to see what they're doing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't really looked around much as I found your comment and figured that sounded pretty simple :) I will take a look around if I find the time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at the Go implementation (https://github.com/open-telemetry/opentelemetry-go/blob/main/sdk/metric/controller/basic/controller.go)

They have a single Controller that has (among others) Start, Collect and ForEach exposed - Start starts the periodic calling of Collect whereas Collect itself collects the metrics and pushes them to the exporter (if one is defined), so basically the same pattern as above. ForEach "calls the passed function once per instrumentation library, allowing the caller to emit metrics grouped by the library that produced them."

They differ in that I do not see a way for the exporter to obtain a handle on the Collector (as I've done through the registerPullController function) - Indeed, the Go Prometheus Exporter explicitly states it does not implement the metric.Exporter interface, but instead itself creates a pull controller (https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/prometheus/prometheus.go#L41) and calls Collect, followed by ForEach whenever the HTTP endpoint is invoked.

If we were to follow their design more closely, we should have one Collector with public Start, Collect and ForEach methods. I do not see a simple way other than the aforementioned registerPullController to expose a handle to the Collector though - What the Go Prometheus Exporter does is basically set its internal Controller as the MetricsProvider, but we do not expose that as an option in the NodeSDKConfiguration.

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