-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tracers provided by the API become useable when provider regist…
…ered (#1448)
- Loading branch information
Showing
9 changed files
with
313 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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 { Span, SpanOptions, Tracer } from '..'; | ||
import { NOOP_TRACER } from './NoopTracer'; | ||
import { ProxyTracerProvider } from './ProxyTracerProvider'; | ||
|
||
/** | ||
* Proxy tracer provided by the proxy tracer provider | ||
*/ | ||
export class ProxyTracer implements Tracer { | ||
// When a real implementation is provided, this will be it | ||
private _delegate?: Tracer; | ||
|
||
constructor( | ||
private _provider: ProxyTracerProvider, | ||
public readonly name: string, | ||
public readonly version?: string | ||
) {} | ||
|
||
getCurrentSpan(): Span | undefined { | ||
return this._getTracer().getCurrentSpan(); | ||
} | ||
|
||
startSpan(name: string, options?: SpanOptions): Span { | ||
return this._getTracer().startSpan(name, options); | ||
} | ||
|
||
withSpan<T extends (...args: unknown[]) => ReturnType<T>>( | ||
span: Span, | ||
fn: T | ||
): ReturnType<T> { | ||
return this._getTracer().withSpan(span, fn); | ||
} | ||
|
||
bind<T>(target: T, span?: Span): T { | ||
return this._getTracer().bind(target, span); | ||
} | ||
|
||
/** | ||
* Try to get a tracer from the proxy tracer provider. | ||
* If the proxy tracer provider has no delegate, return a noop tracer. | ||
*/ | ||
private _getTracer() { | ||
if (this._delegate) { | ||
return this._delegate; | ||
} | ||
|
||
const tracer = this._provider.getDelegateTracer(this.name, this.version); | ||
|
||
if (!tracer) { | ||
return NOOP_TRACER; | ||
} | ||
|
||
this._delegate = tracer; | ||
return this._delegate; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/opentelemetry-api/src/trace/ProxyTracerProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 { Tracer } from './tracer'; | ||
import { TracerProvider } from './tracer_provider'; | ||
import { ProxyTracer } from './ProxyTracer'; | ||
import { NOOP_TRACER_PROVIDER } from './NoopTracerProvider'; | ||
|
||
/** | ||
* Tracer provider which provides {@link ProxyTracer}s. | ||
* | ||
* Before a delegate is set, tracers provided are NoOp. | ||
* When a delegate is set, traces are provided from the delegate. | ||
* When a delegate is set after tracers have already been provided, | ||
* all tracers already provided will use the provided delegate implementation. | ||
*/ | ||
export class ProxyTracerProvider implements TracerProvider { | ||
private _delegate?: TracerProvider; | ||
|
||
/** | ||
* Get a {@link ProxyTracer} | ||
*/ | ||
getTracer(name: string, version?: string): Tracer { | ||
return ( | ||
this.getDelegateTracer(name, version) ?? | ||
new ProxyTracer(this, name, version) | ||
); | ||
} | ||
|
||
getDelegate(): TracerProvider { | ||
return this._delegate ?? NOOP_TRACER_PROVIDER; | ||
} | ||
|
||
/** | ||
* Set the delegate tracer provider | ||
*/ | ||
setDelegate(delegate: TracerProvider) { | ||
this._delegate = delegate; | ||
} | ||
|
||
getDelegateTracer(name: string, version?: string): Tracer | undefined { | ||
return this._delegate?.getTracer(name, version); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* 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 * as sinon from 'sinon'; | ||
import { | ||
NoopSpan, | ||
NOOP_SPAN, | ||
ProxyTracerProvider, | ||
SpanKind, | ||
TracerProvider, | ||
ProxyTracer, | ||
Tracer, | ||
Span, | ||
NoopTracer, | ||
} from '../../src'; | ||
|
||
describe('ProxyTracer', () => { | ||
let provider: ProxyTracerProvider; | ||
|
||
beforeEach(() => { | ||
provider = new ProxyTracerProvider(); | ||
}); | ||
|
||
describe('when no delegate is set', () => { | ||
it('should return proxy tracers', () => { | ||
const tracer = provider.getTracer('test'); | ||
|
||
assert.ok(tracer instanceof ProxyTracer); | ||
}); | ||
|
||
it('startSpan should return Noop Spans', () => { | ||
const tracer = provider.getTracer('test'); | ||
|
||
assert.deepStrictEqual(tracer.startSpan('span-name'), NOOP_SPAN); | ||
assert.deepStrictEqual( | ||
tracer.startSpan('span-name1', { kind: SpanKind.CLIENT }), | ||
NOOP_SPAN | ||
); | ||
assert.deepStrictEqual( | ||
tracer.startSpan('span-name2', { | ||
kind: SpanKind.CLIENT, | ||
}), | ||
NOOP_SPAN | ||
); | ||
|
||
assert.deepStrictEqual(tracer.getCurrentSpan(), NOOP_SPAN); | ||
}); | ||
}); | ||
|
||
describe('when delegate is set before getTracer', () => { | ||
let delegate: TracerProvider; | ||
const sandbox = sinon.createSandbox(); | ||
let getTracerStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
getTracerStub = sandbox.stub().returns(new NoopTracer()); | ||
delegate = { | ||
getTracer: getTracerStub, | ||
}; | ||
provider.setDelegate(delegate); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should return tracers directly from the delegate', () => { | ||
const tracer = provider.getTracer('test', 'v0'); | ||
|
||
sandbox.assert.calledOnce(getTracerStub); | ||
assert.strictEqual(getTracerStub.firstCall.returnValue, tracer); | ||
assert.deepEqual(getTracerStub.firstCall.args, ['test', 'v0']); | ||
}); | ||
}); | ||
|
||
describe('when delegate is set after getTracer', () => { | ||
let tracer: Tracer; | ||
let delegate: TracerProvider; | ||
let delegateSpan: Span; | ||
let delegateTracer: Tracer; | ||
|
||
beforeEach(() => { | ||
delegateSpan = new NoopSpan(); | ||
delegateTracer = { | ||
bind(target) { | ||
return target; | ||
}, | ||
getCurrentSpan() { | ||
return delegateSpan; | ||
}, | ||
startSpan() { | ||
return delegateSpan; | ||
}, | ||
withSpan(span, fn) { | ||
return fn(); | ||
}, | ||
}; | ||
|
||
tracer = provider.getTracer('test'); | ||
|
||
delegate = { | ||
getTracer() { | ||
return delegateTracer; | ||
}, | ||
}; | ||
provider.setDelegate(delegate); | ||
}); | ||
|
||
it('should create spans using the delegate tracer', () => { | ||
const span = tracer.startSpan('test'); | ||
|
||
assert.strictEqual(span, delegateSpan); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.