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:
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);
}
});
}