Skip to content

Commit

Permalink
feat: mark req.id as deprecated (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
melikhov-dev authored Dec 12, 2024
1 parent 8335030 commit 8d75e09
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/base-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {DEFAULT_REQUEST_ID_HEADER} from './constants';
export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
expressApp.use((req, res, next) => {
try {
req.id = (req.headers[DEFAULT_REQUEST_ID_HEADER] || uuidv4()) as string;
res.setHeader(DEFAULT_REQUEST_ID_HEADER, req.id);
const requestId = (req.headers[DEFAULT_REQUEST_ID_HEADER] || uuidv4()) as string;
req.id = requestId;
res.setHeader(DEFAULT_REQUEST_ID_HEADER, requestId);

res.setHeader('Surrogate-Control', 'no-store');
res.setHeader(
Expand All @@ -25,18 +26,18 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {

req.originalContext = req.ctx = ctx.create(`Express ${req.method}`, {
parentSpanContext,
loggerPostfix: `[${req.id}]`,
loggerPostfix: `[${requestId}]`,
spanKind: 1, // SERVER
});
req.ctx.set(REQUEST_ID_PARAM_NAME, req.id);
req.ctx.set(REQUEST_ID_PARAM_NAME, requestId);

req.ctx.setTag('http.hostname', req.hostname);
req.ctx.setTag('http.method', req.method);
req.ctx.setTag('http.url', ctx.utils.redactSensitiveQueryParams(req.url));
req.ctx.setTag('path', ctx.utils.redactSensitiveQueryParams(req.path));
req.ctx.setTag('referer', ctx.utils.redactSensitiveQueryParams(req.get('referer')));
req.ctx.setTag('remote_ip', req.ip ?? 'unknown');
req.ctx.setTag('request_id', req.id);
req.ctx.setTag('request_id', requestId);
req.ctx.setTag('user_agent', userAgent);

const traceId = req.ctx.getTraceId();
Expand All @@ -46,7 +47,7 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
}

req.ctx.addLoggerExtra('req', {
id: req.id,
id: requestId,
method: req.method,
url: ctx.utils.redactSensitiveQueryParams(req.path),
});
Expand All @@ -56,7 +57,7 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
: {
traceId,
req: {
id: req.id,
id: requestId,
method: req.method,
url: ctx.utils.redactSensitiveQueryParams(req.url),
headers: ctx.utils.redactSensitiveHeaders(req.headers),
Expand Down
4 changes: 2 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {AppContext} from '@gravity-ui/nodekit';
import {type AppContext, REQUEST_ID_PARAM_NAME} from '@gravity-ui/nodekit';
import {Express, Router} from 'express';

import {cspMiddleware, getAppPresets} from './csp/middleware';
Expand Down Expand Up @@ -106,7 +106,7 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
service: 'self',
action: req.routeInfo.handlerName || UNNAMED_CONTROLLER,
responseStatus: res.statusCode,
requestId: req.id,
requestId: req.ctx.get(REQUEST_ID_PARAM_NAME) || '',
requestTime: req.originalContext.getTime(), //We have to use req.originalContext here to get full time
requestMethod: req.method,
requestUrl: req.originalUrl,
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ declare global {
// eslint-disable-next-line
namespace Express {
export interface Request {
/**
* @deprecated Use req.ctx.get(REQUEST_ID_PARAM_NAME) instead of req.id
*/
id: string;
ctx: AppContext;
originalContext: AppContext;
Expand All @@ -27,6 +30,9 @@ declare global {

declare module 'express' {
export interface Request {
/**
* @deprecated Use req.ctx.get(REQUEST_ID_PARAM_NAME) instead of req.id
*/
id: string;
ctx: AppContext;
originalContext: AppContext;
Expand Down

0 comments on commit 8d75e09

Please sign in to comment.