NodeJS Agent类似于Java代理

时间:2017-09-27 17:46:18

标签: node.js

我正在尝试创建类似于java代理的节点代理来收集运行nodejs应用程序的性能

我找到了appmetrics npm包,它工作正常,如果我将它包含在exisitng nodejs脚本中,但我试图在不修改现有nodejs脚本的情况下作为代理运行

示例解释了我的问题,该问题可以正常使用

var http = require("http");
var appmetrics = require('appmetrics');
var monitoring = appmetrics.monitor();

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8083);

// Console will print the message
console.log('Server running at http://127.0.0.1:8083/');

monitoring.on('initialized', function (env) {
    env = monitoring.getEnvironment();
    for (var entry in env) {
        console.log(entry + ':' + env[entry]);
    };
});

monitoring.on('cpu', function (cpu) {
    console.log('[' + new Date(cpu.time) + '] CPU: ' + cpu.process);
});

问题是单独appmetric和nodejs hello world脚本分开并一起运行

helloworld.js

var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8083);

// Console will print the message
console.log('Server running at http://127.0.0.1:8083/');

monitor.js

var appmetrics = require('appmetrics');
var monitoring = appmetrics.monitor();

monitoring.on('initialized', function (env) {
    env = monitoring.getEnvironment();
    for (var entry in env) {
        console.log(entry + ':' + env[entry]);
    };
});

monitoring.on('cpu', function (cpu) {
    console.log('[' + new Date(cpu.time) + '] CPU: ' + cpu.process);
});

尝试一次运行上面提到的两个脚本 节点不工作

节点helloworld.js&& node monitor.js

0 个答案:

没有答案