Skip to content

Commit

Permalink
Add log info to middleware ctx, change logging in services (#183)
Browse files Browse the repository at this point in the history
* Add log info to middleware ctx, change loggin in services

* Change msg log

* Fix req context log
  • Loading branch information
Sergey-weber authored Sep 18, 2024
1 parent 58c00b4 commit c247d22
Show file tree
Hide file tree
Showing 56 changed files with 148 additions and 159 deletions.
1 change: 0 additions & 1 deletion api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export {
default as Utils,
logInfo,
makeUserId,
isTenantIdWithOrgId,
getOrgIdFromTenantId,
Expand Down
11 changes: 10 additions & 1 deletion src/components/middlewares/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export const ctx = async (req: Request, res: Response, next: NextFunction) => {
req.headers[DL_COMPONENT_HEADER] as string,
);

const user = {userId, login};

req.originalContext.set('info', {
requestId: req.id,
tenantId,
workbookId,
user: {userId, login},
user,
isPrivateRoute,
dlContext,
onlyPublic,
Expand All @@ -34,5 +36,12 @@ export const ctx = async (req: Request, res: Response, next: NextFunction) => {
zitadelUserRole,
});

req.ctx.log('REQUEST_INFO', {
ctxTenantId: tenantId,
ctxProjectId: projectId,
requestedBy: user,
dlContext,
});

next();
};
6 changes: 3 additions & 3 deletions src/db/models/entry/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {transaction} from 'objection';
import {Model, getId} from '../..';
import Utils, {logInfo, makeUserId} from '../../../utils';
import Utils, {makeUserId} from '../../../utils';
import Revision from '../revision';
import Tenant from '../tenant';
import Link from '../links';
Expand Down Expand Up @@ -519,7 +519,7 @@ class Entry extends Model {
return allCreatedEntries;
});

logInfo(ctx, BiTrackingLogs.CreateEntry, {
ctx.log(BiTrackingLogs.CreateEntry, {
entryId: result && !Array.isArray(result) ? Utils.encodeId(result.entryId) : null,
});

Expand Down Expand Up @@ -643,7 +643,7 @@ class Entry extends Model {
return entries;
});

logInfo(ctx, BiTrackingLogs.PrivateCreateEntry, {
ctx.log(BiTrackingLogs.PrivateCreateEntry, {
entryId: result && !Array.isArray(result) ? Utils.encodeId(result.entryId) : null,
});

Expand Down
8 changes: 4 additions & 4 deletions src/services/color-palettes/create-color-palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {transaction} from 'objection';
import {ServiceArgs} from '../../services/new/types';
import {makeSchemaValidator} from '../../components/validation-schema-compiler';
import {ColorPaletteModel, ColorPaletteModelColumn} from '../../db/models/new/color-palette';
import Utils, {logInfo} from '../../utils';
import Utils from '../../utils';
import {registry} from '../../registry';
import {getColorPalettesCount} from './get-color-palettes-count';
import {US_ERRORS} from '../../const';
Expand Down Expand Up @@ -46,7 +46,7 @@ export const createColorPalette = async (
) => {
const {displayName, colors, isGradient, isDefault} = args;

logInfo(ctx, 'CREATE_COLOR_PALETTE_START', {
ctx.log('CREATE_COLOR_PALETTE_START', {
displayName,
colors,
isGradient,
Expand Down Expand Up @@ -84,7 +84,7 @@ export const createColorPalette = async (

const {tenantId} = ctx.get('info');

logInfo(ctx, 'CREATE_COLOR_PALETTE_IN_DB_START');
ctx.log('CREATE_COLOR_PALETTE_IN_DB_START');

const result = await transaction(ColorPaletteModel.primary, async (transactionTrx) => {
if (isDefault) {
Expand Down Expand Up @@ -112,7 +112,7 @@ export const createColorPalette = async (
.returning('*')
.timeout(ColorPaletteModel.DEFAULT_QUERY_TIMEOUT);

logInfo(ctx, 'CREATE_COLOR_PALETTE_IN_DB_FINISH', {
ctx.log('CREATE_COLOR_PALETTE_IN_DB_FINISH', {
colorPaletteId: Utils.encodeId(model[ColorPaletteModelColumn.ColorPaletteId]),
});

Expand Down
5 changes: 2 additions & 3 deletions src/services/color-palettes/delete-color-palette.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ServiceArgs} from '../../services/new/types';
import {makeSchemaValidator} from '../../components/validation-schema-compiler';
import {ColorPaletteModel, ColorPaletteModelColumn} from '../../db/models/new/color-palette';
import {logInfo} from '../../utils';
import {registry} from '../../registry';

const validateArgs = makeSchemaValidator({
Expand All @@ -24,7 +23,7 @@ export const deleteColorPalette = async (
) => {
const {colorPaletteId} = args;

logInfo(ctx, 'DELETE_COLOR_PALETTE_START', {
ctx.log('DELETE_COLOR_PALETTE_START', {
colorPaletteId,
});

Expand All @@ -45,7 +44,7 @@ export const deleteColorPalette = async (
.delete()
.timeout(ColorPaletteModel.DEFAULT_QUERY_TIMEOUT);

logInfo(ctx, 'DELETE_COLOR_PALETTE_FINISH', {
ctx.log('DELETE_COLOR_PALETTE_FINISH', {
colorPaletteId,
});
};
3 changes: 1 addition & 2 deletions src/services/color-palettes/get-color-palette.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {ServiceArgs} from '../../services/new/types';
import {ColorPaletteModel, ColorPaletteModelColumn} from '../../db/models/new/color-palette';
import {logInfo} from '../../utils';

type GetColorPaletteArgs = {
colorPaletteId: string;
Expand All @@ -10,7 +9,7 @@ export const getColorPalette = async (
{ctx}: ServiceArgs,
{colorPaletteId}: GetColorPaletteArgs,
) => {
logInfo(ctx, 'GET_COLOR_PALETTE_START', {
ctx.log('GET_COLOR_PALETTE_START', {
colorPaletteId,
});

Expand Down
6 changes: 3 additions & 3 deletions src/services/color-palettes/get-color-palettes-count.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {CountAggregation, ServiceArgs} from '../../services/new/types';
import {getReplica} from '../../services/new/utils';
import {makeSchemaValidator} from '../../components/validation-schema-compiler';
import {logInfo} from '../../utils';

import {ColorPaletteModel, ColorPaletteModelColumn} from '../../db/models/new/color-palette';

const validateArgs = makeSchemaValidator({
Expand All @@ -24,7 +24,7 @@ export const getColorPalettesCount = async (
) => {
const {isGradient} = args;

logInfo(ctx, 'GET_COLOR_PALETTE_COUNT_START', {
ctx.log('GET_COLOR_PALETTE_COUNT_START', {
isGradient,
});

Expand All @@ -48,7 +48,7 @@ export const getColorPalettesCount = async (

const result = Number(count?.count || 0);

logInfo(ctx, 'GET_COLOR_PALETTE_COUNT_FINISH', {result});
ctx.log('GET_COLOR_PALETTE_COUNT_FINISH', {result});

return result;
};
6 changes: 3 additions & 3 deletions src/services/color-palettes/get-color-palettes-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ServiceArgs} from '../../services/new/types';
import {ColorPaletteModel, ColorPaletteModelColumn} from '../../db/models/new/color-palette';
import Utils, {logInfo} from '../../utils';
import Utils from '../../utils';

const PAGE = 0;

Expand All @@ -18,7 +18,7 @@ export const getColorPalettesList = async (
) => {
const {tenantId} = ctx.get('info');

logInfo(ctx, 'GET_COLOR_PALETTES_LIST_START', {
ctx.log('GET_COLOR_PALETTES_LIST_START', {
tenantId,
filters,
});
Expand All @@ -30,7 +30,7 @@ export const getColorPalettesList = async (
try {
decodedPaletteId = Utils.decodeId(colorPaletteId);
} catch {
logInfo(ctx, 'GET_COLOR_PALETTES_LIST_FAILED_DECODING');
ctx.log('GET_COLOR_PALETTES_LIST_FAILED_DECODING');

return [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/color-palettes/update-color-palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ServiceArgs} from '../../services/new/types';
import {getPrimary} from '../../services/new/utils';
import {makeSchemaValidator} from '../../components/validation-schema-compiler';
import {WorkbookModel} from '../../db/models/new/workbook';
import Utils, {logInfo} from '../../utils';
import Utils from '../../utils';
import {US_ERRORS} from '../../const';
import {ColorPaletteModel, ColorPaletteModelColumn} from '../../db/models/new/color-palette';
import {transaction} from 'objection';
Expand Down Expand Up @@ -48,7 +48,7 @@ export const updateColorPalette = async (
) => {
const {colorPaletteId, displayName, colors, isGradient, isDefault} = args;

logInfo(ctx, 'UPDATE_COLOR_PALETTE_START', {
ctx.log('UPDATE_COLOR_PALETTE_START', {
colorPaletteId: Utils.encodeId(colorPaletteId),
displayName,
colors,
Expand Down Expand Up @@ -104,7 +104,7 @@ export const updateColorPalette = async (
});
}

logInfo(ctx, 'UPDATE_COLOR_PALETTE_FINISH', {
ctx.log('UPDATE_COLOR_PALETTE_FINISH', {
colorPaletteId: Utils.encodeId(result.colorPaletteId),
});

Expand Down
6 changes: 3 additions & 3 deletions src/services/entry/actions/copy-to-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {JoinedEntryRevision} from '../../../db/presentations/joined-entry-revisi
import {WorkbookModel} from '../../../db/models/new/workbook';
import {CTX} from '../../../types/models';
import {US_ERRORS, BiTrackingLogs} from '../../../const';
import Utils, {logInfo, makeUserId} from '../../../utils';
import Utils, {makeUserId} from '../../../utils';
import {registry} from '../../../registry';
import {WorkbookPermission} from '../../../entities/workbook';
import {getParentIds} from '../../new/collection/utils/get-parents';
Expand Down Expand Up @@ -69,7 +69,7 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => {
isMigrateCopiedEntries,
} = params;

logInfo(ctx, 'COPY_ENTRY_TO_WORKBOOK_CALL', {
ctx.log('COPY_ENTRY_TO_WORKBOOK_CALL', {
entryIds: await Utils.macrotasksMap(entryIds, (entryId) => Utils.encodeId(entryId)),
destinationWorkbookId: Utils.encodeId(destinationWorkbookId),
tenantIdOverride,
Expand Down Expand Up @@ -333,7 +333,7 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => {
});

result.forEach(({newJoinedEntryRevision}) => {
logInfo(ctx, BiTrackingLogs.CopyEntry, {
ctx.log(BiTrackingLogs.CopyEntry, {
entryId: Utils.encodeId(newJoinedEntryRevision.entryId),
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/services/entry/actions/create-in-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import {getWorkbook} from '../../new/workbook/get-workbook';
import {checkWorkbookPermission, getEntryPermissionsByWorkbook} from '../../new/workbook/utils';
import {WorkbookPermission} from '../../../entities/workbook';
import Utils, {logInfo, makeUserId} from '../../../utils';
import Utils, {makeUserId} from '../../../utils';
import {getId} from '../../../db';

export const validateCreateEntryInWorkbook = makeSchemaValidator({
Expand Down Expand Up @@ -107,7 +107,7 @@ export async function createEntryInWorkbook(
includePermissionsInfo,
}: CreateEntryInWorkbookData,
) {
logInfo(ctx, 'CREATE_ENTRY_IN_WORKBOOK_CALL');
ctx.log('CREATE_ENTRY_IN_WORKBOOK_CALL');

validateCreateEntryInWorkbook({
workbookId,
Expand Down Expand Up @@ -208,7 +208,7 @@ export async function createEntryInWorkbook(
resultEntry.permissions = permissions;
}

logInfo(ctx, BiTrackingLogs.CreateEntry, {
ctx.log(BiTrackingLogs.CreateEntry, {
entryId: Utils.encodeId(resultEntry.entryId),
});

Expand Down
6 changes: 3 additions & 3 deletions src/services/entry/actions/delete-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Lock from '../../../db/models/lock';
import {EntryColumns, DlsActions, UsPermissions} from '../../../types/models';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {RETURN_COLUMNS, BiTrackingLogs, DEFAULT_QUERY_TIMEOUT, US_ERRORS} from '../../../const';
import Utils, {logInfo, makeUserId} from '../../../utils';
import Utils, {makeUserId} from '../../../utils';
import {getWorkbook} from '../../new/workbook/get-workbook';
import {WorkbookPermission} from '../../../entities/workbook';
import {checkWorkbookPermission} from '../../new/workbook/utils';
Expand Down Expand Up @@ -42,7 +42,7 @@ export async function deleteEntry(
) {
const {entryId, lockToken, useLegacyLogin = false} = args;

logInfo(ctx, 'DELETE_ENTRY_REQUEST', {
ctx.log('DELETE_ENTRY_REQUEST', {
entryId: Utils.encodeId(entryId),
lockToken,
});
Expand Down Expand Up @@ -198,7 +198,7 @@ export async function deleteEntry(
.timeout(DEFAULT_QUERY_TIMEOUT);
});

logInfo(ctx, BiTrackingLogs.DeleteEntry, {
ctx.log(BiTrackingLogs.DeleteEntry, {
entryId: Utils.encodeId(entryId),
});

Expand Down
3 changes: 1 addition & 2 deletions src/services/entry/actions/entries-by-key-pattern.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Entry from '../../../db/models/entry';
import {logInfo} from '../../../utils';
import {DEFAULT_QUERY_TIMEOUT} from '../../../const';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {ServiceArgs} from '../../new/types';
Expand All @@ -26,7 +25,7 @@ export async function getEntriesByKeyPattern(
) {
const {keyPattern} = args;

logInfo(ctx, 'ENTRIES_BY_KEY_PATTERN_CALL', {keyPattern});
ctx.log('ENTRIES_BY_KEY_PATTERN_CALL', {keyPattern});

if (!skipValidation) {
validateArgs(args);
Expand Down
4 changes: 2 additions & 2 deletions src/services/entry/actions/get-entry-by-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Entry from '../../../db/models/entry';
import {Optional as OptionalFields} from 'utility-types';
import {CTX, DlsActions, RevisionColumns} from '../../../types/models';
import {RETURN_COLUMNS, DEFAULT_QUERY_TIMEOUT, US_ERRORS} from '../../../const';
import Utils, {logInfo} from '../../../utils';
import Utils from '../../../utils';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {checkEntry} from './check-entry';
import {registry} from '../../../registry';
Expand Down Expand Up @@ -56,7 +56,7 @@ export async function getEntryByKey(
const {tenantId, isPrivateRoute: infoIsPrivateRoute} = ctx.get('info');
const isPrivateRoute = customIsPrivateRoute || infoIsPrivateRoute;

logInfo(ctx, 'GET_ENTRY_BY_KEY_REQUEST', {
ctx.log('GET_ENTRY_BY_KEY_REQUEST', {
key,
revId,
branch,
Expand Down
4 changes: 2 additions & 2 deletions src/services/entry/actions/get-entry-relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {AppError} from '@gravity-ui/nodekit';
import {Entry, EntryColumn} from '../../../db/models/new/entry';
import {DlsActions} from '../../../types/models';
import {ServiceArgs} from '../../new/types';
import Utils, {logInfo} from '../../../utils';
import Utils from '../../../utils';
import {US_ERRORS} from '../../../const';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {getWorkbook} from '../../new/workbook/get-workbook';
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function getEntryRelations(

const {entryId, direction = RelationDirection.Parent, includePermissionsInfo = false} = args;

logInfo(ctx, 'GET_ENTRY_RELATIONS_REQUEST', {
ctx.log('GET_ENTRY_RELATIONS_REQUEST', {
entryId: Utils.encodeId(entryId),
direction,
includePermissionsInfo,
Expand Down
6 changes: 3 additions & 3 deletions src/services/entry/actions/get-entry-revisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DEFAULT_PAGE,
DEFAULT_PAGE_SIZE,
} from '../../../const';
import Utils, {logInfo} from '../../../utils';
import Utils from '../../../utils';
import {checkEntry} from './check-entry';
import {getWorkbook} from '../../new/workbook';
import {ServiceArgs} from '../../new/types';
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function getEntryRevisions(
) {
const {entryId, page = DEFAULT_PAGE, pageSize = DEFAULT_PAGE_SIZE, revIds, updatedAfter} = args;

logInfo(ctx, 'GET_REVISIONS_REQUEST', {
ctx.log('GET_REVISIONS_REQUEST', {
entryId: Utils.encodeId(entryId),
page,
pageSize,
Expand Down Expand Up @@ -157,7 +157,7 @@ type GetLegacyEntryRevisionsData = {
};

export async function getLegacyEntryRevisions(ctx: CTX, {entryId}: GetLegacyEntryRevisionsData) {
logInfo(ctx, 'GET_LEGACY_REVISIONS_REQUEST', {
ctx.log('GET_LEGACY_REVISIONS_REQUEST', {
entryId: Utils.encodeId(entryId),
});

Expand Down
Loading

0 comments on commit c247d22

Please sign in to comment.