Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent duplicated resource creation #1015

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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