简体   繁体   English

如何在NodeJ中使用AWS X-Ray?

[英]How to use AWS X-Ray in NodeJs?

I'm trying to use aws-xray-sdk to get the traces of my App, 我正在尝试使用aws-xray-sdk来获取我的应用程序的踪迹,
I have written three APIs, 我已经编写了三个API,
1. Making a call to an AWS S3 service ( /traceS3 ) 1.调用AWS S3服务( / traceS3
2. Making a HTTP call to external endpoint ie. 2.对外部端点进行HTTP调用,即 google.com ( /traceHttp ) google.com( / traceHttp
3. Making a DB call to a locally installed MySql DB ( /traceMysql ) 3.对本地安装的MySql DB( / traceMysql )进行数据库调用

Also installed the AWS X-Ray Daemon locally 还在本地安装了AWS X-Ray守护程序
And made AWS configurations using the aws-cli and then ran the daemon using the following command (I'm using Ubuntu) - 并使用aws-cli进行AWS配置,然后使用以下命令运行守护程序(我正在使用Ubuntu)-

/usr/bin/xray -o -n ap-southeast-2

I am referring the following resources - 我指的是以下资源-
1. aws x-ray tracing breaks on outgoing requests in Node.js 1. AWS X射线跟踪在Node.js中的传出请求上中断
2. https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-httpclients.html 2. https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-httpclients.html

The Daemon runs successfully but, doesn't send data for all the APIs, 守护程序可以成功运行,但是不会发送所有API的数据,
It only sends the data for the /traceS3 and /traceMysql APIs. 它仅发送/traceS3/traceMysql API的数据。

Following is the running code - 以下是运行代码-

var AWSXRay = require('aws-xray-sdk');
var AWS = AWSXRay.captureAWS(require('aws-sdk'));
var mysql = AWSXRay.captureMySQL(require('mysql'));

var express = require('express');
var app = express();

var http = AWSXRay.captureHTTPs(require('http'));
app.use(AWSXRay.express.openSegment('X-Ray-Node-Example'));

AWS.config.update({region: 'ap-southeast-2'});
s3 = new AWS.S3({apiVersion: '2006-03-01'});

var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "root",
    database: "nodejs",
    port: 3306
});

con.connect(function (err) {
    if (err) throw err

});

app.get('/traceS3', function(req,res){

    // Call S3 to list current buckets
    s3.listBuckets(function(err, data) {
        if (err) {
            console.log("Error", err);
            res.send(err);
        } else {
            res.send(data.Buckets);
        }
    });
});

app.get('/traceHttp', function (req, res) {
    console.log("traceHttp called!\n"+req);

    http.get("http://jsonplaceholder.typicode.com/todos/1", (resp) => {
        console.log(resp);
        res.send("googlefetched")
    });
});

app.get('/traceMysql', function (req, res) {
    console.log("traceMysql called!\n"+req);

    var start = Date.now();

    var query = con.query('SELECT * FROM customer', function (err, rows, fields) {
        if (err) {
            console.log("Error Selecting : %s ", err);
        }
        else {
            var resp = [];
            function Person(id, name) {
                this.id = id;
                this.name = name;
            }

            for (var i = 0; i < rows.length; i++) {
                var id = rows[i].id;
                var name = rows[i].name;
                resp.push(new Person(id, name));
            }
            var end = Date.now();
            res.send(JSON.stringify({ "status": 200, "error": null, "response": resp, "requestTime": end - start }));
        }
    });
});

app.use(AWSXRay.express.closeSegment());
var server = app.listen(8085, function () {
    var host = server.address().address
    var port = server.address().port
    console.log("Example app listening at http://%s:%s", host, port)
});

Can someone help fix the code? 有人可以帮忙修复代码吗?

The symptoms seem similar to those of a bug that was reportedly fixed in v1.2.0 症状似乎类似于据报道已在v1.2.0中修复的错误的症状。

HTTP Patcher leaks response & stalls segments if no other response listener is supplied 如果未提供其他响应侦听器,则HTTP Patcher泄漏响应并停止段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM