diff --git a/src/requestTracing/constants.ts b/src/requestTracing/constants.ts index 6f9311b4..7277cd2a 100644 --- a/src/requestTracing/constants.ts +++ b/src/requestTracing/constants.ts @@ -17,6 +17,9 @@ export const NODEJS_DEV_ENV_VAL = "development"; export const ENV_KEY = "Env"; export const DEV_ENV_VAL = "Dev"; +// Node.js Version +export const NODE_VERSION_KEY = "Node"; + // Host Type export const HOST_TYPE_KEY = "Host"; export enum HostType { diff --git a/src/requestTracing/utils.ts b/src/requestTracing/utils.ts index e9949505..9f9c2ccf 100644 --- a/src/requestTracing/utils.ts +++ b/src/requestTracing/utils.ts @@ -23,6 +23,7 @@ import { FEATURE_FILTER_TYPE_KEY, FF_MAX_VARIANTS_KEY, FF_FEATURES_KEY, + NODE_VERSION_KEY, HOST_TYPE_KEY, HostType, KEY_VAULT_CONFIGURED_TAG, @@ -126,6 +127,7 @@ function createCorrelationContextHeader(requestTracingOptions: RequestTracingOpt keyValues.set(REQUEST_TYPE_KEY, requestTracingOptions.initialLoadCompleted ? RequestType.WATCH : RequestType.STARTUP); keyValues.set(HOST_TYPE_KEY, getHostType()); keyValues.set(ENV_KEY, isDevEnvironment() ? DEV_ENV_VAL : undefined); + keyValues.set(NODE_VERSION_KEY, getNodeVersion()); const appConfigOptions = requestTracingOptions.appConfigOptions; if (appConfigOptions?.keyVaultOptions) { @@ -198,7 +200,7 @@ function createFeaturesString(requestTracingOptions: RequestTracingOptions): str return tags.join(DELIMITER); } -function getEnvironmentVariable(name: string) { +function getEnvironmentVariable(name: string): string | undefined { // Make it compatible with non-Node.js runtime if (typeof process !== "undefined" && typeof process?.env === "object") { return process.env[name]; @@ -207,6 +209,16 @@ function getEnvironmentVariable(name: string) { } } +function getNodeVersion(): string | undefined { + // Make it compatible with non-Node.js runtime + if (typeof process !== "undefined" && typeof process?.versions === "object") { + const version = process.versions.node; + return version ? version.split(".")[0] : undefined; + } else { + return undefined; + } +} + function getHostType(): string | undefined { let hostType: string | undefined; if (getEnvironmentVariable(AZURE_FUNCTION_ENV_VAR)) { @@ -235,7 +247,7 @@ function isDevEnvironment(): boolean { return false; } -export function isBrowser() { +export function isBrowser(): boolean { // https://developer.mozilla.org/en-US/docs/Web/API/Window const isWindowDefinedAsExpected = typeof window === "object" && typeof Window === "function" && window instanceof Window; // https://developer.mozilla.org/en-US/docs/Web/API/Document @@ -244,7 +256,7 @@ export function isBrowser() { return isWindowDefinedAsExpected && isDocumentDefinedAsExpected; } -export function isWebWorker() { +export function isWebWorker(): boolean { // https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope const workerGlobalScopeDefined = typeof WorkerGlobalScope !== "undefined"; // https://developer.mozilla.org/en-US/docs/Web/API/WorkerNavigator diff --git a/test/requestTracing.test.ts b/test/requestTracing.test.ts index b61441b1..81226984 100644 --- a/test/requestTracing.test.ts +++ b/test/requestTracing.test.ts @@ -57,7 +57,8 @@ describe("request tracing", function () { }); } catch { /* empty */ } expect(headerPolicy.headers).not.undefined; - expect(headerPolicy.headers.get("Correlation-Context")).eq("RequestType=Startup"); + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext.includes("RequestType=Startup")).eq(true); }); it("should have key vault tag in correlation-context header", async () => {