AWS X-Ray Segments Sent from NestJS App Not Visible in X-Ray Console

58 Views Asked by At

I'm working on integrating AWS X-Ray tracing into a NestJS application. I'm manually constructing segment documents and using a UDP socket to send this data to the AWS X-Ray Daemon.

The segments appear to be successfully sent to the X-Ray Daemon, as evidenced by the logs from the daemon itself:

enter image description here

However, these segments are not visible in the AWS X-Ray console. Here's a sample of the log data I'm sending to the X-Ray Daemon:

Jan 17 14:56:06 backend[63] INFO: {"message":"TraceData1 sent successfully","params":["{\"format\":\"json\",\"version\":1}\n{\"trace_id\":\"1-65a7ea87-e68e835cc63b6b0c8e183c50\",\"id\":\"4ccb3162564bdf12\",\"start_time\":1705503366.878,\"name\":\"app-Backend\",\"in_progress\":true,\"counter\":0,\"service\":{\"runtime\":\"node\",\"runtime_version\":\"v18.18.2\",\"version\":\"0.0.0\",\"name\":\"backend\"},\"aws\":{\"ecs\":{\"container\":\"ip-172-16-11-244.ec2.internal\"},\"xray\":{\"sdk\":\"X-Ray for Node.js\",\"sdk_version\":\"3.5.3\",\"package\":\"aws-xray-sdk\"}},\"origin\":\"AWS::ECS::Container\"}"]}

I've verified the format of the trace_id, segment_id, and other fields according to AWS documentation. The application is running in an ECS container, and the X-Ray daemon has the necessary IAM permissions.

I'm puzzled as to why the segments are not showing up in the X-Ray console. Has anyone encountered a similar issue or can provide insights into what might be going wrong?

Thank you in advance for your help!

Here is the code block;

public sendTrace(segment: AWSXRay.Segment) {
    const udpIp = '127.0.0.1';
    const udpPort = 2000;
    const socket = dgram.createSocket('udp4');

    const startTime = Date.now() / 1000; // Convert to seconds
    const hex = Math.floor(startTime).toString(16);
    const traceId = '1-' + hex + '-' + crypto.randomBytes(12).toString('hex');
    const segmentId = crypto.randomBytes(8).toString('hex');
    const segmentDoc = JSON.stringify({
      trace_id: traceId,
      id: segmentId,
      start_time: startTime,
      in_progress: true,
      name: 'orion-backend',
    });
    const header = JSON.stringify({ format: 'json', version: 1 });
    const traceData = header + '\n' + segmentDoc;
    const traceData1 = header + '\n' + JSON.stringify(segment);

    socket.send(Buffer.from(traceData1), udpPort, udpIp, (error) => {
      if (error) {
        console.error('Trace send error:', error);
      } else {
        logger.info('TraceData1 sent successfully', traceData1);
      }
    });

    socket.send(Buffer.from(traceData), udpPort, udpIp, (error) => {
      if (error) {
        console.error('Trace send error:', error);
      } else {
        console.log('TraceData sent successfully', traceData);
      }
    });
  }

In my efforts to integrate AWS X-Ray tracing into my NestJS application, I've taken the following steps:

  1. Manually Constructed Segment Documents: I created segment documents according to the format specified by AWS X-Ray, ensuring that fields like trace_id, segment_id, and start_time are correctly formatted.

  2. Sent Data via UDP Socket: I set up a UDP socket connection in my application to send the constructed segment data directly to the AWS X-Ray Daemon.

  3. Verified Daemon Receipt: I confirmed that the AWS X-Ray Daemon is receiving the data as intended, as indicated by the daemon's log entries stating Successfully sent batch of 2 segments.

  4. Checked IAM Permissions: I ensured that the IAM role associated with the X-Ray daemon has the necessary permissions, namely xray:PutTraceSegments and xray:PutTelemetryRecords.

  5. Ensured Correct Networking: Since the application is containerized in ECS, I made sure that the network configuration allows for UDP traffic between my application container and the X-Ray daemon.

What I was expecting after these steps was to see the traces from these segments reflected in the AWS X-Ray console. However, despite the daemon logs indicating successful transmission, these segments are not visible in the console. I anticipated being able to view and analyze these traces to monitor and troubleshoot my application, but the absence of data in the X-Ray console is hindering this process.

I'm seeking insights or suggestions on what might be missing or misconfigured in this setup that's preventing the segments from being displayed in the AWS X-Ray console.

0

There are 0 best solutions below