Skip to content

Commit

Permalink
feat: create telemetry interceptor for http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeais committed Sep 3, 2024
1 parent 4a50be5 commit 5010f91
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/infra/http/controllers/auth/client-sign-up.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ClientSignUpUseCase } from '@/domain/auth/application/use-cases/client-sign-up';
import { Public } from '@/infra/auth/public.decorator';
import { Logger } from '@/shared/logger';
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Post, UseInterceptors } from '@nestjs/common';
import { z } from 'zod';
import { ZodValidationPipe } from '../../pipes/zod-validation.pipe';
import { UserPresenter } from './presenters/user-presenter';
import { LoginResponse } from './login.controller';
import { TelemetryInterceptor } from '../../interceptors/telemetry.interceptor';

const AddressSchema = z.object({
cep: z.string({
Expand Down Expand Up @@ -50,6 +51,7 @@ export type SignUpBody = z.infer<typeof SignUpBodySchema>;

export type SignUpResponse = LoginResponse;

@UseInterceptors(TelemetryInterceptor)
@Controller('sign-up')
export class ClientSignUpController {
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { User } from '@/domain/auth/enterprise/entities/user';
import { CurrentUser } from '@/infra/auth/current-user.decorator';
import { Logger } from '@/shared/logger';
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { UserHTTPResponse, UserPresenter } from './presenters/user-presenter';
import { TelemetryInterceptor } from '../../interceptors/telemetry.interceptor';

export type GetLoggedUserResponse = UserHTTPResponse;

@Controller('/user')
export class GetLoggedUserController {
constructor(private readonly logger: Logger) {}

@UseInterceptors(TelemetryInterceptor)
@Get()
async handle(@CurrentUser() user: User): Promise<GetLoggedUserResponse> {
try {
Expand Down
8 changes: 3 additions & 5 deletions src/infra/http/controllers/showcase/checkout.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { PaymentType } from '@/domain/showcase/enterprise/entities/enums/payment
import { CurrentUser } from '@/infra/auth/current-user.decorator';
import { Roles } from '@/infra/auth/roles.decorator';
import { Logger } from '@/shared/logger';
import { Body, Controller, Post } from '@nestjs/common';
import { context, trace } from '@opentelemetry/api';
import { Body, Controller, Post, UseInterceptors } from '@nestjs/common';
import { z } from 'zod';
import { TelemetryInterceptor } from '../../interceptors/telemetry.interceptor';
import { ZodValidationPipe } from '../../pipes/zod-validation.pipe';
import {
OrderHTTPResponse,
Expand Down Expand Up @@ -62,6 +62,7 @@ export type CheckoutBody = z.infer<typeof CheckoutBodySchema>;

export type CheckoutResponse = OrderHTTPResponse;

@UseInterceptors(TelemetryInterceptor)
@Controller('/checkout')
export class CheckoutController {
constructor(
Expand All @@ -77,8 +78,6 @@ export class CheckoutController {
body: CheckoutBody,
): Promise<CheckoutResponse> {
try {
const span = trace.getSpan(context.active());
span?.setAttribute('http.request_body', JSON.stringify(body));
this.logger.log(
CheckoutController.name,
`User ${currentUser.id.toString()} - ${currentUser.name} Checkout with: ${JSON.stringify(body)}.`,
Expand All @@ -102,7 +101,6 @@ export class CheckoutController {
);

const result = OrderPresenter.toHTTP(order);
span?.setAttribute('http.response_body', JSON.stringify(result));

return result;
} catch (error: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { GetCustomerOrders } from '@/domain/showcase/application/use-cases/get-c
import { CurrentUser } from '@/infra/auth/current-user.decorator';
import { Roles } from '@/infra/auth/roles.decorator';
import { Logger } from '@/shared/logger';
import { Controller, Get, Query } from '@nestjs/common';
import { Controller, Get, Query, UseInterceptors } from '@nestjs/common';
import { z } from 'zod';
import { ZodValidationPipe } from '../../pipes/zod-validation.pipe';
import {
OrderHTTPResponse,
OrderPresenter,
} from './presenters/order-presenter';
import { TelemetryInterceptor } from '../../interceptors/telemetry.interceptor';

const GetCustomerOrdersParamsSchema = z.object({
limit: z
Expand All @@ -32,6 +33,7 @@ export type GetCustomerOrdersParams = z.infer<

export type GetCustomerOrdersResponse = PaginatedResponse<OrderHTTPResponse>;

@UseInterceptors(TelemetryInterceptor)
@Controller('/orders')
export class GetCustomerOrdersController {
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { GetShowcaseCategoriesUseCase } from '@/domain/showcase/application/use-cases/get-showcase-categories';
import { Public } from '@/infra/auth/public.decorator';
import { Logger } from '@/shared/logger';
import { Controller, Get, Query } from '@nestjs/common';
import { Controller, Get, Query, UseInterceptors } from '@nestjs/common';
import { z } from 'zod';
import { ZodValidationPipe } from '../../pipes/zod-validation.pipe';
import {
ShowcaseCategoryHTTPResponse,
ShowcaseCategoryPresenter,
} from './presenters/showcase-category-presenter';
import { TelemetryInterceptor } from '../../interceptors/telemetry.interceptor';

const GetShowcaseCategoriesParamsSchema = z.object({
limit: z
Expand Down Expand Up @@ -37,6 +38,7 @@ export type GetShowcaseCategoriesResponse = {

export type GetShowcaseCategoryResponse = ShowcaseCategoryHTTPResponse;

@UseInterceptors(TelemetryInterceptor)
@Controller('/showcase/categories')
export class ShowcaseCategoriesController {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { GetShowcaseProductsUseCase } from '@/domain/showcase/application/use-ca
import { GetSimilarProductsUseCase } from '@/domain/showcase/application/use-cases/get-similar-products';
import { Public } from '@/infra/auth/public.decorator';
import { Logger } from '@/shared/logger';
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Controller, Get, Param, Query, UseInterceptors } from '@nestjs/common';
import { z } from 'zod';
import { ZodValidationPipe } from '../../pipes/zod-validation.pipe';
import {
ShowcaseProductHTTPResponse,
ShowcaseProductPresenter,
} from './presenters/showcase-product-presenter';
import { TelemetryInterceptor } from '../../interceptors/telemetry.interceptor';

const GetShowcaseProductsParamsSchema = z.object({
limit: z
Expand Down Expand Up @@ -53,6 +54,7 @@ export type GetSimilarProductsResponse = {
items: ShowcaseProductHTTPResponse[];
};

@UseInterceptors(TelemetryInterceptor)
@Controller('/showcase/products')
export class ShowcaseProductsController {
constructor(
Expand Down
40 changes: 40 additions & 0 deletions src/infra/http/interceptors/telemetry.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { context, trace } from '@opentelemetry/api';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class TelemetryInterceptor implements NestInterceptor {
intercept(_context: ExecutionContext, next: CallHandler): Observable<any> {
const request = _context.switchToHttp().getRequest();
const span = trace.getSpan(context.active());

if (span) {
span.setAttribute('http.request_body', JSON.stringify(request.body));
span.setAttribute(
'http.request_headers',
JSON.stringify(request.headers),
);
}

return next.handle().pipe(
tap({
next: (data) => {
if (span) {
span.setAttribute('http.response_body', JSON.stringify(data));
}
},
error: (error) => {
if (span) {
span.setAttribute('http.error', JSON.stringify(error));
}
},
}),
);
}
}

0 comments on commit 5010f91

Please sign in to comment.