-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathCLSContext.js
More file actions
53 lines (46 loc) · 1.41 KB
/
CLSContext.js
File metadata and controls
53 lines (46 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2020 The OpenZipkin Authors; licensed to You under the Apache License, Version 2.0.
const cls = require('continuation-local-storage');
const clsHooked = require('cls-hooked');
module.exports = class CLSContext {
constructor(namespace = 'zipkin', supportAsyncAwait = false) {
if (supportAsyncAwait) {
this._session = clsHooked.getNamespace(namespace) || clsHooked.createNamespace(namespace);
} else {
this._session = cls.getNamespace(namespace) || cls.createNamespace(namespace);
}
const defaultContext = this._session.createContext();
this._session.enter(defaultContext);
}
setContext(ctx) {
this._session.set('zipkin', ctx);
}
getContext() {
const currentCtx = this._session.get('zipkin');
if (currentCtx != null) {
return currentCtx;
} else {
return null; // explicitly return null (not undefined)
}
}
scoped(callable) {
let result;
this._session.run(() => {
result = callable();
});
return result;
}
letContext(ctx, callable) {
return this.scoped(() => {
this.setContext(ctx);
return callable();
});
}
// _bindEmitter is exposed but not meant to be used.
// It was introduced in the aim to test the async/await support
// for CLSContext.
// See https://github.com/openzipkin/zipkin-js/pull/499 for more
// context
_bindEmitter(emitter) {
this._session.bindEmitter(emitter);
}
};