简体   繁体   中英

How can we trace axios http requests with aws x-ray?

I'm looking for a method to trace axios http requests in my node.js based aws lambda function . I've found a method to trace HTTP request on aws official docs https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-httpclients.html

var AWSXRay = require('aws-xray-sdk');
var http = AWSXRay.captureHTTPs(require('http'));

But I didn't found any doc or blog regarding axios request tracing. I've tried this code as well, but it's not working.

import AWSXRay from 'aws-xray-sdk';
AWSXRay.captureHTTPsGlobal("../../common/http/HttpClient");
import { HttpClient } from "../../common/http/HttpClient";

I need help in this regards. Thanks!

Since axios will use node's http/https modules under the covers, if you globally capture http and https before you import/require axios, things should work as expected.

import AWSXRay from 'aws-xray-sdk';
import http from 'http';
import https from 'https';

AWSXRay.captureHTTPsGlobal(http);
AWSXRay.captureHTTPsGlobal(https);

const axios = require('axios');

Simple example that should just work is

const
  axios = require('axios'),
  AWSXRay = require('aws-xray-sdk-core');

AWSXRay.captureHTTPsGlobal(require('http')); // Globally instrument http client
AWSXRay.captureHTTPsGlobal(require('https')); // Globally instrument https client

const http = require('http');
const https = require('https');

AWSXRay.capturePromise(); // We should capture promies
const instance = axios.create({
  httpAgent: new http.Agent(),
  httpsAgent: new https.Agent(),
}); // Instrument axious instance

const post = async (url, body) => {
  return await instance.post(url, body);
}

Make sure Lambda has correct acces rights.

I'm looking for a method to trace axios http requests in my node.js based aws lambda function . I've found a method to trace HTTP request on aws official docs https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-httpclients.html

var AWSXRay = require('aws-xray-sdk');
var http = AWSXRay.captureHTTPs(require('http'));

But I didn't found any doc or blog regarding axios request tracing. I've tried this code as well, but it's not working.

import AWSXRay from 'aws-xray-sdk';
AWSXRay.captureHTTPsGlobal("../../common/http/HttpClient");
import { HttpClient } from "../../common/http/HttpClient";

I need help in this regards. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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