diff --git a/README.md b/README.md index e7e30d16..f0da024d 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,7 @@ trademarks or logos is subject to and must follow [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies. + +# Data Collection + +The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry by setting the environment variable `AZURE_APP_CONFIGURATION_TRACING_DISABLED` to `TRUE`. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices. \ No newline at end of file diff --git a/src/AzureAppConfigurationImpl.ts b/src/AzureAppConfigurationImpl.ts index 07889b1f..19a93d20 100644 --- a/src/AzureAppConfigurationImpl.ts +++ b/src/AzureAppConfigurationImpl.ts @@ -1,14 +1,16 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { AppConfigurationClient, ConfigurationSetting } from "@azure/app-configuration"; +import { AppConfigurationClient, ConfigurationSetting, ListConfigurationSettingsOptions } from "@azure/app-configuration"; import { AzureAppConfiguration } from "./AzureAppConfiguration"; import { AzureAppConfigurationOptions } from "./AzureAppConfigurationOptions"; import { IKeyValueAdapter } from "./IKeyValueAdapter"; +import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter"; import { KeyFilter } from "./KeyFilter"; import { LabelFilter } from "./LabelFilter"; import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter"; -import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter"; +import { CorrelationContextHeaderName, RequestTracingDisabledEnvironmentVariable } from "./requestTracing/constants"; +import { createCorrelationContextHeader } from "./requestTracing/utils"; export class AzureAppConfigurationImpl extends Map implements AzureAppConfiguration { private adapters: IKeyValueAdapter[] = []; @@ -17,12 +19,23 @@ export class AzureAppConfigurationImpl extends Map implements A * Since multiple prefixes could start with the same characters, we need to trim the longest prefix first. */ private sortedTrimKeyPrefixes: string[] | undefined; + private readonly requestTracingEnabled: boolean; + private correlationContextHeader: string | undefined; constructor( private client: AppConfigurationClient, private options: AzureAppConfigurationOptions | undefined ) { 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.enableRequestTracing(); + } + if (options?.trimKeyPrefixes) { this.sortedTrimKeyPrefixes = [...options.trimKeyPrefixes].sort((a, b) => b.localeCompare(a)); } @@ -36,10 +49,17 @@ export class AzureAppConfigurationImpl extends Map implements A const keyValues: [key: string, value: unknown][] = []; const selectors = this.options?.selectors ?? [{ keyFilter: KeyFilter.Any, labelFilter: LabelFilter.Null }]; for (const selector of selectors) { - const settings = this.client.listConfigurationSettings({ + const listOptions: ListConfigurationSettingsOptions = { keyFilter: selector.keyFilter, labelFilter: selector.labelFilter - }); + }; + if (this.requestTracingEnabled) { + listOptions.requestOptions = { + customHeaders: this.customHeaders() + } + } + + const settings = this.client.listConfigurationSettings(listOptions); for await (const setting of settings) { if (setting.key) { @@ -55,7 +75,7 @@ export class AzureAppConfigurationImpl extends Map implements A } private async processAdapters(setting: ConfigurationSetting): Promise<[string, unknown]> { - for(const adapter of this.adapters) { + for (const adapter of this.adapters) { if (adapter.canProcess(setting)) { return adapter.processKeyValue(setting); } @@ -73,4 +93,18 @@ export class AzureAppConfigurationImpl extends Map implements A } return key; } + + private enableRequestTracing() { + this.correlationContextHeader = createCorrelationContextHeader(this.options); + } + + private customHeaders() { + if (!this.requestTracingEnabled) { + return undefined; + } + + const headers = {}; + headers[CorrelationContextHeaderName] = this.correlationContextHeader; + return headers; + } } diff --git a/src/index.ts b/src/index.ts index d1d7cdc2..b2dc467b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -export { load } from "./Load"; +export { load } from "./load"; export { AzureAppConfiguration } from "./AzureAppConfiguration"; export { KeyFilter } from "./KeyFilter"; export { LabelFilter } from "./LabelFilter"; \ No newline at end of file diff --git a/src/Load.ts b/src/load.ts similarity index 83% rename from src/Load.ts rename to src/load.ts index e60417f0..dbb64196 100644 --- a/src/Load.ts +++ b/src/load.ts @@ -6,6 +6,7 @@ import { TokenCredential } from "@azure/identity"; import { AzureAppConfiguration } from "./AzureAppConfiguration"; import { AzureAppConfigurationImpl } from "./AzureAppConfigurationImpl"; import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./AzureAppConfigurationOptions"; +import * as RequestTracing from "./requestTracing/constants"; export async function load(connectionString: string, options?: AzureAppConfigurationOptions): Promise; export async function load(endpoint: URL | string, credential: TokenCredential, options?: AzureAppConfigurationOptions): Promise; @@ -38,7 +39,7 @@ export async function load( const credential = credentialOrOptions as TokenCredential; options = appConfigOptions; const clientOptions = getClientOptions(options); - client = new AppConfigurationClient(endpoint.toString(), credential, clientOptions) + client = new AppConfigurationClient(endpoint.toString(), credential, clientOptions); } else { throw new Error("A connection string or an endpoint with credential must be specified to create a client."); } @@ -53,15 +54,24 @@ function instanceOfTokenCredential(obj: unknown) { } function getClientOptions(options?: AzureAppConfigurationOptions): AppConfigurationClientOptions | undefined { - // TODO: user-agent - // TODO: set correlation context using additional policies + // user-agent + let userAgentPrefix = RequestTracing.UserAgentPrefix; // Default UA for JavaScript Provider + const userAgentOptions = options?.clientOptions?.userAgentOptions; + if (userAgentOptions?.userAgentPrefix) { + userAgentPrefix = `${userAgentOptions.userAgentPrefix} ${userAgentPrefix}`; // Prepend if UA prefix specified by user + } + // retry options const defaultRetryOptions = { maxRetries: MaxRetries, maxRetryDelayInMs: MaxRetryDelayInMs, } const retryOptions = Object.assign({}, defaultRetryOptions, options?.clientOptions?.retryOptions); + return Object.assign({}, options?.clientOptions, { - retryOptions + retryOptions, + userAgentOptions: { + userAgentPrefix + } }); } diff --git a/src/requestTracing/constants.ts b/src/requestTracing/constants.ts new file mode 100644 index 00000000..412f74c7 --- /dev/null +++ b/src/requestTracing/constants.ts @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { Version } from "../version"; + +export const RequestTracingDisabledEnvironmentVariable = "AZURE_APP_CONFIGURATION_TRACING_DISABLED"; + +// User Agent +export const UserAgentPrefix = `javascript-appconfiguration-provider/${Version}`; + +// Correlation Context +export const CorrelationContextHeaderName = "Correlation-Context"; + +// Env +export const NodeJSEnvironmentVariable = "NODE_ENV"; +export const NodeJSDevEnvironmentVariableValue = "development"; +export const EnvironmentKey = "Env"; +export const DevEnvironmentValue = "Dev"; + +// Host Type +export const HostTypeKey = "Host"; +export enum HostType { + AzureFunction = "AzureFunction", + AzureWebApp = "AzureWebApp", + ContainerApp = "ContainerApp", + Kubernetes = "Kubernetes", + ServiceFabric = "ServiceFabric" +} + +// Environment variables to identify Host type. +export const AzureFunctionEnvironmentVariable = "FUNCTIONS_EXTENSION_VERSION"; +export const AzureWebAppEnvironmentVariable = "WEBSITE_SITE_NAME"; +export const ContainerAppEnvironmentVariable = "CONTAINER_APP_NAME"; +export const KubernetesEnvironmentVariable = "KUBERNETES_PORT"; +export const ServiceFabricEnvironmentVariable = "Fabric_NodeName"; // See: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference + +// Request Type +export const RequestTypeKey = "RequestType"; +export enum RequestType { + Startup = "Startup", + Watch = "Watch" +} + +// Tag names +export const KeyVaultConfiguredTag = "UsesKeyVault"; diff --git a/src/requestTracing/utils.ts b/src/requestTracing/utils.ts new file mode 100644 index 00000000..29ec0a39 --- /dev/null +++ b/src/requestTracing/utils.ts @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { AzureAppConfigurationOptions } from "../AzureAppConfigurationOptions"; +import { + AzureFunctionEnvironmentVariable, + AzureWebAppEnvironmentVariable, + ContainerAppEnvironmentVariable, + DevEnvironmentValue, + EnvironmentKey, + HostType, + HostTypeKey, + KeyVaultConfiguredTag, + KubernetesEnvironmentVariable, + NodeJSDevEnvironmentVariableValue, + NodeJSEnvironmentVariable, + RequestType, + RequestTypeKey, + ServiceFabricEnvironmentVariable +} from "./constants"; + +// Utils +export function createCorrelationContextHeader(options: AzureAppConfigurationOptions | undefined): string { + /* + RequestType: 'Startup' during application starting up, 'Watch' after startup completed. + Host: identify with defined envs + Env: identify by env `NODE_ENV` which is a popular but not standard.usually the value can be "development", "production". + UsersKeyVault + */ + const keyValues = new Map(); + keyValues.set(RequestTypeKey, RequestType.Startup); // TODO: now always "Startup", until refresh is supported. + keyValues.set(HostTypeKey, getHostType()); + keyValues.set(EnvironmentKey, isDevEnvironment() ? DevEnvironmentValue : undefined); + + const tags: string[] = []; + if (options?.keyVaultOptions) { + const { credential, secretClients, secretResolver } = options.keyVaultOptions; + if (credential !== undefined || secretClients?.length || secretResolver !== undefined) { + tags.push(KeyVaultConfiguredTag); + } + } + + const contextParts: string[] = []; + for (const [k, v] of keyValues) { + if (v !== undefined) { + contextParts.push(`${k}=${v}`); + } + } + for (const tag of tags) { + contextParts.push(tag); + } + + return contextParts.join(","); +} + +function getHostType(): string | undefined { + let hostType: string | undefined; + if (process.env[AzureFunctionEnvironmentVariable]) { + hostType = HostType.AzureFunction; + } else if (process.env[AzureWebAppEnvironmentVariable]) { + hostType = HostType.AzureWebApp; + } else if (process.env[ContainerAppEnvironmentVariable]) { + hostType = HostType.ContainerApp; + } else if (process.env[KubernetesEnvironmentVariable]) { + hostType = HostType.Kubernetes; + } else if (process.env[ServiceFabricEnvironmentVariable]) { + hostType = HostType.ServiceFabric; + } + return hostType; +} + +function isDevEnvironment(): boolean { + const envType = process.env[NodeJSEnvironmentVariable]; + if (NodeJSDevEnvironmentVariableValue === envType?.toLowerCase()) { + return true; + } + return false; +} \ No newline at end of file diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 00000000..6e2c2c5f --- /dev/null +++ b/src/version.ts @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +export const Version = "0.1.0-preview"; \ No newline at end of file diff --git a/test/requestTracing.test.js b/test/requestTracing.test.js new file mode 100644 index 00000000..533cd7a3 --- /dev/null +++ b/test/requestTracing.test.js @@ -0,0 +1,118 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +const chai = require("chai"); +const chaiAsPromised = require("chai-as-promised"); +chai.use(chaiAsPromised); +const expect = chai.expect; +const { load } = require("../dist/index"); +const { createMockedConnectionString, createMockedTokenCredential } = require("./utils/testHelper"); + +class HttpRequestHeadersPolicy { + constructor() { + this.headers = {}; + this.name = "HttpRequestHeadersPolicy"; + } + sendRequest(req, next) { + this.headers = req.headers; + return next(req).then(resp => resp); + } +} + +describe("request tracing", function () { + const fakeEndpoint = "https://127.0.0.1"; // sufficient to test the request it sends out + const headerPolicy = new HttpRequestHeadersPolicy(); + const clientOptions = { + retryOptions: { + maxRetries: 0 // save time + }, + additionalPolicies: [{ + policy: headerPolicy, + position: "perCall" + }] + }; + + before(() => { + }); + + after(() => { + }) + + it("should have correct user agent prefix", async () => { + try { + await load(createMockedConnectionString(fakeEndpoint), { clientOptions }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + expect(headerPolicy.headers.get("User-Agent")).satisfy(ua => ua.startsWith("javascript-appconfiguration-provider")); + }); + + it("should have request type in correlation-context header", async () => { + try { + await load(createMockedConnectionString(fakeEndpoint), { + clientOptions + }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + expect(headerPolicy.headers.get("Correlation-Context")).eq("RequestType=Startup"); + }); + + it("should have key vault tag in correlation-context header", async () => { + try { + await load(createMockedConnectionString(fakeEndpoint), { + clientOptions, + keyVaultOptions: { + credential: createMockedTokenCredential() + } + }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("UsesKeyVault")).eq(true); + }); + + it("should detect env in correlation-context header", async () => { + process.env.NODE_ENV = "development"; + try { + await load(createMockedConnectionString(fakeEndpoint), { + clientOptions + }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Env=Dev")).eq(true); + delete process.env.NODE_ENV; + }); + + it("should detect host type in correlation-context header", async () => { + process.env.WEBSITE_SITE_NAME = "website-name"; + try { + await load(createMockedConnectionString(fakeEndpoint), { + clientOptions + }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).not.undefined; + expect(correlationContext.includes("Host=AzureWebApp")).eq(true); + delete process.env.WEBSITE_SITE_NAME; + }); + + it("should disable request tracing when AZURE_APP_CONFIGURATION_TRACING_DISABLED is true", async() => { + for (const indicator of ["TRUE", "true"]) { + process.env.AZURE_APP_CONFIGURATION_TRACING_DISABLED = indicator; + try { + await load(createMockedConnectionString(fakeEndpoint), { + clientOptions + }); + } catch (e) { /* empty */ } + expect(headerPolicy.headers).not.undefined; + const correlationContext = headerPolicy.headers.get("Correlation-Context"); + expect(correlationContext).undefined; + } + + // clean up + delete process.env.AZURE_APP_CONFIGURATION_TRACING_DISABLED; + }); +}); \ No newline at end of file