From d2876e50b09ab0e63bd8dc935cbb3a4d1326e871 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Mon, 13 May 2024 11:26:54 +0800 Subject: [PATCH] chore(core): add custom domain host to app insights --- packages/core/src/utils/request.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/core/src/utils/request.ts b/packages/core/src/utils/request.ts index 73c27ed8c134..403868cf5813 100644 --- a/packages/core/src/utils/request.ts +++ b/packages/core/src/utils/request.ts @@ -1,4 +1,5 @@ import { type ExceptionTelemetry } from '@logto/app-insights/node'; +import { type Context } from 'koa'; // eslint-disable-next-line @typescript-eslint/ban-types const getRequestIdFromContext = (context: object): string | undefined => { @@ -9,13 +10,28 @@ const getRequestIdFromContext = (context: object): string | undefined => { return undefined; }; +const getHostFromContext = (context: Context): string | undefined => { + if ('host' in context.headers && typeof context.headers.host === 'string') { + return context.headers.host; + } + + return undefined; +}; + // eslint-disable-next-line @typescript-eslint/ban-types export const buildAppInsightsTelemetry = (context: object): Partial => { const requestId = getRequestIdFromContext(context); + // eslint-disable-next-line no-restricted-syntax + const host = getHostFromContext(context as Context); - if (requestId) { - return { properties: { requestId } }; + if (!requestId && !host) { + return {}; } - return {}; + return { + properties: { + ...(requestId ? { requestId } : {}), + ...(host ? { host } : {}), + }, + }; };