Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export default [
plugins: [
typescript({
compilerOptions: {
"lib": [
"DOM",
"WebWorker",
"ESNext"
],
"skipDefaultLibCheck": true,
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2022",
Expand Down
11 changes: 4 additions & 7 deletions src/AzureAppConfigurationImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter";
import { KeyFilter } from "./KeyFilter";
import { LabelFilter } from "./LabelFilter";
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter";
import { CorrelationContextHeaderName, RequestTracingDisabledEnvironmentVariable } from "./requestTracing/constants";
import { createCorrelationContextHeader } from "./requestTracing/utils";
import { CorrelationContextHeaderName } from "./requestTracing/constants";
import { createCorrelationContextHeader, requestTracingEnabled } from "./requestTracing/utils";

export class AzureAppConfigurationImpl extends Map<string, unknown> implements AzureAppConfiguration {
private adapters: IKeyValueAdapter[] = [];
Expand All @@ -28,11 +28,8 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
) {
super();
// Enable request tracing if not opt-out
const requestTracingDisabledEnv = process.env[RequestTracingDisabledEnvironmentVariable];
if (requestTracingDisabledEnv && requestTracingDisabledEnv.toLowerCase() === "true") {
this.requestTracingEnabled = false;
} else {
this.requestTracingEnabled = true;
this.requestTracingEnabled = requestTracingEnabled();
if (this.requestTracingEnabled) {
this.enableRequestTracing();
}

Expand Down
5 changes: 4 additions & 1 deletion src/requestTracing/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export enum HostType {
AzureWebApp = "AzureWebApp",
ContainerApp = "ContainerApp",
Kubernetes = "Kubernetes",
ServiceFabric = "ServiceFabric"
ServiceFabric = "ServiceFabric",
// Client-side
Browser = "Web",
WebWorker = "WebWorker"
}

// Environment variables to identify Host type.
Expand Down
52 changes: 46 additions & 6 deletions src/requestTracing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
KubernetesEnvironmentVariable,
NodeJSDevEnvironmentVariableValue,
NodeJSEnvironmentVariable,
RequestTracingDisabledEnvironmentVariable,
RequestType,
RequestTypeKey,
ServiceFabricEnvironmentVariable
Expand Down Expand Up @@ -53,26 +54,65 @@ export function createCorrelationContextHeader(options: AzureAppConfigurationOpt
return contextParts.join(",");
}

export function requestTracingEnabled(): boolean {
const requestTracingDisabledEnv = getEnvironmentVariable(RequestTracingDisabledEnvironmentVariable);
const disabled = requestTracingDisabledEnv?.toLowerCase() === "true";
return !disabled;
}

function getEnvironmentVariable(name: string) {
// Make it compatible with non-Node.js runtime
if (typeof process?.env === "object") {
return process.env[name];
} else {
return undefined;
}
}

function getHostType(): string | undefined {
let hostType: string | undefined;
if (process.env[AzureFunctionEnvironmentVariable]) {
if (getEnvironmentVariable(AzureFunctionEnvironmentVariable)) {
hostType = HostType.AzureFunction;
} else if (process.env[AzureWebAppEnvironmentVariable]) {
} else if (getEnvironmentVariable(AzureWebAppEnvironmentVariable)) {
hostType = HostType.AzureWebApp;
} else if (process.env[ContainerAppEnvironmentVariable]) {
} else if (getEnvironmentVariable(ContainerAppEnvironmentVariable)) {
hostType = HostType.ContainerApp;
} else if (process.env[KubernetesEnvironmentVariable]) {
} else if (getEnvironmentVariable(KubernetesEnvironmentVariable)) {
hostType = HostType.Kubernetes;
} else if (process.env[ServiceFabricEnvironmentVariable]) {
} else if (getEnvironmentVariable(ServiceFabricEnvironmentVariable)) {
hostType = HostType.ServiceFabric;
} else if (isBrowser()) {
hostType = HostType.Browser;
} else if (isWebWorker()) {
hostType = HostType.WebWorker;
}
return hostType;
}

function isDevEnvironment(): boolean {
const envType = process.env[NodeJSEnvironmentVariable];
const envType = getEnvironmentVariable(NodeJSEnvironmentVariable);
if (NodeJSDevEnvironmentVariableValue === envType?.toLowerCase()) {
return true;
}
return false;
}

function isBrowser() {
// 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
const isDocumentDefinedAsExpected = typeof document === "object" && typeof Document === "function" && document instanceof Document;

return isWindowDefinedAsExpected && isDocumentDefinedAsExpected;
}

function isWebWorker() {
// 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
const isNavigatorDefinedAsExpected = typeof navigator === "object" && typeof WorkerNavigator !== "function" && navigator instanceof WorkerNavigator;
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#importing_scripts_and_libraries
const importScriptsAsGlobalFunction = typeof importScripts === "function";

return workerGlobalScopeDefined && importScriptsAsGlobalFunction && isNavigatorDefinedAsExpected;
}
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"compilerOptions": {
"lib": [
"DOM",
"WebWorker",
"ESNext"
],
"skipDefaultLibCheck": true,
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2022",
Expand Down