Skip to content

Instantly share code, notes, and snippets.

@camous
Last active June 1, 2018 08:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camous/0194d0c921e19b7e27df9a0acc2ba6dd to your computer and use it in GitHub Desktop.
Save camous/0194d0c921e19b7e27df9a0acc2ba6dd to your computer and use it in GitHub Desktop.
redirect azure log stream to another web service endpoint
var request = require('request');
var http = require('http');
var express = require('express');
var os = require("os");
var streamoptions = {
url: 'https://xxxxxxx.scm.azurewebsites.net/api/logstream',
headers: {
'Authorization': 'Basic xxxxxxxxxxxxxxxxx'
}
};
// query Azure stream and explicitly pipe the response on stdout
function loadStream(){
if(stream != null){
console.warn('stream not empty');
stream.abort();
}
stream = request.get(streamoptions)
.on('response', function(response) {
});
stream.pipe(process.stdout);
}
loadStream();
var stream = null;
app.get('/stream', function (req,res){
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('data: connecting to ' + os.hostname() + ' ...\n\n');
// on any stdout write event, send text as response to express
process.stdout.write = (function(write) {
return function(result, encoding, fileDescriptor) {
res.write('data: ' + result + '\n\n');
if(result.toString().indexOf('Stream terminated due to timeout 30 min(s)') != -1){
console.warn('timeout detected');
loadStream();
}
write.apply(process.stdout, arguments);
};
})(process.stdout.write);
}
);
process.on('uncaughtException', function(err){
console.error('uncaughtException: ' + err.message);
setTimeout(loadStream, 60000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment