社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
在今天的这篇文章中,我来简单地介绍如何为Nodejs应用实现APM。关于如何安装Elasticsearch, Kibana及APM server,请参阅我之前的文章“应用程序性能监控/管理(APM)实践”。在这篇文章中,我们来简单介绍一下需注意的一些地方。
我们在进行下面的工作之前,先启动Elasticsearch, Kibana及APM server。
我们打开Kibana:
点击上面的Add APM按钮:
我们在APM agents里找到Nodejs,并安装上面的要求进行安装和配置。如果你还没有自己的nodejs应用的话,请参考我一个示例的 nodejs应用:
git clone https://github.com/liu-xiao-guo/apm-zipcode-microservice
这是一个很简单的测量两个zipcode直接的距离的一个微服务应用。我们可以在terminal中打入如下的命令来进行安装及运行:
npm install
npm start
我们可以在浏览器中输入如下的地址:
http://localhost:3000/distance/84010/97229
我们将看到如下的输出:
{"distance":638.174}
我们在浏览器地址中输入如下的地址:
http://localhost:3000/distance/84010/92001
我们可以看到如下的输出:
{"distance":-1}
我们现在看一下nodejs的第一个文件index.js:
index.js
// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
// Override service name from package.json
// Allowed characters: a-z, A-Z, 0-9, -, _, and space
serviceName: 'zipcode service',
// Use if APM Server requires a token
secretToken: '123456',
// Set custom APM Server URL (default: http://localhost:8200)
serverUrl: 'http://localhost:8200'
})
var express = require('express')
var app = express();
var port = process.env.PORT || 3000;
var routes = require('./api/routes');
routes(app);
app.listen(port, function() {
console.log('Server started on port: ' + port);
});
我们可以注意这个文件的最上面的部分:
// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
// Override service name from package.json
// Allowed characters: a-z, A-Z, 0-9, -, _, and space
serviceName: 'zipcode service',
// Use if APM Server requires a token
secretToken: '123456',
// Set custom APM Server URL (default: http://localhost:8200)
serverUrl: 'http://localhost:8200'
})
这个部分其实就是要求我们拷贝过来并修改的部分。
我们切换到Kibana的APM应用界面:
点击上面的zipcode service:
点击GET /distance/:zipcode1/:zipcode2链接:
这样我们就可以看到整个API的调用情况。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!