From 062d21764c9db33247528fd034f3befd0a4b182f Mon Sep 17 00:00:00 2001 From: wangsijie Date: Mon, 13 May 2024 21:04:18 +0800 Subject: [PATCH] chore(core): add custom domain host to app insights (#5852) --- packages/core/src/utils/request.ts | 20 ++++++++++++++++---- packages/core/src/utils/test-utils.ts | 1 + 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/core/src/utils/request.ts b/packages/core/src/utils/request.ts index 73c27ed8c13..86215a76f10 100644 --- a/packages/core/src/utils/request.ts +++ b/packages/core/src/utils/request.ts @@ -1,21 +1,33 @@ 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 => { if ('requestId' in context && typeof context.requestId === 'string') { return context.requestId; } +}; - return undefined; +const getHostFromContext = (context: Context): string | undefined => { + if ('host' in context.headers && typeof context.headers.host === 'string') { + return context.headers.host; + } }; // 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 } : {}), + }, + }; }; diff --git a/packages/core/src/utils/test-utils.ts b/packages/core/src/utils/test-utils.ts index 98ea31c5ace..6a0a1be408d 100644 --- a/packages/core/src/utils/test-utils.ts +++ b/packages/core/src/utils/test-utils.ts @@ -97,6 +97,7 @@ export const createContextWithRouteParameters = ( path: ctx.path, URL: ctx.URL, params: {}, + headers: {}, router: new Router(), _matchedRoute: undefined, _matchedRouteName: undefined,