Skip to content

Commit

Permalink
refactor: refactor options in rabbitmq and otel with builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Dec 20, 2023
1 parent c7f86e2 commit e961bd8
Show file tree
Hide file tree
Showing 45 changed files with 413 additions and 272 deletions.
5 changes: 4 additions & 1 deletion src/booking/src/extensions/otel.extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { container } from 'tsyringe';
import { Express } from 'express';
import { PrometheusMetrics } from 'building-blocks/monitoring/prometheus.metrics';
import { OpenTelemetryTracer } from 'building-blocks/open-telemetry/open-telemetry';
import config from 'building-blocks/config/config';

export const initialOpenTelemetry = async (app?: Express): Promise<OpenTelemetryTracer> => {
// tracing
const openTelemetryTracer = container.resolve(OpenTelemetryTracer);
await openTelemetryTracer.createTracer((x) => x.serviceName == x.serviceName);
await openTelemetryTracer.createTracer((builder) => {
builder.serviceName(config?.serviceName);
});

// monitoring
if (app) {
Expand Down
29 changes: 15 additions & 14 deletions src/booking/src/extensions/rabbitmq.extensions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { RabbitMQConnection } from 'building-blocks/rabbitmq/rabbitmq-connection';
import { container } from 'tsyringe';
import { Rabbitmq } from 'building-blocks/rabbitmq/rabbitmq';
import { RabbitmqConnectionOptions } from 'building-blocks/rabbitmq/rabbitmq-connection-options-builder';
import { IPublisher, Publisher } from 'building-blocks/rabbitmq/rabbitmq-publisher';
import { RabbitmqOptions } from 'building-blocks/rabbitmq/rabbitmq-options-builder';

export const initialRabbitmq = async (options?: RabbitmqOptions): Promise<RabbitMQConnection> => {
const rabbitMQConnection = container.resolve(RabbitMQConnection);
export const initialRabbitmq = async (options?: RabbitmqConnectionOptions): Promise<void> => {
const rabbitmq = container.resolve(Rabbitmq);

await rabbitMQConnection.createConnection((optionsBuilder) => {
optionsBuilder.host = options?.host;
optionsBuilder.port = options?.port;
optionsBuilder.username = options?.username;
optionsBuilder.password = options?.password;
});

container.register<IPublisher>('IPublisher', Publisher);

return rabbitMQConnection;
await rabbitmq
.createConnection((builder) => {
builder
.host(options?.host)
.port(options?.port)
.username(options?.username)
.password(options?.password);
})
.then((c) => {
container.register<IPublisher>('IPublisher', Publisher);
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ export declare class OpenTelemetryOptions {
}
export declare class OpenTelemetryOptionsBuilder {
private _options;
get jaegerEndpoint(): string;
set jaegerEndpoint(value: string);
get zipkinEndpoint(): string;
set zipkinEndpoint(value: string);
get serviceName(): string;
set serviceName(value: string);
jaegerEndpoint(value: string): OpenTelemetryOptionsBuilder;
zipkinEndpoint(value: string): OpenTelemetryOptionsBuilder;
serviceName(value: string): OpenTelemetryOptionsBuilder;
build(): OpenTelemetryOptions;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,19 @@ export class OpenTelemetryOptions {
export class OpenTelemetryOptionsBuilder {
private _options: OpenTelemetryOptions = new OpenTelemetryOptions();

get jaegerEndpoint(): string {
return this._options.jaegerEndpoint;
}

set jaegerEndpoint(value: string) {
jaegerEndpoint(value: string): OpenTelemetryOptionsBuilder {
this._options.jaegerEndpoint = value;
return this;
}

get zipkinEndpoint(): string {
return this._options.zipkinEndpoint;
}

set zipkinEndpoint(value: string) {
zipkinEndpoint(value: string): OpenTelemetryOptionsBuilder {
this._options.zipkinEndpoint = value;
return this;
}

get serviceName(): string {
return this._options.serviceName;
}

set serviceName(value: string) {
serviceName(value: string) : OpenTelemetryOptionsBuilder{
this._options.serviceName = value;
return this;
}

build(): OpenTelemetryOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export declare class RabbitmqConnectionOptions {
host: string;
port: number;
username: string;
password: string;
}
export declare class RabbitmqConnectionOptionsBuilder {
private _options;
host(value: string): RabbitmqConnectionOptionsBuilder;
port(value: number): RabbitmqConnectionOptionsBuilder;
username(value: string): RabbitmqConnectionOptionsBuilder;
password(value: string): RabbitmqConnectionOptionsBuilder;
build(): RabbitmqConnectionOptions;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class RabbitmqConnectionOptions {
host: string;
port: number;
username: string;
password: string;
}

export class RabbitmqConnectionOptionsBuilder {
private _options: RabbitmqConnectionOptions = new RabbitmqConnectionOptions();

host(value: string): RabbitmqConnectionOptionsBuilder {
this._options.host = value;
return this;
}

port(value: number): RabbitmqConnectionOptionsBuilder {
this._options.port = value;
return this;
}

username(value: string): RabbitmqConnectionOptionsBuilder {
this._options.username = value;
return this;
}

password(value: string): RabbitmqConnectionOptionsBuilder {
this._options.password = value;
return this;
}

build(): RabbitmqConnectionOptions {
return this._options;
}
}
8 changes: 5 additions & 3 deletions src/building-blocks/rabbitmq/rabbitmq-connection.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { RabbitmqOptionsBuilder } from './rabbitmq-options-builder';
import * as amqp from 'amqplib';
import { RabbitmqConnectionOptions } from "./rabbitmq-connection-options-builder";
export interface IRabbitMQConnection {
createConnection(rabbitmqOptionsBuilder?: (rabbitmqOptionsBuilder?: RabbitmqOptionsBuilder) => void): Promise<amqp.Connection>;
createConnection(options?: RabbitmqConnectionOptions): Promise<amqp.Connection>;
getConnection(): Promise<amqp.Connection>;
getChannel(): Promise<amqp.Channel>;
closeChanel(): Promise<void>;
closeConnection(): Promise<void>;
}
export declare class RabbitMQConnection implements IRabbitMQConnection {
createConnection(rabbitmqOptionsBuilder?: (rabbitmqOptionsBuilder?: RabbitmqOptionsBuilder) => void): Promise<amqp.Connection>;
createConnection(options?: RabbitmqConnectionOptions): Promise<amqp.Connection>;
getConnection(): Promise<amqp.Connection>;
getChannel(): Promise<amqp.Channel>;
closeChanel(): Promise<void>;
closeConnection(): Promise<void>;
Expand Down
9 changes: 4 additions & 5 deletions src/building-blocks/rabbitmq/rabbitmq-connection.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/building-blocks/rabbitmq/rabbitmq-connection.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions src/building-blocks/rabbitmq/rabbitmq-connection.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { container, injectable } from 'tsyringe';
import { injectable } from 'tsyringe';
import { Logger } from '../logging/logger';
import { RabbitmqOptionsBuilder } from './rabbitmq-options-builder';
import * as amqp from 'amqplib';
import config from '../config/config';
import asyncRetry from 'async-retry';
import {RabbitmqConnectionOptions} from "./rabbitmq-connection-options-builder";

let connection: amqp.Connection = null;
let channel: amqp.Channel = null;

export interface IRabbitMQConnection {
createConnection(
rabbitmqOptionsBuilder?: (rabbitmqOptionsBuilder?: RabbitmqOptionsBuilder) => void
): Promise<amqp.Connection>;
createConnection(options?: RabbitmqConnectionOptions): Promise<amqp.Connection>;

getConnection(): Promise<amqp.Connection>;

getChannel(): Promise<amqp.Channel>;

Expand All @@ -22,16 +22,9 @@ export interface IRabbitMQConnection {

@injectable()
export class RabbitMQConnection implements IRabbitMQConnection {
async createConnection(
rabbitmqOptionsBuilder?: (rabbitmqOptionsBuilder?: RabbitmqOptionsBuilder) => void
): Promise<amqp.Connection> {
async createConnection(options?: RabbitmqConnectionOptions): Promise<amqp.Connection> {
if (!connection || !connection == undefined) {
try {
const builder = new RabbitmqOptionsBuilder();
rabbitmqOptionsBuilder(builder);

const options = builder.build();

const host = options?.host ?? config.rabbitmq.host;
const port = options?.port ?? config.rabbitmq.port;

Expand Down Expand Up @@ -70,6 +63,10 @@ export class RabbitMQConnection implements IRabbitMQConnection {
return connection;
}

async getConnection(): Promise<amqp.Connection> {
return connection;
}

async getChannel(): Promise<amqp.Channel> {
try {
if (!connection) {
Expand Down
Loading

0 comments on commit e961bd8

Please sign in to comment.