Skip to content

Commit

Permalink
Helper function getTraceId
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-yau-ttd committed Nov 29, 2023
1 parent 6e55678 commit 7e3708f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/api/configureApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getErrorLoggingMiddleware,
getLoggers,
getLoggingMiddleware,
getTraceId,
} from './helpers/loggingHelpers';
import makeMetricsApiMiddleware from './middleware/metrics';
import { createParticipantsRouter } from './routers/participantsRouter';
Expand Down Expand Up @@ -167,7 +168,7 @@ export function configureAndStartApi(useMetrics: boolean = true) {

app.use(getErrorLoggingMiddleware());
const errorHandler: ErrorRequestHandler = (err, req, res, _next) => {
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
errorLogger.error(`Fallback error handler invoked: ${err.message}`, traceId);
if (err.statusCode === 401) {
res.status(401).json({
Expand Down
5 changes: 5 additions & 0 deletions src/api/helpers/loggingHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Request } from 'express';
import expressWinston from 'express-winston';
import winston from 'winston';
import LokiTransport from 'winston-loki';
Expand Down Expand Up @@ -80,3 +81,7 @@ export const getErrorLoggingMiddleware = () =>
winstonInstance: errorLogger,
headerBlacklist: headersToRedact,
});

export const getTraceId = (request: Request): string => {
return request?.headers?.traceId?.toString() ?? '';
};
15 changes: 8 additions & 7 deletions src/api/routers/participantsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ParticipantStatus,
} from '../entities/Participant';
import { UserDTO, UserRole } from '../entities/User';
import { getTraceId } from '../helpers/loggingHelpers';
import { getKcAdminClient } from '../keycloakAdminClient';
import { isApproverCheck } from '../middleware/approversMiddleware';
import { addKeyPair, getKeyPairsList, getSharingList } from '../services/adminServiceClient';
Expand Down Expand Up @@ -90,7 +91,7 @@ export function createParticipantsRouter() {

participantsRouter.post('/', async (req, res) => {
try {
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
const data = {
...ParticipantCreationPartial.parse(req.body),
status: ParticipantStatus.AwaitingApproval,
Expand Down Expand Up @@ -119,7 +120,7 @@ export function createParticipantsRouter() {
isApproverCheck,
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
const data = {
...ParticipantApprovalPartial.parse(req.body),
status: ParticipantStatus.Approved,
Expand Down Expand Up @@ -202,7 +203,7 @@ export function createParticipantsRouter() {
'/:participantId/sharingPermission',
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
if (!participant?.siteId) {
return res.status(400).send('Site id is not set');
}
Expand All @@ -226,7 +227,7 @@ export function createParticipantsRouter() {
'/:participantId/sharingPermission/add',
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
if (!participant?.siteId) {
return res.status(400).send('Site id is not set');
}
Expand Down Expand Up @@ -261,7 +262,7 @@ export function createParticipantsRouter() {
'/:participantId/keyPair/add',
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
if (!participant?.siteId) {
return res.status(400).send('Site id is not set');
}
Expand Down Expand Up @@ -305,7 +306,7 @@ export function createParticipantsRouter() {
'/:participantId/sharingPermission/delete',
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
if (!participant?.siteId) {
return res.status(400).send('Site id is not set');
}
Expand Down Expand Up @@ -339,7 +340,7 @@ export function createParticipantsRouter() {
'/:participantId/sharingPermission/shareWithTypes',
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
if (!participant?.siteId) {
return res.status(400).send('Site id is not set');
}
Expand Down
4 changes: 2 additions & 2 deletions src/api/routers/usersRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { z } from 'zod';
import { Participant, ParticipantStatus } from '../entities/Participant';
import { ParticipantType } from '../entities/ParticipantType';
import { UserRole } from '../entities/User';
import { getLoggers } from '../helpers/loggingHelpers';
import { getLoggers, getTraceId } from '../helpers/loggingHelpers';
import { mapClientTypeToParticipantType } from '../helpers/siteConvertingHelpers';
import { getKcAdminClient } from '../keycloakAdminClient';
import { getSiteList } from '../services/adminServiceClient';
Expand Down Expand Up @@ -76,7 +76,7 @@ export function createUsersRouter() {

usersRouter.post('/:userId/resendInvitation', async (req: UserRequest, res) => {
const { infoLogger, errorLogger } = getLoggers();
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
const kcAdminClient = await getKcAdminClient();
const user = await queryUsersByEmail(kcAdminClient, req.user?.email || '');

Expand Down
3 changes: 2 additions & 1 deletion src/api/services/participantsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { ParticipantType } from '../entities/ParticipantType';
import { User } from '../entities/User';
import { SSP_WEB_BASE_URL } from '../envars';
import { getTraceId } from '../helpers/loggingHelpers';
import { getSharingList, updateSharingList } from './adminServiceClient';
import { ClientType, SharingListResponse } from './adminServiceHelpers';
import { findApproversByType, getApprovableParticipantTypeIds } from './approversService';
Expand Down Expand Up @@ -168,7 +169,7 @@ const idParser = z.object({

const hasParticipantAccess = async (req: ParticipantRequest, res: Response, next: NextFunction) => {
const { participantId } = idParser.parse(req.params);
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
const participant = await Participant.query().findById(participantId).withGraphFetched('types');
if (!participant) {
return res.status(404).send([{ message: 'The participant cannot be found.' }]);
Expand Down
4 changes: 2 additions & 2 deletions src/api/services/usersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod';

import { Participant } from '../entities/Participant';
import { User, UserDTO } from '../entities/User';
import { getLoggers } from '../helpers/loggingHelpers';
import { getLoggers, getTraceId } from '../helpers/loggingHelpers';
import { isUserAnApprover } from './approversService';

export type UserWithIsApprover = User & { isApprover: boolean };
Expand Down Expand Up @@ -72,7 +72,7 @@ export const enrichWithUserFromParams = async (
next: NextFunction
) => {
const { userId } = userIdParser.parse(req.params);
const traceId = req?.headers?.traceId?.toString() ?? '';
const traceId = getTraceId(req);
const user = await User.query().findById(userId);
if (!user) {
return res.status(404).send([{ message: 'The user cannot be found.' }]);
Expand Down

0 comments on commit 7e3708f

Please sign in to comment.