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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With