From c87739ba776516a8ac52cdc8f3937dca7428f12d Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 5 May 2020 00:21:40 +0800 Subject: [PATCH] fix: prevent duplicated resource creation --- packages/opentelemetry-metrics/src/MeterProvider.ts | 12 +++++++++--- .../opentelemetry-metrics/test/MeterProvider.test.ts | 10 +++++++++- .../opentelemetry-tracing/src/BasicTracerProvider.ts | 11 ++++++++--- .../test/BasicTracerProvider.test.ts | 8 ++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry-metrics/src/MeterProvider.ts b/packages/opentelemetry-metrics/src/MeterProvider.ts index 4bb0d33928..0315a76e35 100644 --- a/packages/opentelemetry-metrics/src/MeterProvider.ts +++ b/packages/opentelemetry-metrics/src/MeterProvider.ts @@ -24,12 +24,18 @@ import { DEFAULT_CONFIG, MeterConfig } from './types'; * This class represents a meter provider which platform libraries can extend */ export class MeterProvider implements api.MeterProvider { + private readonly _config: MeterConfig; private readonly _meters: Map = new Map(); - readonly resource: Resource = Resource.createTelemetrySDKResource(); + readonly resource: Resource; readonly logger: api.Logger; - constructor(private _config: MeterConfig = DEFAULT_CONFIG) { - this.logger = _config.logger || new ConsoleLogger(_config.logLevel); + constructor(config: MeterConfig = DEFAULT_CONFIG) { + this.logger = config.logger ?? new ConsoleLogger(config.logLevel); + this.resource = config.resource ?? Resource.createTelemetrySDKResource(); + this._config = Object.assign({}, config, { + logger: this.logger, + resource: this.resource, + }); } /** diff --git a/packages/opentelemetry-metrics/test/MeterProvider.test.ts b/packages/opentelemetry-metrics/test/MeterProvider.test.ts index 82877759b3..45c54db6bb 100644 --- a/packages/opentelemetry-metrics/test/MeterProvider.test.ts +++ b/packages/opentelemetry-metrics/test/MeterProvider.test.ts @@ -15,7 +15,7 @@ */ import * as assert from 'assert'; -import { MeterProvider, Meter } from '../src'; +import { MeterProvider, Meter, CounterMetric } from '../src'; import { NoopLogger } from '@opentelemetry/core'; describe('MeterProvider', () => { @@ -39,6 +39,14 @@ describe('MeterProvider', () => { assert.ok(meter instanceof Meter); }); + it('should propagate resources', () => { + const meterProvider = new MeterProvider(); + const meter = meterProvider.getMeter('test-meter-provider'); + const counter = meter.createCounter('test-counter') as CounterMetric; + assert.strictEqual((meter as any)._resource, meterProvider.resource); + assert.strictEqual(counter.resource, meterProvider.resource); + }); + it('should return the meter with default version without a version option', () => { const provider = new MeterProvider(); const meter1 = provider.getMeter('default'); diff --git a/packages/opentelemetry-tracing/src/BasicTracerProvider.ts b/packages/opentelemetry-tracing/src/BasicTracerProvider.ts index fe44199e61..375f998aa7 100644 --- a/packages/opentelemetry-tracing/src/BasicTracerProvider.ts +++ b/packages/opentelemetry-tracing/src/BasicTracerProvider.ts @@ -27,6 +27,7 @@ import { Resource } from '@opentelemetry/resources'; * This class represents a basic tracer provider which platform libraries can extend */ export class BasicTracerProvider implements api.TracerProvider { + private readonly _config: TracerConfig; private readonly _registeredSpanProcessors: SpanProcessor[] = []; private readonly _tracers: Map = new Map(); @@ -34,9 +35,13 @@ export class BasicTracerProvider implements api.TracerProvider { readonly logger: api.Logger; readonly resource: Resource; - constructor(private _config: TracerConfig = DEFAULT_CONFIG) { - this.logger = _config.logger || new ConsoleLogger(_config.logLevel); - this.resource = _config.resource || Resource.createTelemetrySDKResource(); + constructor(config: TracerConfig = DEFAULT_CONFIG) { + this.logger = config.logger ?? new ConsoleLogger(config.logLevel); + this.resource = config.resource ?? Resource.createTelemetrySDKResource(); + this._config = Object.assign({}, config, { + logger: this.logger, + resource: this.resource, + }); } getTracer(name: string, version = '*', config?: TracerConfig): Tracer { diff --git a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts b/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts index 4b38b09046..6de7339798 100644 --- a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts @@ -121,6 +121,14 @@ describe('BasicTracerProvider', () => { assert.ok(span instanceof Span); }); + it('should propagate resources', () => { + const tracerProvider = new BasicTracerProvider(); + const tracer = tracerProvider.getTracer('default'); + const span = tracer.startSpan('my-span') as Span; + assert.strictEqual(tracer.resource, tracerProvider.resource); + assert.strictEqual(span.resource, tracerProvider.resource); + }); + it('should start a span with name and options', () => { const tracer = new BasicTracerProvider().getTracer('default'); const span = tracer.startSpan('my-span', {});