-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Async hooks based context tracking (#538)
Introduces a lower overhead, more correct context tracking mechanism based on async hooks that is available when using the trace agent on newer versions of node (>=8). To enable async hooks based context, set the GCLOUD_TRACE_NEW_CONTEXT environment variable. This change is not expected to have observable behavior difference but due to the nature of context propagation it is difficult to say for sure without real world testing. PR-URL: #538
- Loading branch information
1 parent
debc493
commit 87e4254
Showing
12 changed files
with
279 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ machine: | |
- redis | ||
environment: | ||
GCLOUD_PROJECT: 0 | ||
GCLOUD_TRACE_NEW_CONTEXT: 1 |
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,113 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const asyncHook = require('async_hooks'); | ||
|
||
const wrappedSymbol = Symbol('context_wrapped'); | ||
let contexts = {}; | ||
let current = {}; | ||
|
||
asyncHook.createHook({init, before, destroy}).enable(); | ||
|
||
function Namespace() {} | ||
|
||
Namespace.prototype.get = function(k) { | ||
return current[k]; | ||
}; | ||
|
||
Namespace.prototype.set = function(k, v) { | ||
current[k] = v; | ||
}; | ||
|
||
Namespace.prototype.run = function(fn) { | ||
const oldContext = current; | ||
current = {}; | ||
const res = fn(); | ||
current = oldContext; | ||
return res; | ||
}; | ||
|
||
Namespace.prototype.runAndReturn = Namespace.prototype.run; | ||
|
||
Namespace.prototype.bind = function(cb) { | ||
if (cb[wrappedSymbol] || !current) { | ||
return cb; | ||
} | ||
const boundContext = current; | ||
const contextWrapper = function() { | ||
const oldContext = current; | ||
current = boundContext; | ||
const res = cb.apply(this, arguments); | ||
current = oldContext; | ||
return res; | ||
}; | ||
contextWrapper[wrappedSymbol] = true; | ||
Object.defineProperty(contextWrapper, 'length', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: false, | ||
value: cb.length | ||
}); | ||
return contextWrapper; | ||
}; | ||
|
||
const eventEmitterMethods = | ||
[ 'addListener', 'on', 'once', 'prependListener', 'prependOncelistener' ]; | ||
|
||
// This function is not technically needed and all tests currently pass without it | ||
// (after removing call sites). While it is not a complete solution, restoring | ||
// correct context before running every request/response event handler reduces | ||
// the number of situations in which userspace queuing will cause us to lose context. | ||
Namespace.prototype.bindEmitter = function(ee) { | ||
var ns = this; | ||
eventEmitterMethods.forEach(function(method) { | ||
const oldMethod = ee[method]; | ||
ee[method] = function(e, f) { return oldMethod.call(this, e, ns.bind(f)); }; | ||
}); | ||
}; | ||
|
||
var namespace = new Namespace(); | ||
|
||
// AsyncWrap Hooks | ||
|
||
function init(uid, provider, parentUid, parentHandle) { | ||
contexts[uid] = current; | ||
} | ||
|
||
function before(uid) { | ||
if (contexts[uid]) { | ||
current = contexts[uid]; | ||
} | ||
} | ||
|
||
function destroy(uid) { | ||
delete contexts[uid]; | ||
} | ||
|
||
module.exports = { | ||
createNamespace: function() { | ||
return namespace; | ||
}, | ||
|
||
destroyNamespace: function() { | ||
current = {}; | ||
contexts = {}; | ||
}, | ||
|
||
getNamespace: function() { return namespace; } | ||
}; |
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
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,102 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var http = require('http'); | ||
var semver = require('semver'); | ||
|
||
if (semver.satisfies(process.version, '<8')) { | ||
console.log('Skipping cls-ah tests on node version without async hooks'); | ||
return; | ||
} else { | ||
assert.ok(process.env.GCLOUD_TRACE_NEW_CONTEXT); | ||
} | ||
|
||
var cls = require('../src/cls-ah.js'); | ||
|
||
describe('test-cls-ah', function() { | ||
it('should preserve request context', function(done) { | ||
var namespace = cls.createNamespace(); | ||
var id = 0; | ||
var ended = 0; | ||
var server = http.createServer(function(req, res) { | ||
var reqId = id++; | ||
namespace.run(function() { | ||
namespace.set('id', reqId); | ||
assert.equal(namespace.get('id'), reqId); | ||
var count = 0; | ||
var i = setInterval(function () { | ||
assert.equal(namespace.get('id'), reqId); | ||
if (count++ > reqId) { | ||
clearInterval(i); | ||
res.end('yay'); | ||
if (++ended === 10) { | ||
done(); | ||
} | ||
} | ||
}, Math.random() * 50); | ||
}); | ||
}); | ||
server.listen(8080, function() { | ||
for (var i = 0; i < 10; i++) { | ||
http.get('http://localhost:8080'); | ||
} | ||
}); | ||
}); | ||
|
||
it('should correctly run context in series', function(done) { | ||
var namespace = cls.createNamespace(); | ||
namespace.run(function() { | ||
assert.equal(namespace.get('id'), null); | ||
namespace.set('id', 'first'); | ||
setTimeout(function() { | ||
assert.equal(namespace.get('id'), 'first'); | ||
done(); | ||
}, 30); | ||
assert.equal(namespace.get('id'), 'first'); | ||
}); | ||
namespace.run(function() { | ||
assert.equal(namespace.get('id'), null); | ||
namespace.set('id', 'second'); | ||
setTimeout(function() { | ||
assert.equal(namespace.get('id'), 'second'); | ||
}, 10); | ||
assert.equal(namespace.get('id'), 'second'); | ||
}); | ||
}); | ||
|
||
it('should correctly run context nested', function(done) { | ||
var namespace = cls.createNamespace(); | ||
namespace.run(function() { | ||
assert.equal(namespace.get('id'), null); | ||
namespace.set('id', 'first'); | ||
namespace.run(function() { | ||
assert.equal(namespace.get('id'), null); | ||
namespace.set('id', 'second'); | ||
setTimeout(function() { | ||
assert.equal(namespace.get('id'), 'second'); | ||
}, 10); | ||
assert.equal(namespace.get('id'), 'second'); | ||
}); | ||
setTimeout(function() { | ||
assert.equal(namespace.get('id'), 'first'); | ||
done(); | ||
}, 30); | ||
assert.equal(namespace.get('id'), 'first'); | ||
}); | ||
}); | ||
}); |
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.