Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 738
Zeeshan Tariq Avatar asked Oct 09 '18 08:10

Zeeshan Tariq


People also ask

What is AWS XRAY tracing?

AWS X-Ray is a service that helps developers analyze and debug distributed applications. Customers use X-Ray to monitor application traces, including the performance of calls to other downstream components or services, in either cloud-hosted applications or from their own machines during development.

How do I enable X-ray on API gateway?

To enable active tracing on an API stageOpen the API Gateway console at https://console.aws.amazon.com/apigateway/ . Choose an API. Choose a stage. On the Logs/Tracing tab, choose Enable X-Ray Tracing and then choose Save Changes.

What is the use of AWS x-ray API?

AWS X-Ray provides APIs for managing debug traces and retrieving service maps and other data created by processing those traces. This document was last published on August 19, 2022. To use the Amazon Web Services Documentation, Javascript must be enabled.

What is AWS XRAY SDK?

PDFRSS. The X-Ray SDK for Java is a set of libraries for Java web applications that provide classes and methods for generating and sending trace data to the X-Ray daemon.


2 Answers

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');
like image 82
ericf89 Avatar answered Oct 28 '22 19:10

ericf89


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.

like image 38
Tanzwud Avatar answered Oct 28 '22 18:10

Tanzwud