UNPKG

1.55 kBJavaScriptView Raw
1/**
2 * Copyright 2015 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17'use strict';
18
19
20var cls = require('continuation-local-storage');
21
22/** @const {string} */
23var TRACE_NAMESPACE = 'com.google.cloud.trace';
24
25function createNamespace() {
26 return cls.createNamespace(TRACE_NAMESPACE);
27}
28
29function destroyNamespace() {
30 cls.destroyNamespace(TRACE_NAMESPACE);
31}
32
33function getNamespace() {
34 return cls.getNamespace(TRACE_NAMESPACE);
35}
36
37module.exports = {
38 createNamespace: createNamespace,
39
40 destroyNamespace: destroyNamespace,
41
42 getNamespace: getNamespace,
43
44 getRootContext: function getRootContext() {
45 // First getNamespace check is necessary in case any
46 // patched closures escaped before the agent was stopped and the
47 // namespace was destroyed.
48 if (getNamespace() && getNamespace().get('root')) {
49 return getNamespace().get('root');
50 }
51 return null;
52 },
53
54 setRootContext: function setRootContext(rootContext) {
55 getNamespace().set('root', rootContext);
56 }
57};
58