2

I have a lambda which has xray enabled and it sends http request to API gateway which links to another lambda. I also enabled xray in the API gateway. But it doesn't trace the whole flow because the trace id in the first lambda is not used by API Gateway.

My question is how I can send the trace id over http rest request? I'd like to link from end to end flow.

I am using axios in nodejs and I don't know how awsXRay.captureHTTPsGlobal works with this library.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

2 Answers2

2

The AWS Lambda has automatic instrumentation enabled, It means the calls to aws services are tracked. But when it comes to http(s) client, explicit instrumentation is necessary.

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

Tracing calls to downstream HTTP web services using the X-Ray SDK for Node.js

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
1

You can retrace the whole flow but you need manually capture the all HTTP/S external requests :

const awsXRay = require("aws-xray-sdk")
awsXRay.captureAWS(require("aws-sdk"))
awsXRay.captureHTTPsGlobal(require("http"))
awsXRay.captureHTTPsGlobal(require("https"))

And in your handler ( before making the HTTP request ) to allow the tracing of promise based requests ( Axios for example ) :

awsXRay.capturePromise()
  • Have you configured x-ray for NodeJs scripts that are not lambda or express. I could not find any samples.do you have any samples – Arun Kamalanathan Oct 07 '20 at 22:12
  • I see you are using `http`. Does it work for `axios`? – Joey Yi Zhao Oct 07 '20 at 22:21
  • You need to send the instrumented http/s agents in the Axios config object as follows: `awsXRay.captureHTTPsGlobal(require("http")) awsXRay.captureHTTPsGlobal(require("https")) const http = require('http') const https = require('https') const config = { httpAgent: new http.Agent(), httpsAgent: new https.Agent(), ... }` – Jordi P.S. Jan 11 '21 at 01:17