Skip to content

Commit

Permalink
Send subsegment to xray daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Nov 13, 2023
1 parent ca35ee6 commit 2ca81b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/enhanced/enhancedApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWSXRay from 'aws-xray-sdk';
// eslint-disable-next-line @typescript-eslint/no-var-requires


import mongodb, { MongoClient } from 'mongodb';
import c from 'config';
Expand Down
34 changes: 34 additions & 0 deletions src/enhanced/utils/queue/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ReceiveMessageCommandInput, SQS } from '@aws-sdk/client-sqs';
import crypto from 'crypto';
import config from 'config';
import dgram from 'dgram';
import AWSXRay from 'aws-xray-sdk-core';
import { JobsQueuePayload } from '../../types/job-types';
import { isJobQueuePayload } from '../../types/utils/type-guards';
Expand Down Expand Up @@ -72,6 +74,38 @@ export async function listenToJobQueue(): Promise<JobsQueuePayload> {

const payload = JSON.parse(message.Body);

const { xrayTraceId } = payload;

if (xrayTraceId) {
console.log('Xray trace id: ', xrayTraceId);
const startTime = Date.now();
const traceId = xrayTraceId.split(';')[0];
const parentSegment = xrayTraceId.split(';')[1].split('=')[1];
const segmentId = crypto.randomBytes(16).toString('hex');

const newSegment = {
name: 'Autobuilder',
id: segmentId,
trace_id: traceId,
parent_id: parentSegment,
type: 'subsegment',
start_time: startTime,
end_time: Date.now(),
};

const client = dgram.createSocket('udp4');

client.send(JSON.stringify(newSegment), 2000, '127.0.0.1', (err) => {
if (err) {
console.error('Error occurred when sending udp message to xray daemon', err);
}

client.close();
});
} else {
console.log('no trace id found');
}

// Use type guard here to validate payload we have received from the queue.
// This ensures that the `payload` object will be of type `JobQueuePayload` after the if statement.
if (!isJobQueuePayload(payload)) {
Expand Down
1 change: 1 addition & 0 deletions src/entities/queueMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class JobQueueMessage {
jobId: string;
jobStatus: JobStatus;
tries: number;
xrayTraceId?: string = process.env._X_AMZN_TRACE_ID;
taskId?: string;

constructor(jobId: string, status: JobStatus, tries = 0, taskId?: string) {
Expand Down

0 comments on commit 2ca81b2

Please sign in to comment.