Skip to content

Commit

Permalink
chore(instrumentation-runtime-node): remove scrape interval, remove u…
Browse files Browse the repository at this point in the history
…nused variables
  • Loading branch information
pikalovArtemN committed Jul 15, 2024
1 parent fdfea51 commit 6a84254
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export const NODE_JS_VERSION_ATTRIBUTE = 'nodejsruntime.version';
export const V8_HEAP_SIZE_NAME_ATTRIBUTE = 'heap.space.name';
export const V8_HEAP_SIZE = 'heap.size';
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { MetricCollector } from './types/metricCollector';
import { EventLoopUtilizationCollector } from './metrics/eventLoopUtilizationCollector';
import { EventLoopDelayCollector } from './metrics/eventLoopDelayCollector';
import { GCCollector } from './metrics/gcCollector';
import { HeapSizeAndUsedCollector } from './metrics/heapSizeAndUsedCollector';
import { HeapSpacesSizeAndUsedCollector } from './metrics/heapSpacesSizeAndUsedCollector';
import { ConventionalNamePrefix } from './types/ConventionalNamePrefix';

Expand All @@ -48,10 +47,6 @@ export class RuntimeNodeInstrumentation extends InstrumentationBase {
ConventionalNamePrefix.NodeJs
),
new GCCollector(this._config, ConventionalNamePrefix.V8js),
new HeapSizeAndUsedCollector(
this._config,
ConventionalNamePrefix.V8js
),
new HeapSpacesSizeAndUsedCollector(
this._config,
ConventionalNamePrefix.V8js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
*/
import { MetricCollector } from '../types/metricCollector';
import { Meter } from '@opentelemetry/api';
import { clearInterval } from 'node:timers';
import { RuntimeNodeInstrumentationConfig } from '../types';


export abstract class BaseCollector<T> implements MetricCollector {
export abstract class BaseCollector implements MetricCollector {
protected _config: RuntimeNodeInstrumentationConfig = {};

protected namePrefix: string;
private _interval: NodeJS.Timeout | undefined;
protected _scrapeQueue: T[] = [];

protected constructor(
config: RuntimeNodeInstrumentationConfig = {},
Expand All @@ -35,43 +32,19 @@ export abstract class BaseCollector<T> implements MetricCollector {
}

public disable(): void {
this._clearQueue();
clearInterval(this._interval);
this._interval = undefined;

this._config.enabled = false;
this.internalDisable();
}

public enable(): void {
this._clearQueue();
clearInterval(this._interval);
this._interval = setInterval(
() => this._addTask(),
this._config.monitoringPrecision
);

// unref so that it does not keep the process running if disable() is never called
this._interval?.unref();

this._config.enabled = true;
this.internalEnable();
}

private _clearQueue() {
this._scrapeQueue.length = 0;
}

private _addTask() {
const taskResult = this.scrape();
if (taskResult) {
this._scrapeQueue.push(taskResult);
}
}

public abstract updateMetricInstruments(meter: Meter): void;

protected abstract internalEnable(): void;

protected abstract internalDisable(): void;

protected abstract scrape(): T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface EventLoopLagInformation {
p99: number;
}

export class EventLoopDelayCollector extends BaseCollector<EventLoopLagInformation> {
export class EventLoopDelayCollector extends BaseCollector {
private _histogram: IntervalHistogram;

constructor(
Expand Down Expand Up @@ -132,9 +132,9 @@ export class EventLoopDelayCollector extends BaseCollector<EventLoopLagInformati

meter.addBatchObservableCallback(
async observableResult => {
if (this._scrapeQueue.length === 0) return;
if(!this._config.enabled) return

const data = this._scrapeQueue.shift();
const data = this.scrape();
if (data === undefined) return;

observableResult.observe(delayMin, data.min);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { eventLoopUtilization: eventLoopUtilizationCollector } = performance;

export const NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';

export class EventLoopUtilizationCollector extends BaseCollector<EventLoopUtilization> {
export class EventLoopUtilizationCollector extends BaseCollector {
constructor(
config: RuntimeNodeInstrumentationConfig = {},
namePrefix: string
Expand All @@ -40,15 +40,16 @@ export class EventLoopUtilizationCollector extends BaseCollector<EventLoopUtiliz
}
)
.addCallback(async observableResult => {
if (this._scrapeQueue.length === 0) {
return;
}
const elu = eventLoopUtilizationCollector(this._scrapeQueue.shift());
if(!this._config.enabled) return

const elu = eventLoopUtilizationCollector(this.scrape());
observableResult.observe(elu.utilization);
});
}

protected internalDisable(): void {}
protected internalDisable(): void {

}

protected internalEnable(): void {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR] = 'minor';
kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL] = 'incremental';
kinds[perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB] = 'weakcb';

export class GCCollector extends BaseCollector<null> {
export class GCCollector extends BaseCollector {
private _gcDurationByKindHistogram?: Histogram;
private _observer: PerformanceObserver;

Expand All @@ -39,6 +39,8 @@ export class GCCollector extends BaseCollector<null> {
) {
super(config, namePrefix);
this._observer = new perf_hooks.PerformanceObserver(list => {
if(!this._config.enabled) return

const entry = list.getEntries()[0];
// Node < 16 uses entry.kind
// Node >= 16 uses entry.detail.kind
Expand Down Expand Up @@ -77,8 +79,4 @@ export class GCCollector extends BaseCollector<null> {
internalDisable(): void {
this._observer.disconnect();
}

protected scrape(): null {
return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export const metricNames: Record<V8HeapSpaceMetrics, { description: string }> =
},
};

export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
HeapSpaceInfo[]
> {
export class HeapSpacesSizeAndUsedCollector extends BaseCollector {
constructor(
config: RuntimeNodeInstrumentationConfig = {},
namePrefix: string
Expand Down Expand Up @@ -86,9 +84,9 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<

meter.addBatchObservableCallback(
observableResult => {
if (this._scrapeQueue.length === 0) return;
if(!this._config.enabled) return

const data = this._scrapeQueue.shift();
const data = this.scrape();
if (data === undefined) return;
for (const space of data) {
const spaceName = space.space_name;
Expand Down
20 changes: 0 additions & 20 deletions plugins/node/instrumentation-runtime-node/src/types/heapSpaces.ts

This file was deleted.

Loading

0 comments on commit 6a84254

Please sign in to comment.