Skip to content

Commit

Permalink
fix: change requestId header name (#37)
Browse files Browse the repository at this point in the history
* fix: change requestId header name

* fix: separate span and requesrId checks

* feat: add param name const
  • Loading branch information
melikhov-dev authored Sep 20, 2023
1 parent 19892af commit a9d8173
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export {NodeKit} from './nodekit';
export {AppContext} from './lib/context';
export {AppConfig, AppContextParams, AppDynamicConfig} from './types';
export {AppError} from './lib/app-error';
export {REQUEST_ID_HEADER, REQUEST_ID_PARAM_NAME} from './lib/consts';
1 change: 1 addition & 0 deletions src/lib/consts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const REQUEST_ID_HEADER = 'x-request-id';
export const REQUEST_ID_PARAM_NAME = 'requestId';
export const TRACE_KEY = 'uber-trace-id';
12 changes: 7 additions & 5 deletions src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {NodeKit} from '../nodekit';
import {AppConfig, AppContextParams, AppDynamicConfig, Dict} from '../types';

import {AppError} from './app-error';
import {REQUEST_ID_HEADER, REQUEST_ID_PARAM_NAME} from './consts';
import {extractErrorInfo} from './error-parser';

type ContextParams = ContextInitialParams | ContextParentParams;
Expand Down Expand Up @@ -213,14 +214,15 @@ export class AppContext {
}

getMetadata() {
const metadata: Record<string, string> = {};
const requestId = this.get(REQUEST_ID_PARAM_NAME);
if (requestId) {
metadata[REQUEST_ID_HEADER] = requestId;
}
if (this.span) {
const requestId = this.get('requestId');
const metadata = requestId ? {requestId} : {};
this.tracer.inject(this.span, FORMAT_HTTP_HEADERS, metadata);
return metadata;
} else {
return {};
}
return metadata;
}

getTraceId() {
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {pino} from 'pino';

import {REQUEST_ID_PARAM_NAME} from './lib/consts';
import type {LoggingLevel} from './lib/logging';

export interface AppConfig {
Expand Down Expand Up @@ -43,7 +44,7 @@ export interface AppConfig {
}

export interface AppContextParams {
requestId?: string;
[REQUEST_ID_PARAM_NAME]?: string;
}

export interface AppDynamicConfig {}
Expand Down

0 comments on commit a9d8173

Please sign in to comment.