Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
feat(trace): add optional schema url to TracerProvider.getTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Nov 11, 2021
1 parent 35582a7 commit e897b37
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/trace/NoopTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { NoopTracer } from './NoopTracer';
import { Tracer } from './tracer';
import { TracerOptions } from './tracer_options';
import { TracerProvider } from './tracer_provider';

/**
Expand All @@ -25,7 +26,11 @@ import { TracerProvider } from './tracer_provider';
* All operations are no-op.
*/
export class NoopTracerProvider implements TracerProvider {
getTracer(_name?: string, _version?: string): Tracer {
getTracer(
_name?: string,
_version?: string,
_options?: TracerOptions
): Tracer {
return new NoopTracer();
}
}
3 changes: 2 additions & 1 deletion src/trace/ProxyTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class ProxyTracer implements Tracer {
constructor(
private _provider: TracerDelegator,
public readonly name: string,
public readonly version?: string
public readonly version?: string,
public readonly schemaUrl?: string
) {}

startSpan(name: string, options?: SpanOptions, context?: Context): Span {
Expand Down
15 changes: 10 additions & 5 deletions src/trace/ProxyTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Tracer } from './tracer';
import { TracerProvider } from './tracer_provider';
import { ProxyTracer } from './ProxyTracer';
import { NoopTracerProvider } from './NoopTracerProvider';
import { TracerOptions } from './tracer_options';

const NOOP_TRACER_PROVIDER = new NoopTracerProvider();

Expand All @@ -35,10 +36,10 @@ export class ProxyTracerProvider implements TracerProvider {
/**
* Get a {@link ProxyTracer}
*/
getTracer(name: string, version?: string): Tracer {
getTracer(name: string, version?: string, options?: TracerOptions): Tracer {
return (
this.getDelegateTracer(name, version) ??
new ProxyTracer(this, name, version)
this.getDelegateTracer(name, version, options) ??
new ProxyTracer(this, name, version, options?.schemaUrl)
);
}

Expand All @@ -53,7 +54,11 @@ export class ProxyTracerProvider implements TracerProvider {
this._delegate = delegate;
}

getDelegateTracer(name: string, version?: string): Tracer | undefined {
return this._delegate?.getTracer(name, version);
getDelegateTracer(
name: string,
version?: string,
options?: TracerOptions
): Tracer | undefined {
return this._delegate?.getTracer(name, version, options);
}
}
25 changes: 25 additions & 0 deletions src/trace/tracer_options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* An interface describes additional metadata of a tracer.
*/
export interface TracerOptions {
/**
* The schemaUrl of the tracer or instrumentation library
*/
schemaUrl?: string;
}
3 changes: 2 additions & 1 deletion src/trace/tracer_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { Tracer } from './tracer';
import { TracerOptions } from './tracer_options';

/**
* A registry for creating named {@link Tracer}s.
Expand All @@ -31,5 +32,5 @@ export interface TracerProvider {
* @param version The version of the tracer or instrumentation library.
* @returns Tracer A Tracer with the given name and version
*/
getTracer(name: string, version?: string): Tracer;
getTracer(name: string, version?: string, options?: TracerOptions): Tracer;
}
31 changes: 31 additions & 0 deletions test/noop-implementations/noop-tracer-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import { NoopTracer } from '../../src/trace/NoopTracer';
import { NoopTracerProvider } from '../../src/trace/NoopTracerProvider';

describe('NoopTracerProvider', () => {
it('should not crash', () => {
const tracerProvider = new NoopTracerProvider();

assert.ok(tracerProvider.getTracer('tracer-name') instanceof NoopTracer);
assert.ok(tracerProvider.getTracer('tracer-name', 'v1') instanceof NoopTracer);
assert.ok(tracerProvider.getTracer('tracer-name', 'v1', {
schemaUrl: 'http://example.com/schema.json'
}) instanceof NoopTracer);
});
});
20 changes: 19 additions & 1 deletion test/proxy-implementations/proxy-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,25 @@ describe('ProxyTracer', () => {

sandbox.assert.calledOnce(getTracerStub);
assert.strictEqual(getTracerStub.firstCall.returnValue, tracer);
assert.deepEqual(getTracerStub.firstCall.args, ['test', 'v0']);
assert.deepStrictEqual(getTracerStub.firstCall.args, [
'test',
'v0',
undefined,
]);
});

it('should return tracers directly from the delegate with schema url', () => {
const tracer = provider.getTracer('test', 'v0', {
schemaUrl: 'https://opentelemetry.io/schemas/1.7.0',
});

sandbox.assert.calledOnce(getTracerStub);
assert.strictEqual(getTracerStub.firstCall.returnValue, tracer);
assert.deepStrictEqual(getTracerStub.firstCall.args, [
'test',
'v0',
{ schemaUrl: 'https://opentelemetry.io/schemas/1.7.0' },
]);
});
});

Expand Down

0 comments on commit e897b37

Please sign in to comment.