Skip to content

Commit 2ac44ad

Browse files
authored
refactor(context-zone-peer-dep): remove unnecessary helper methods and use meaningful zone names (#6452)
1 parent 600e51d commit 2ac44ad

File tree

2 files changed

+8
-47
lines changed

2 files changed

+8
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
127127

128128
### :house: Internal
129129

130+
* refactor(context-zone-peer-dep): remove unnecessary helper methods and use meaningful zone names[#6452](https://github.com/open-telemetry/opentelemetry-js/pull/6452) @dyladan
130131
* chore: enable tsconfig isolatedModules [#5697](https://github.com/open-telemetry/opentelemetry-js/pull/5697) @legendecas
131132

132133
## 2.0.1

packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ export class ZoneContextManager implements ContextManager {
2626
*/
2727
private _enabled = false;
2828

29-
/**
30-
* Helps to create a unique name for the zones - part of zone name
31-
*/
32-
private _zoneCounter = 0;
33-
34-
/**
35-
* Returns the active context from certain zone name
36-
* @param activeZone
37-
*/
38-
private _activeContextFromZone(activeZone: Zone | undefined): Context {
39-
return (activeZone && activeZone.get(ZONE_CONTEXT_KEY)) || ROOT_CONTEXT;
40-
}
41-
4229
/**
4330
* @param context A context (span) to be executed within target function
4431
* @param target Function to be executed within the context
@@ -87,15 +74,6 @@ export class ZoneContextManager implements ContextManager {
8774
return obj;
8875
}
8976

90-
/**
91-
* Creates a new unique zone name
92-
*/
93-
private _createZoneName() {
94-
this._zoneCounter++;
95-
const random = Math.random();
96-
return `${this._zoneCounter}-${random}`;
97-
}
98-
9977
/**
10078
* Creates a new zone
10179
* @param zoneName zone name
@@ -110,13 +88,6 @@ export class ZoneContextManager implements ContextManager {
11088
});
11189
}
11290

113-
/**
114-
* Returns the active zone
115-
*/
116-
private _getActiveZone(): Zone | undefined {
117-
return Zone.current;
118-
}
119-
12091
/**
12192
* Patches addEventListener method
12293
* @param target any target that has "addEventListener" method
@@ -173,17 +144,10 @@ export class ZoneContextManager implements ContextManager {
173144
* Returns the active context
174145
*/
175146
active(): Context {
176-
if (!this._enabled) {
147+
if (!this._enabled || !Zone.current) {
177148
return ROOT_CONTEXT;
178149
}
179-
const activeZone = this._getActiveZone();
180-
181-
const active = this._activeContextFromZone(activeZone);
182-
if (active) {
183-
return active;
184-
}
185-
186-
return ROOT_CONTEXT;
150+
return Zone.current.get(ZONE_CONTEXT_KEY) || ROOT_CONTEXT;
187151
}
188152

189153
/**
@@ -193,10 +157,6 @@ export class ZoneContextManager implements ContextManager {
193157
* the provided context will be used as the active context for the duration of the call.
194158
*/
195159
bind<T>(context: Context, target: T | TargetWithEvents): T {
196-
// if no specific context to propagate is given, we use the current one
197-
if (context === undefined) {
198-
context = this.active();
199-
}
200160
if (typeof target === 'function') {
201161
return this._bindFunction(context, target);
202162
} else if (isListenerObject(target)) {
@@ -236,10 +196,10 @@ export class ZoneContextManager implements ContextManager {
236196
thisArg?: ThisParameterType<F>,
237197
...args: A
238198
): ReturnType<F> {
239-
const zoneName = this._createZoneName();
240-
241-
const newZone = this._createZone(zoneName, context);
242-
243-
return newZone.run(fn, thisArg, args);
199+
let zoneName = 'otel:with';
200+
if (fn.name) {
201+
zoneName += `:${fn.name}`;
202+
}
203+
return this._createZone(zoneName, context).run(fn, thisArg, args);
244204
}
245205
}

0 commit comments

Comments
 (0)