Last active
March 27, 2018 03:30
-
-
Save luk3thomas/7cfe7370a27e555610e9 to your computer and use it in GitHub Desktop.
Send chrome memory usage stats to librato
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
// 1. Go to chrome://memory-redirect/ | |
// 2. Paste this script into the developer console | |
(function(){ | |
var SERVER = 'http://localhost:4567/', // Post metrics to any server | |
STRINGS = 'chrome://memory-redirect/strings.js', | |
INTERVAL = 10e3, | |
RE_INCLUDE = /./; // only include certain results, if you want. | |
function getMemoryStats() { | |
console.log('Getting stats.') | |
xhr({ method: 'GET', url: STRINGS, callback: sendMemoryStats }) | |
} | |
function sendMemoryStats(req){ | |
eval(req.responseText.replace(/loadTimeData./, '')) | |
xhr({ method: 'POST', url: SERVER, data: formatData(data) }) | |
} | |
function xhr(opts){ | |
var req, type, url, callback; | |
method = opts.method; | |
url = opts.url; | |
callback = opts.callback || function(){}; | |
data = opts.data; | |
req = new XMLHttpRequest() | |
req.open(method, url, true) | |
req.onreadystatechange = function(){ | |
if (req.readyState === 4 && req.status === 200) { | |
callback(req) | |
} | |
} | |
req.send(data) | |
} | |
function formatData(data){ | |
return JSON.stringify({ | |
browsers: data.jstemplateData.browsers.map(function(browser){ | |
memory = getMemory(browser); | |
memory.source = formatSource(browser.name); | |
return memory; | |
}), | |
processes: data.jstemplateData.child_data | |
.map(function(item){ | |
memory = getMemory(item); | |
memory.source = formatSource(item.child_name, item.titles.slice(-1)[0]); | |
return memory; | |
}) | |
.filter(function(item){ | |
return RE_INCLUDE.test(item.source) | |
}) | |
}); | |
} | |
function getMemory(measurement) { | |
return { | |
virtual: measurement.comm_priv * 1000, | |
private: measurement.ws_priv * 1000 | |
}; | |
} | |
function formatSource() { | |
var args = [].slice.call(arguments); | |
return args.join(" ") | |
.toLowerCase() | |
.replace(/[^a-z\- ]/g, ' ') | |
.replace(/ [ ]*/g, ' ') | |
.replace(/ /g, '.') | |
.replace(/\.$/g, '') | |
} | |
console.info('Start collecting memory stats...') | |
getMemoryStats() | |
setInterval(getMemoryStats, INTERVAL) | |
})() |
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
require 'sinatra' | |
require 'json' | |
require 'librato/metrics' | |
Librato::Metrics.authenticate ENV['email'], ENV['token'] | |
post '/' do | |
queue = Librato::Metrics::Queue.new | |
headers \ | |
"Access-Control-Allow-Credentials" => "true", | |
"Access-Control-Allow-Headers" => "*", | |
"Access-Control-Allow-Methods" => "*", | |
"Access-Control-Allow-Origin" => "*" | |
req = JSON.parse(request.body.read) | |
req['processes'].each do |item| | |
queue.add 'chrome.process.memory.private' => { | |
value: item['private'], | |
source: item['source'] | |
} | |
queue.add 'chrome.process.memory.virtual' => { | |
value: item['virtual'], | |
source: item['source'] | |
} | |
end | |
req['browsers'].each do |item| | |
queue.add 'chrome.memory.private' => { | |
value: item['private'], | |
source: item['source'] | |
} | |
queue.add 'chrome.memory.virtual' => { | |
value: item['virtual'], | |
source: item['source'] | |
} | |
end | |
queue.submit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment