aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.
Apache License 2.0
267 stars 156 forks source link

XRay doesn't work with axios #582

Closed PolestarTommy closed 1 year ago

PolestarTommy commented 1 year ago

Hi X-Ray team

I want to use X-ray in our service, I try to capture the http/https/node-fetch packages, it works, but when I try to use the axios package, it doesn't work, I wonder why

Here is a sample

import AWSXRay = require('aws-xray-sdk');

import Express = require("express");

AWSXRay.captureHTTPsGlobal(require('http'));
AWSXRay.captureHTTPsGlobal(require('https'));
AWSXRay.capturePromise();
var http = require('http');
const fetch = require('node-fetch');
var https = require('https');
const axios = require("axios")

const app = Express()
app.use(AWSXRay.express.openSegment('MyApp'));
const xRayIdMiddleware = (req, res, next) => {
    var segment = AWSXRay.getSegment(); 
    var traceId = (segment as AWSXRay.Segment)?.trace_id;
    res.set('X-Ray-ID', traceId);
    next();
};

app.use(xRayIdMiddleware);

app.get('/', (req, res) => {

    res.send('Hello World!')
})

app.get('/axios',async (req, res) => {   
     await AxiosTest();
     res.send("ok")
});

app.listen(3000, () => {
    console.log('Server is running on port 3000')
})

app.use(AWSXRay.express.closeSegment());

async function AxiosTest() {
    await axios.get("https://aws.com")
}

package version list "http": "0.0.1-security", "https": "^1.0.0", "aws-xray-sdk": "^3.4.1", "axios": "^1.2.1", "express": "^4.18.2", "node-fetch": "^2.6.7",

carolabadeer commented 1 year ago

Hi @PolestarTommy, Did you see any errors when using axios or were the traces just not showing in the X-Ray console?

I followed your example to reproduce the issue on my end but I was able to see traces in my X-Ray console. I modified the imports to only require axios after AWSXRay.capturePromise() and kept all the other imports for http and https modules before that line, which follows the example we have in the docs for capturing axios requests

const AWSXRay = require('aws-xray-sdk');
const express = require('express');

// Capture all outgoing https requests
AWSXRay.captureHTTPsGlobal(require('https'));
const https = require('https');
AWSXRay.capturePromise();
const AxiosWithXray = require('axios');

and I used a very simple express endpoint with axios to send a request to aws.amazon.com:

app.get('/http-call-aws/', async (req, res) => {

  const endpoint = 'https://aws.amazon.com/';
  await AxiosWithXray.get(endpoint);
  res.send(`Successfully reached ${endpoint}.`);
});

and I was able to see the subsegment in X-Ray: image

Hope some of this information helps! If you're still not able to see traces for axios requests, maybe enable debug logging in the X-Ray daemon and share those logs here to see if there's another issue causing this to happen?

PolestarTommy commented 1 year ago

Hi @carolabadeer

Thank you very much , xray is works with axios , I use this address https://aws.com ,the http response code is 301 , so that is why my case dones't work , thanks again