(stackparser/cache) harden test + fixes

This commit is contained in:
Unitech 2017-03-29 19:04:57 +02:00
parent 2578ab6f8c
commit a5bbf25ddc
7 changed files with 213 additions and 17 deletions

View File

@ -624,7 +624,10 @@ var TransactionAggregator = module.exports = function (pushInteractor) {
spans.forEach(function (span) {
// if empty make sure that it doesnt exist
if (!span.labels.stacktrace || typeof(span.labels.stacktrace) !== 'string')
if (!span ||
!span.labels ||
!span.labels.stacktrace ||
typeof(span.labels.stacktrace) !== 'string')
return;
// you never know what come through that door
@ -644,6 +647,8 @@ var TransactionAggregator = module.exports = function (pushInteractor) {
});
spans.forEach(function (span) {
if (!span || !span.labels)
return;
delete span.labels.stacktrace;
})
}

View File

@ -61,9 +61,11 @@ Cache.prototype.get = function (key) {
var value = this._cache[key]
if (value) return value
var value = this._miss(key)
value = this._miss(key)
if (value)
this.set(key, value)
return value
}
@ -100,12 +102,20 @@ StackTraceParser.prototype.parse = function (stack) {
for (var i = 0, len = stack.length; i < len; i++) {
var callsite = stack[i];
// avoid null values
if (!callsite.file_name || !callsite.line_number) continue;
if (!callsite ||
!callsite.file_name ||
!callsite.line_number)
continue;
var type = (!path.isAbsolute(callsite.file_name) && callsite.file_name[0] !== '.') ? 'core' : 'user'
// only use the callsite if its inside user space
if (!callsite || type === 'core' || callsite.file_name.indexOf('node_modules') > -1 || callsite.file_name.indexOf('vxx') > -1)
if (!callsite ||
type === 'core' ||
callsite.file_name.indexOf('node_modules') > -1 ||
callsite.file_name.indexOf('vxx') > -1)
continue;
// get the whole context (all lines) and cache them if necessary

View File

@ -189,17 +189,4 @@ describe('Transactions Aggregator', function() {
});
});
describe('.parseStacktrace', function() {
it('should parse stacktrace and get context', function(done) {
var obj = [{
labels: {
stacktrace: JSON.stringify(TraceFactory.stacktrace)
} }]
aggregator.parseStacktrace(obj);
obj[0].labels['source/file'].indexOf('test/interface/misc/trace_factory.js:10').should.be.above(0);
should(obj[0].labels['source/context']).eql("var random_routes = [\n '/api/bucket',\n>>'/api/bucket/users',\n '/api/bucket/chameau',\n '/backo/testo'")
done();
});
});
});

View File

@ -0,0 +1,61 @@
var should = require('should');
var Utility = require('../../lib/Interactor/Utility.js');
var path = require('path');
var fs = require('fs');
describe('Cache Utility', function() {
var aggregator;
var stackParser;
var cache;
it('should instanciate context cache', function() {
cache = new Utility.Cache({
miss: function (key) {
try {
var content = fs.readFileSync(path.resolve(key));
return content.toString().split(/\r?\n/);
} catch (err) {
return null;
}
}
})
});
it('should get null without key', function() {
should(cache.get()).be.null();
});
it('should get null with unknow value', function() {
should(cache.get('toto')).be.null();
});
it('should get null', function() {
should(cache.get()).be.null();
});
it('should set null', function() {
should(cache.set()).be.false();
});
it('should not set key without value', function() {
should(cache.set('toto')).be.false();
});
it('should set value', function() {
should(cache.set('toto', 'val')).be.true();
});
it('should get value', function() {
should(cache.get('toto')).eql('val');
});
it('should reset', function() {
cache.reset();
});
it('should get null with unknow value', function() {
should(cache.get('toto')).be.null();
});
});

View File

@ -140,6 +140,7 @@ exports.stacktrace = {
]
}
if (require.main === module) {
console.log(generateTrace());
}

View File

@ -0,0 +1,128 @@
var should = require('should');
var Aggregator = require('../../lib/Interactor/TransactionAggregator.js');
var Utility = require('../../lib/Interactor/Utility.js');
var TraceFactory = require('./misc/trace_factory.js');
var path = require('path');
var fs = require('fs');
describe('StackTrace Utility', function() {
var aggregator;
var stackParser;
it('should instanciate context cache', function() {
var cache = new Utility.Cache({
miss: function (key) {
try {
var content = fs.readFileSync(path.resolve(key));
return content.toString().split(/\r?\n/);
} catch (err) {
return undefined;
}
}
})
stackParser = new Utility.StackTraceParser({ cache: cache, context: 2});
});
it('should instanciate aggregator', function() {
aggregator = new Aggregator({ stackParser: stackParser});
});
describe('.parseStacktrace', function() {
it('should parse stacktrace and get context', function(done) {
var obj = [{
labels: {
stacktrace: JSON.stringify(TraceFactory.stacktrace)
}
}];
aggregator.parseStacktrace(obj);
obj[0].labels['source/file'].indexOf('test/interface/misc/trace_factory.js:10').should.be.above(0);
should(obj[0].labels['source/context']).eql("var random_routes = [\n '/api/bucket',\n>>'/api/bucket/users',\n '/api/bucket/chameau',\n '/backo/testo'");
done();
});
it('should handle malformated stacktraces', function() {
aggregator.parseStacktrace([{
labels: {
stacktrace: JSON.stringify({
stack_frame: [{
line_number: 10,
column_number: 10,
method_name: '<anonymous function>'
}, {
file_name: 'node_modules/express.js',
column_number: 10,
method_name: '<anonymous function>'
}, {
file_name: path.resolve(__dirname, 'trace_factory.js'),
line_number: 10,
column_number: 10,
method_name: '<anonymous function>'
}]
})
}
}]);
});
it('should handle malformated stacktrace v1', function() {
aggregator.parseStacktrace([{
labels: {
stacktrace: JSON.stringify({
stack_frame: [{
file_name: 'events.js'
},{
file_name: 'node_modules/express.js'
},{
file_name: path.resolve(__dirname, 'trace_factory.js')
}]
})
}
}]);
});
it('should handle malformated stacktrace v2', function() {
aggregator.parseStacktrace([{
labels: {
stacktrace: JSON.stringify({
stack_frame: [{
file_name: 'events.js',
column_number: 10,
method_name: '<anonymous function>'
},{
file_name: 'node_modules/express.js',
column_number: 10,
method_name: '<anonymous function>'
},{
file_name: path.resolve(__dirname, 'trace_factory.js'),
line_number: 10,
column_number: 10,
method_name: '<anonymous function>'
}]
})
}
}]);
});
it('should handle malformated stacktrace v3', function() {
aggregator.parseStacktrace([{
labels: {}
}]);
});
it('should handle malformated stacktrace v4', function() {
aggregator.parseStacktrace([{
}]);
});
it('should handle malformated stacktrace v5', function() {
aggregator.parseStacktrace([]);
});
it('should handle malformated stacktrace v5', function() {
aggregator.parseStacktrace();
});
});
});

View File

@ -105,5 +105,9 @@ mocha --opts ./mocha.opts ./request.mocha.js
spec "Protocol communication test"
mocha --opts ./mocha.opts ./aggregator.mocha.js
spec "Transaction trace aggregator test"
mocha --opts ./mocha.opts ./stacktrace.mocha.js
spec "Stacktrace Utility"
mocha --opts ./mocha.opts ./cache.mocha.js
spec "Cache Utility"
mocha --opts ./mocha.opts ./pm2.link.check.mocha.js
spec "Transaction option enablement"