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

2

27

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!

Empedocles answered 9/10, 2018 at 8:50 Comment(4)
Hi, could you elaborate more on "it's not working"? Are you getting any errors or the http info is simply missing? What is the syntax or code snippet you are using for making http calls using axios? Please note that the AWS X-Ray SDK for Nodejs doesn't officially support axios and you can see the patching code for the built-in http client here github.com/aws/aws-xray-sdk-node/blob/master/packages/core/lib/…Medieval
Hi, I'm not getting any error by adding these lines in my lambda function. import AWSXRay from 'aws-xray-sdk'; AWSXRay.captureHTTPsGlobal("../../common/http/HttpClient"); import { HttpClient } from "../../common/http/HttpClient"; Actually in my lambda function I'm making an axios post request and testing an endpoint which returns 500 code. Ideally this error should be logged via aws x-ray but it's not logging the error. But getting this message printed while deployment: "AWS_XRAY_CONTEXT_MISSING is set. Configured context missing strategy to LOG_ERROR"Empedocles
@Medieval I've just found a method to log axios errors using captureAsyncFunc but I'm wondering if I can convert it into promise so the code becomes cleaner. I've added the piece of code here: jsfiddle.net/zeeshantariq/1b68eLdr/77938 But I'm getting error: Error: Param "fcn" must be a function.Empedocles
this is in the backlog. You can open an issue here github.com/aws/aws-xray-sdk-node or submit a PR for promisifying captureMedieval
F
22

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');
Flemings answered 6/12, 2018 at 20:34 Comment(0)
F
1

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.

Fumigator answered 27/12, 2019 at 21:6 Comment(1)
this doesn't work, it says capturePromise is not a function. Where do these variables need to be declared? globally?Agentival

© 2022 - 2024 — McMap. All rights reserved.