title: AWS X-Ray with Node.JS Express and request
body: <p>So I'm trying to implement X-Ray with my express app.
  I have my app.js file and inside reference a router file.</p>
  
  <p>my project structure:<br>
  <code>project/
    routes/
      index.js
    app.js</code></p>
  
  <p>app.js:<br></p>
  
  <pre><code>var indexRouter = require("./routes/index")
  ...
  app.use("/", indexRouter)
  ...
  </code></pre>
  
  <p>index.js:<br></p>
  
  <pre><code>const AWSXRay = require("aws-xray-sdk")
  const request = require("request")
  
  router.post("/xray", async function(req, res, next) {
      let app = req.app
      app.use(AWSXRay.express.openSegment("MyApp"))
  
      try {
          console.log(AWSXRay.getSegment()) // this succesfully gets segment
          AWSXRay.captureAsyncFunc("send", function(subsegment) {
              request.get("http://www.google.com", { XRaySegment: subsegment }, function() {
                  res.json({
                      success: "success"
                  })
                  subsegment.close()
          })
      } catch (err) {
          next(err)
      }
      app.use(AWSXRay.express.closeSegment())
  })
  </code></pre>
  
  <p>I'm following the automatic mode examples: Capture through async function calls from the AWS documentation (<a href="https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/index.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/index.html</a>), but I am getting a error that says:
  "Failed to get the current sub/segment from the context." </p>
  
  <p><br>Can anyone let me know what I am doing wrong?</p>