UNPKG

event-message-broker

Version:

A Message Bus that uses AWS Stack and RabbitMQ

96 lines (95 loc) 4.11 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { startSpan } from "../../application/utils/o11y"; import { EventBridgeService } from "./EventBridgeService"; import { SNSService } from "./SNSService"; import { SQSService } from "./SQSService"; import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; export class AWSBus { constructor() { this.eventBridge = new EventBridgeService(); this.sqs = new SQSService(); this.sns = new SNSService(); } publish(topicName, message) { return __awaiter(this, void 0, void 0, function* () { yield this.handleSpan((span) => __awaiter(this, void 0, void 0, function* () { span.setAttribute('topic', topicName); const messageId = yield this.sns.publish(topicName, message); span.addEvent('message-published', { provider: 'aws-sns', messageId, correlationId: message.CorrelationId }); })); }); } send(queueName, message, params) { return __awaiter(this, void 0, void 0, function* () { yield this.handleSpan((span) => __awaiter(this, void 0, void 0, function* () { span.setAttribute('queue', queueName); const messageId = yield this.sqs.send(queueName, message, params); span.addEvent('message-sent', { provider: 'aws-sqs', messageId, correlationId: message.CorrelationId }); })); }); } redelivery(params) { return __awaiter(this, void 0, void 0, function* () { yield this.handleSpan((span) => __awaiter(this, void 0, void 0, function* () { span.setAttribute('queue', params.QueueName); const output = yield this.sqs.redelivery(params); span.addEvent('message-redelivered', { provider: 'aws-sqs', messageId: output.messageId, correlationId: params.Message.Object.CorrelationId, delay: params.DelaySeconds + 's', attempt: output.attempt, startsAt: output.startsAt }); })); }); } schedule(params) { return __awaiter(this, void 0, void 0, function* () { yield this.handleSpan((span) => __awaiter(this, void 0, void 0, function* () { span.setAttribute('topic', params.TopicName); const output = yield this.eventBridge.schedule(params); span.addEvent('message-scheduled', { correlationId: params.Message.CorrelationId, scheduleName: output.Id, scheduleArn: output.ScheduleArn }); })); }); } consume(params) { this.sqs.consume(params); } handleSpan(func) { return __awaiter(this, void 0, void 0, function* () { const span = startSpan('bus', SpanKind.PRODUCER); try { yield func(span); } catch (error) { span.setStatus({ code: SpanStatusCode.ERROR, message: error.message }); span.recordException(error); throw error; } finally { span.end(); } }); } }