Skip to content
Next Next commit
feat(instrumentation)!: reduce complexity
  • Loading branch information
pichlermarc committed May 3, 2024
commit 706bd88ec2ae39e96c8b3f18fda7878e31f257cf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { logs } from '@opentelemetry/api-logs';
import {
disableInstrumentations,
enableInstrumentations,
parseInstrumentationOptions,
} from './autoLoaderUtils';
import { AutoLoaderOptions } from './types_internal';

Expand All @@ -32,21 +31,24 @@ import { AutoLoaderOptions } from './types_internal';
export function registerInstrumentations(
options: AutoLoaderOptions
): () => void {
const { instrumentations } = parseInstrumentationOptions(
options.instrumentations
);
if (options.instrumentations == null) {
return () => {};
}

const tracerProvider = options.tracerProvider || trace.getTracerProvider();
const meterProvider = options.meterProvider || metrics.getMeterProvider();
const loggerProvider = options.loggerProvider || logs.getLoggerProvider();

enableInstrumentations(
instrumentations,
options.instrumentations ?? [],
tracerProvider,
meterProvider,
loggerProvider
);

return () => {
disableInstrumentations(instrumentations);
if (options.instrumentations != null) {
disableInstrumentations(options.instrumentations);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,8 @@

import { TracerProvider, MeterProvider } from '@opentelemetry/api';
import { Instrumentation } from './types';
import { AutoLoaderResult, InstrumentationOption } from './types_internal';
import { LoggerProvider } from '@opentelemetry/api-logs';

/**
* Parses the options and returns instrumentations, node plugins and
* web plugins
* @param options
*/
export function parseInstrumentationOptions(
options: InstrumentationOption[] = []
): AutoLoaderResult {
let instrumentations: Instrumentation[] = [];
for (let i = 0, j = options.length; i < j; i++) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const option = options[i] as any;
if (Array.isArray(option)) {
const results = parseInstrumentationOptions(option);
instrumentations = instrumentations.concat(results.instrumentations);
} else if (typeof option === 'function') {
instrumentations.push(new option());
} else if ((option as Instrumentation).instrumentationName) {
instrumentations.push(option);
}
}

return { instrumentations };
}

/**
* Enable instrumentations
* @param instrumentations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

export * from './autoLoader';
export { registerInstrumentations } from './autoLoader';
export { InstrumentationBase } from './platform/index';
export { InstrumentationNodeModuleDefinition } from './instrumentationNodeModuleDefinition';
export { InstrumentationNodeModuleFile } from './instrumentationNodeModuleFile';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@
*/

import { TracerProvider, MeterProvider } from '@opentelemetry/api';
import { InstrumentationBase } from './platform';
import { Instrumentation } from './types';
import { LoggerProvider } from '@opentelemetry/api-logs';

export type InstrumentationOption =
| typeof InstrumentationBase
| (typeof InstrumentationBase)[]
| Instrumentation
| Instrumentation[];

export interface AutoLoaderResult {
instrumentations: Instrumentation[];
}

export interface AutoLoaderOptions {
instrumentations?: InstrumentationOption[];
instrumentations?: Instrumentation[];
tracerProvider?: TracerProvider;
meterProvider?: MeterProvider;
loggerProvider?: LoggerProvider;
Expand Down

This file was deleted.

19 changes: 5 additions & 14 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@opentelemetry/api';
import { logs } from '@opentelemetry/api-logs';
import {
InstrumentationOption,
Instrumentation,
registerInstrumentations,
} from '@opentelemetry/instrumentation';
import {
Expand Down Expand Up @@ -51,10 +51,7 @@ import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { NodeSDKConfiguration } from './types';
import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter';
import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core';
import {
getResourceDetectorsFromEnv,
parseInstrumentationOptions,
} from './utils';
import { getResourceDetectorsFromEnv } from './utils';

/** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */

Expand Down Expand Up @@ -85,7 +82,7 @@ export class NodeSDK {
};
private _loggerProviderConfig?: LoggerProviderConfig;
private _meterProviderConfig?: MeterProviderConfig;
private _instrumentations: InstrumentationOption[];
private _instrumentations: Instrumentation[];

private _resource: IResource;
private _resourceDetectors: Array<Detector | DetectorSync>;
Expand Down Expand Up @@ -196,11 +193,7 @@ export class NodeSDK {
this._meterProviderConfig = meterProviderConfig;
}

let instrumentations: InstrumentationOption[] = [];
if (configuration.instrumentations) {
instrumentations = configuration.instrumentations;
}
this._instrumentations = instrumentations;
this._instrumentations = configuration.instrumentations ?? [];
}

/**
Expand Down Expand Up @@ -289,9 +282,7 @@ export class NodeSDK {
// TODO: This is a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/3609
// If the MeterProvider is not yet registered when instrumentations are registered, all metrics are dropped.
// This code is obsolete once https://github.com/open-telemetry/opentelemetry-js/issues/3622 is implemented.
for (const instrumentation of parseInstrumentationOptions(
this._instrumentations
)) {
for (const instrumentation of this._instrumentations) {
instrumentation.setMeterProvider(metrics.getMeterProvider());
}
}
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { ContextManager } from '@opentelemetry/api';
import { TextMapPropagator } from '@opentelemetry/api';
import { InstrumentationOption } from '@opentelemetry/instrumentation';
import { Instrumentation } from '@opentelemetry/instrumentation';
import { Detector, DetectorSync, IResource } from '@opentelemetry/resources';
import { LogRecordProcessor } from '@opentelemetry/sdk-logs';
import { MetricReader, View } from '@opentelemetry/sdk-metrics';
Expand All @@ -35,7 +35,7 @@ export interface NodeSDKConfiguration {
logRecordProcessor: LogRecordProcessor;
metricReader: MetricReader;
views: View[];
instrumentations: InstrumentationOption[];
instrumentations: Instrumentation[];
resource: IResource;
resourceDetectors: Array<Detector | DetectorSync>;
sampler: Sampler;
Expand Down
27 changes: 0 additions & 27 deletions experimental/packages/opentelemetry-sdk-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/

import { diag } from '@opentelemetry/api';
import {
Instrumentation,
InstrumentationOption,
} from '@opentelemetry/instrumentation';
import {
DetectorSync,
envDetectorSync,
Expand All @@ -28,29 +24,6 @@ import {
serviceInstanceIdDetectorSync,
} from '@opentelemetry/resources';

// TODO: This part of a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/3609
// If the MeterProvider is not yet registered when instrumentations are registered, all metrics are dropped.
// This code is obsolete once https://github.com/open-telemetry/opentelemetry-js/issues/3622 is implemented.
export function parseInstrumentationOptions(
options: InstrumentationOption[] = []
): Instrumentation[] {
let instrumentations: Instrumentation[] = [];
for (let i = 0, j = options.length; i < j; i++) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const option = options[i] as any;
if (Array.isArray(option)) {
const results = parseInstrumentationOptions(option);
instrumentations = instrumentations.concat(results);
} else if (typeof option === 'function') {
instrumentations.push(new option());
} else if ((option as Instrumentation).instrumentationName) {
instrumentations.push(option);
}
}

return instrumentations;
}

const RESOURCE_DETECTOR_ENVIRONMENT = 'env';
const RESOURCE_DETECTOR_HOST = 'host';
const RESOURCE_DETECTOR_OS = 'os';
Expand Down