Skip to content

Instantly share code, notes, and snippets.

@lloydmeta
Last active December 17, 2015 03:48
Show Gist options
  • Save lloydmeta/5545466 to your computer and use it in GitHub Desktop.
Save lloydmeta/5545466 to your computer and use it in GitHub Desktop.
Javascript API function for RockMongo to find slowish endpoints based on nginx access logs gathered from instances. Now sorts endpoints based on number of results in descending order ! Doesn't care about individual query params
// Contains questionable Javascript and Regex. You have been warned.
function () {
var counting_object = {
count_hash : {},
clean_endpoint : function(endpoint) {
return endpoint.replace(/(\/[\w]+\/[\d]+)/, function(v){return v.replace(/\/[\d]+/, "/:id")})
.replace(/\?.* ->/,'{some_query_params} ->')
},
increment_for_endpoint : function(endpoint){
var that = this,
clean_endpoint = that.clean_endpoint(endpoint);
if (that.count_hash[clean_endpoint] && that.count_hash[clean_endpoint] >= 1){
that.count_hash[clean_endpoint] = that.count_hash[clean_endpoint] + 1;
}else{
that.count_hash[clean_endpoint] = 1;
}
return that.count_hash[clean_endpoint];
},
sorted_results : function(){
var that = this,
sortable = [];
for (var count_key in that.count_hash) {
sortable.push([count_key, that.count_hash[count_key]])
};
sortable.sort(function(a,b){return b[1] - a[1]});
return sortable;
}
};
var cur = db.access.find(
{
"upstream_request_time":{
$gte: 0.3
}
}
);
var out = [];
cur.forEach(function(x){
counting_object.increment_for_endpoint((x['path']+' -> '+x['method']))});
return(counting_object.sorted_results());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment