I'm trying to use aws-xray-sdk to get the traces of my App,
I have written three APIs,
1. Making a call to an AWS S3 service (/traceS3)
2. Making a HTTP call to external endpoint ie. google.com (/traceHttp)
3. Making a DB call to a locally installed MySql DB (/traceMysql)
Also installed the AWS X-Ray Daemon locally
And made AWS configurations using the aws-cli
and then ran the daemon using the following command (I'm using 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
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,
It only sends the data for the /traceS3
and /traceMysql
APIs.
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?