Skip to content

Commit 0e3d36c

Browse files
committed
chore: address PR comments
1 parent 3ce40ab commit 0e3d36c

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

packages/opentelemetry-api/src/api/context.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class ContextAPI {
5454
): ContextManager {
5555
if (_global[GLOBAL_CONTEXT_MANAGER_API_KEY]) {
5656
// global context manager has already been set
57-
return this._getContextManager();
57+
return this.getContextManager();
5858
}
5959

6060
_global[GLOBAL_CONTEXT_MANAGER_API_KEY] = makeGetter(
@@ -70,7 +70,7 @@ export class ContextAPI {
7070
* Get the currently active context
7171
*/
7272
public active(): Context {
73-
return this._getContextManager().active();
73+
return this.getContextManager().active();
7474
}
7575

7676
/**
@@ -83,7 +83,7 @@ export class ContextAPI {
8383
context: Context,
8484
fn: T
8585
): ReturnType<T> {
86-
return this._getContextManager().with(context, fn);
86+
return this.getContextManager().with(context, fn);
8787
}
8888

8989
/**
@@ -93,10 +93,10 @@ export class ContextAPI {
9393
* @param context context to bind to the event emitter or function. Defaults to the currently active context
9494
*/
9595
public bind<T>(target: T, context: Context = this.active()): T {
96-
return this._getContextManager().bind(target, context);
96+
return this.getContextManager().bind(target, context);
9797
}
9898

99-
private _getContextManager(): ContextManager {
99+
public getContextManager(): ContextManager {
100100
return (
101101
_global[GLOBAL_CONTEXT_MANAGER_API_KEY]?.(
102102
API_BACKWARDS_COMPATIBILITY_VERSION
@@ -106,7 +106,7 @@ export class ContextAPI {
106106

107107
/** Disable and remove the global context manager */
108108
public disable() {
109-
this._getContextManager().disable();
109+
this.getContextManager().disable();
110110
delete _global[GLOBAL_CONTEXT_MANAGER_API_KEY];
111111
}
112112
}

packages/opentelemetry-api/test/api/global.test.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('Global Utils', () => {
3434
assert.notEqual(api1, api2);
3535
// that return separate noop instances to start
3636
assert.notStrictEqual(
37-
api1.context['_getContextManager'](),
38-
api2.context['_getContextManager']()
37+
api1.context['getContextManager'](),
38+
api2.context['getContextManager']()
3939
);
4040

4141
beforeEach(() => {
@@ -46,33 +46,33 @@ describe('Global Utils', () => {
4646
});
4747

4848
it('should change the global context manager', () => {
49-
const original = api1.context['_getContextManager']();
49+
const original = api1.context['getContextManager']();
5050
const newContextManager = new NoopContextManager();
5151
api1.context.setGlobalContextManager(newContextManager);
52-
assert.notStrictEqual(api1.context['_getContextManager'](), original);
53-
assert.strictEqual(api1.context['_getContextManager'](), newContextManager);
52+
assert.notStrictEqual(api1.context['getContextManager'](), original);
53+
assert.strictEqual(api1.context['getContextManager'](), newContextManager);
5454
});
5555

5656
it('should load an instance from one which was set in the other', () => {
5757
api1.context.setGlobalContextManager(new NoopContextManager());
5858
assert.strictEqual(
59-
api1.context['_getContextManager'](),
60-
api2.context['_getContextManager']()
59+
api1.context['getContextManager'](),
60+
api2.context['getContextManager']()
6161
);
6262
});
6363

6464
it('should disable both if one is disabled', () => {
65-
const original = api1.context['_getContextManager']();
65+
const original = api1.context['getContextManager']();
6666

6767
api1.context.setGlobalContextManager(new NoopContextManager());
6868

69-
assert.notStrictEqual(original, api1.context['_getContextManager']());
69+
assert.notStrictEqual(original, api1.context['getContextManager']());
7070
api2.context.disable();
71-
assert.strictEqual(original, api1.context['_getContextManager']());
71+
assert.strictEqual(original, api1.context['getContextManager']());
7272
});
7373

7474
it('should return the module NoOp implementation if the version is a mismatch', () => {
75-
const original = api1.context['_getContextManager']();
75+
const original = api1.context['getContextManager']();
7676
api1.context.setGlobalContextManager(new NoopContextManager());
7777
const afterSet = _global[GLOBAL_CONTEXT_MANAGER_API_KEY]!(-1);
7878

packages/opentelemetry-node/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ provider.register()
9292
## Examples
9393
See how to automatically instrument [http](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/http) and [gRPC](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/grpc) using node-sdk.
9494

95+
## Experimental API methods
96+
97+
#### withSpanAsync
98+
99+
The `NodeTracer` expose a method called `withSpanAsync` that allows you to set a current span in the context of a `async` function. This previously was a problem with `withSpan` because its synchronous behavior, read about that in [this issue](https://github.com/open-telemetry/opentelemetry-js/issues/752).
100+
This method have two drawback:
101+
- strict requirement of the `AsyncHooksScopeManager`, you **cannot** use it without.
102+
- the underlying implementation inside `AsyncHooksScopeManager` is experimental, so is this method, please open an issue if you have a problem with it.
95103

96104
## Useful links
97105
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>

packages/opentelemetry-node/src/NodeTracer.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,17 @@ export class NodeTracer extends Tracer {
5454
T extends Promise<any>,
5555
U extends (...args: unknown[]) => T
5656
>(span: api.Span, fn: U): Promise<T> {
57-
// @ts-ignore
58-
const contextManager = api.context._getContextManager();
57+
const contextManager = api.context.getContextManager();
5958
if (contextManager instanceof AsyncHooksContextManager) {
6059
return contextManager.withAsync(
6160
setActiveSpan(api.context.active(), span),
6261
fn
6362
);
6463
} else {
6564
this.logger.warn(
66-
`Using withAsync without AsyncHookContextManager doesn't work, please refer to`
65+
`Using withAsync without AsyncHookContextManager doesn't work, please refer to https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node#withspanasync`
6766
);
68-
return fn();
67+
return await fn();
6968
}
7069
}
7170
}

0 commit comments

Comments
 (0)