Skip to content

Commit

Permalink
fix: prevent duplicated resource creation
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed May 6, 2020
1 parent a5444dc commit c87739b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
12 changes: 9 additions & 3 deletions packages/opentelemetry-metrics/src/MeterProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Meter> = 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,
});
}

/**
Expand Down
10 changes: 9 additions & 1 deletion packages/opentelemetry-metrics/test/MeterProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
Expand Down
11 changes: 8 additions & 3 deletions packages/opentelemetry-tracing/src/BasicTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ 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<string, Tracer> = new Map();

activeSpanProcessor = new NoopSpanProcessor();
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', {});
Expand Down

0 comments on commit c87739b

Please sign in to comment.