Solutions:为Nodejs微服务提供APM功能 - Go语言中文社区

Solutions:为Nodejs微服务提供APM功能


在今天的这篇文章中,我来简单地介绍如何为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的调用情况。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/UbuntuTouch/article/details/105510200
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-05-07 23:09:58
  • 阅读 ( 1479 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢