Skip to content

Commit

Permalink
feat: add options for configurations rabbitmq and open-telemetry module
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Dec 18, 2023
1 parent 2df865b commit dd15ba0
Show file tree
Hide file tree
Showing 39 changed files with 331 additions and 178 deletions.
2 changes: 1 addition & 1 deletion src/booking/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {HttpContextMiddleware} from "building-blocks/context/context";
secret: configs.jwt.secret,
signOptions: {expiresIn: configs.jwt.refreshExpirationDays},
}),
OpenTelemetryModule,
OpenTelemetryModule.forRoot(),
TypeOrmModule.forRoot(postgresOptions),
BookingModule,
RouterModule.register([
Expand Down
2 changes: 1 addition & 1 deletion src/booking/src/booking/booking.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {CreateBookingController, CreateBookingHandler} from "./features/v1/creat


@Module({
imports: [CqrsModule, RabbitmqModule, TypeOrmModule.forFeature([Booking])],
imports: [CqrsModule, RabbitmqModule.forRoot(), TypeOrmModule.forFeature([Booking])],
controllers: [CreateBookingController],
providers: [ CreateBookingHandler,
{
Expand Down
9 changes: 8 additions & 1 deletion src/booking/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Request, Response} from "express";
async function bootstrap() {
const app = await NestFactory.create(AppModule);

app.enableShutdownHooks();

const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = configs.port || 3366;
Expand All @@ -30,7 +32,12 @@ async function bootstrap() {

app.useGlobalPipes(new ValidationPipe({ transform: true }));

app.use((req: Request, res: Response, next: any) => {req.url === '/' || '/favicon.ico' ? res.send(configs.serviceName) : next();});
app.use((req: Request, res: Response, next: any) => {
if (req.originalUrl == '/' || req.originalUrl.includes('favicon.ico')) {
return res.send(configs.serviceName);
}
return next();
});

PrometheusMetrics.registerMetricsEndpoint(app);

Expand Down
13 changes: 12 additions & 1 deletion src/building-blocks/openTelemetry/open-telemetry-tracer.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { OnModuleInit } from '@nestjs/common';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { Tracer } from '@opentelemetry/api';
export declare class OpenTelemetryOptions {
jaegerEndpoint: string;
zipkinEndpoint: string;
serviceName: string;
constructor(partial?: Partial<OpenTelemetryOptions>);
}
export interface IOpenTelemetryTracer {
createTracer(tracerName: string): Tracer;
}
export declare const otelSDK: NodeSDK;
export declare class OpenTelemetryTracer implements OnModuleInit {
export declare class OpenTelemetryTracer implements IOpenTelemetryTracer, OnModuleInit {
private readonly options?;
constructor(options?: OpenTelemetryOptions);
onModuleInit(): void;
createTracer(tracerName: string): Tracer;
}
45 changes: 38 additions & 7 deletions src/building-blocks/openTelemetry/open-telemetry-tracer.js

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.

34 changes: 28 additions & 6 deletions src/building-blocks/openTelemetry/open-telemetry-tracer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OnModuleInit } from '@nestjs/common';
import {Inject, Injectable, OnModuleInit} from '@nestjs/common';
import { BatchSpanProcessor, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
import { AmqplibInstrumentation } from '@opentelemetry/instrumentation-amqplib';
Expand All @@ -13,13 +13,29 @@ import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { trace, Tracer } from '@opentelemetry/api';
import configs from '../configs/configs';

export class OpenTelemetryOptions {
jaegerEndpoint: string;
zipkinEndpoint: string;
serviceName: string;

constructor(partial?: Partial<OpenTelemetryOptions>) {
Object.assign(this, partial);
}
}

let openTelemetryOptions: OpenTelemetryOptions;

export interface IOpenTelemetryTracer {
createTracer(tracerName: string): Tracer;
}

const zipkinExporter = new ZipkinExporter({
url: configs.monitoring.zipkinEndpoint,
serviceName: configs.serviceName
url: openTelemetryOptions?.zipkinEndpoint ?? configs.monitoring.zipkinEndpoint,
serviceName: openTelemetryOptions?.serviceName ?? configs.serviceName
});

const jaegerExporter = new JaegerExporter({
endpoint: configs.monitoring.jaegerEndpoint
endpoint: openTelemetryOptions?.jaegerEndpoint ?? configs.monitoring.jaegerEndpoint
});

export const otelSDK = initializeOpenTelemetrySDK();
Expand All @@ -28,7 +44,7 @@ function initializeOpenTelemetrySDK() {

const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: configs.serviceName
[SemanticResourceAttributes.SERVICE_NAME]: openTelemetryOptions?.serviceName ?? configs.serviceName
})
});

Expand All @@ -50,7 +66,12 @@ function initializeOpenTelemetrySDK() {
});
}

export class OpenTelemetryTracer implements OnModuleInit {
@Injectable()
export class OpenTelemetryTracer implements IOpenTelemetryTracer, OnModuleInit {
constructor(@Inject(OpenTelemetryOptions) private readonly options?: OpenTelemetryOptions) {
openTelemetryOptions = options;
}

onModuleInit(): void {
otelSDK.start();
}
Expand All @@ -60,3 +81,4 @@ export class OpenTelemetryTracer implements OnModuleInit {
return tracer;
}
}

3 changes: 3 additions & 0 deletions src/building-blocks/openTelemetry/open-telemetry.module.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import { DynamicModule } from '@nestjs/common';
import { OpenTelemetryOptions } from './open-telemetry-tracer';
export declare class OpenTelemetryModule {
static forRoot(options?: OpenTelemetryOptions): DynamicModule;
}
12 changes: 10 additions & 2 deletions src/building-blocks/openTelemetry/open-telemetry.module.js

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.

15 changes: 11 additions & 4 deletions src/building-blocks/openTelemetry/open-telemetry.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Module } from '@nestjs/common';
import { OpenTelemetryTracer } from './open-telemetry-tracer';

import {DynamicModule, Global, Module} from '@nestjs/common';
import {OpenTelemetryOptions, OpenTelemetryTracer} from './open-telemetry-tracer';
@Global()
@Module({
providers: [OpenTelemetryTracer],
exports: [OpenTelemetryTracer]
})
export class OpenTelemetryModule {}
export class OpenTelemetryModule {
static forRoot(options?: OpenTelemetryOptions): DynamicModule {
return {
module: OpenTelemetryModule,
providers: [OpenTelemetryTracer, { provide: OpenTelemetryOptions, useValue: options }]
};
}
}
13 changes: 7 additions & 6 deletions src/building-blocks/rabbitmq/rabbitmq-connection.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { OnModuleInit } from '@nestjs/common';
import * as amqp from 'amqplib';
export interface RabbitmqOptions {
export declare class RabbitmqOptions {
host: string;
port: number;
username: string;
password: string;
exchange: string;
username: string;
constructor(partial?: Partial<RabbitmqOptions>);
}
export interface IRabbitmqConnection {
createConnection(options?: RabbitmqOptions): Promise<amqp.Connection>;
getChannel(): Promise<amqp.Channel>;
closeChanel(): Promise<void>;
closeConnection(): Promise<void>;
}
export declare class RabbitmqConnection implements OnModuleInit, IRabbitmqConnection {
private connection;
private channel;
private readonly options?;
constructor(options?: RabbitmqOptions);
onModuleInit(): Promise<void>;
createConnection(options?: RabbitmqOptions): Promise<amqp.Connection>;
getChannel(): Promise<amqp.Channel>;
closeChanel(): Promise<void>;
closeConnection(): Promise<void>;
private initializeConnection;
}
Loading

0 comments on commit dd15ba0

Please sign in to comment.