Skip to content

Commit

Permalink
feat(opentelemetry-sdk-node): update metrics-sdk configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Mar 31, 2022
1 parent 65b7819 commit ff547ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
32 changes: 12 additions & 20 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { TextMapPropagator } from '@opentelemetry/api';
import { metrics } from '@opentelemetry/api-metrics';
import { ContextManager } from '@opentelemetry/api';
import { MeterConfig, MeterProvider } from '@opentelemetry/sdk-metrics-base';
import { MeterProvider, MetricReader } from '@opentelemetry/sdk-metrics-base';
import {
InstrumentationOption,
registerInstrumentations,
Expand All @@ -44,7 +44,7 @@ export class NodeSDK {
textMapPropagator?: TextMapPropagator;
};
private _instrumentations: InstrumentationOption[];
private _meterProviderConfig?: MeterConfig;
private _metricReader?: MetricReader;

private _resource: Resource;

Expand Down Expand Up @@ -83,19 +83,8 @@ export class NodeSDK {
);
}

if (configuration.metricExporter) {
const meterConfig: MeterConfig = {
exporter: configuration.metricExporter,
};

if (configuration.metricProcessor) {
meterConfig.processor = configuration.metricProcessor;
}
if (typeof configuration.metricInterval === 'number') {
meterConfig.interval = configuration.metricInterval;
}

this.configureMeterProvider(meterConfig);
if (configuration.metricReader) {
this.configureMeterProvider(configuration.metricReader);
}

let instrumentations: InstrumentationOption[] = [];
Expand All @@ -104,6 +93,7 @@ export class NodeSDK {
}
this._instrumentations = instrumentations;
}

/** Set configurations required to register a NodeTracerProvider */
public configureTracerProvider(
tracerConfig: NodeTracerConfig,
Expand All @@ -120,8 +110,8 @@ export class NodeSDK {
}

/** Set configurations needed to register a MeterProvider */
public configureMeterProvider(config: MeterConfig): void {
this._meterProviderConfig = config;
public configureMeterProvider(reader: MetricReader): void {
this._metricReader = reader;
}

/** Detect resource attributes */
Expand Down Expand Up @@ -162,12 +152,13 @@ export class NodeSDK {
});
}

if (this._meterProviderConfig) {
if (this._metricReader) {
const meterProvider = new MeterProvider({
...this._meterProviderConfig,
resource: this._resource,
});

meterProvider.addMetricReader(this._metricReader);

this._meterProvider = meterProvider;

metrics.setGlobalMeterProvider(meterProvider);
Expand All @@ -190,7 +181,8 @@ export class NodeSDK {
return (
Promise.all(promises)
// return void instead of the array from Promise.all
.then(() => {})
.then(() => {
})
);
}
}
5 changes: 2 additions & 3 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { SpanAttributes, TextMapPropagator, Sampler } from '@opentelemetry/api';
import type { ContextManager } from '@opentelemetry/api';
import { InstrumentationOption } from '@opentelemetry/instrumentation';
import { MetricExporter, Processor } from '@opentelemetry/sdk-metrics-base';
import { MetricReader } from '@opentelemetry/sdk-metrics-base';
import { Resource } from '@opentelemetry/resources';
import {
SpanExporter,
Expand All @@ -30,8 +30,7 @@ export interface NodeSDKConfiguration {
contextManager: ContextManager;
defaultAttributes: SpanAttributes;
textMapPropagator: TextMapPropagator;
metricProcessor: Processor;
metricExporter: MetricExporter;
metricReader: MetricReader;
metricInterval: number;
instrumentations: InstrumentationOption[];
resource: Resource;
Expand Down
13 changes: 10 additions & 3 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
AsyncLocalStorageContextManager,
} from '@opentelemetry/context-async-hooks';
import { CompositePropagator } from '@opentelemetry/core';
import { ConsoleMetricExporter, MeterProvider } from '@opentelemetry/sdk-metrics-base';
import { ConsoleMetricExporter, MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { awsEc2Detector } from '@opentelemetry/resource-detector-aws';
import { resetIsAvailableCache } from '@opentelemetry/resource-detector-gcp';
Expand Down Expand Up @@ -163,11 +163,16 @@ describe('Node SDK', () => {
assert.ok(apiTracerProvider.getDelegate() instanceof NodeTracerProvider);
});

it('should register a meter provider if an exporter is provided', async () => {
it('should register a meter provider if a reader is provided', async () => {
const exporter = new ConsoleMetricExporter();
const metricReader = new PeriodicExportingMetricReader({
exporter: exporter,
exportIntervalMillis: 100,
exportTimeoutMillis: 100
});

const sdk = new NodeSDK({
metricExporter: exporter,
metricReader: metricReader,
autoDetectResources: false,
});

Expand All @@ -178,6 +183,8 @@ describe('Node SDK', () => {
assert.strictEqual((trace.getTracerProvider() as ProxyTracerProvider).getDelegate(), delegate, 'tracer provider should not have changed');

assert.ok(metrics.getMeterProvider() instanceof MeterProvider);

await sdk.shutdown();
});
});

Expand Down

0 comments on commit ff547ff

Please sign in to comment.