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
51 changes: 34 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"vitest": "^4.1.8"
},
"dependencies": {
"@azure/app-configuration": "^1.11.0",
"@azure/app-configuration": "^1.12.1",
"@azure-rest/core-client": "^2.6.0",
"@azure/core-rest-pipeline": "^1.6.0",
"@azure/identity": "^4.2.1",
"@azure/keyvault-secrets": "^4.7.0",
Expand Down
6 changes: 4 additions & 2 deletions src/requestTracing/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-client";
import { OperationOptions } from "@azure-rest/core-client";
import {
AppConfigurationClient,
ConfigurationSettingId,
Expand Down Expand Up @@ -101,7 +101,9 @@ function applyRequestTracing<T extends OperationOptions>(requestTracingOptions:
const actualOptions = { ...operationOptions };
if (requestTracingOptions.enabled) {
actualOptions.requestOptions = {
customHeaders: {
...actualOptions.requestOptions,
headers: {
...actualOptions.requestOptions?.headers,
[CORRELATION_CONTEXT_HEADER_NAME]: createCorrelationContextHeader(requestTracingOptions)
}
};
Expand Down
82 changes: 31 additions & 51 deletions test/requestTracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("request tracing", function () {
after(() => {
});

it("should have correct user agent prefix", async () => {
it("should include the provider identifier in the user agent", async () => {
try {
await load(createMockedConnectionString(fakeEndpoint), {
clientOptions,
Expand All @@ -44,7 +44,7 @@ describe("request tracing", function () {
});
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
expect(headerPolicy.headers.get("User-Agent")).satisfy((ua: string) => ua.startsWith("javascript-appconfiguration-provider"));
expect(headerPolicy.headers.get("User-Agent")).satisfy((ua: string) => ua.includes("javascript-appconfiguration-provider"));
});

it("should have request type in correlation-context header", async () => {
Expand Down Expand Up @@ -204,18 +204,14 @@ describe("request tracing", function () {
});

it("should have filter type in correlation-context header if feature flags use feature filters", async () => {
let correlationContext: string = "";
const listKvCallback = (listOptions) => {
correlationContext = listOptions?.requestOptions?.customHeaders[CORRELATION_CONTEXT_HEADER_NAME] ?? "";
};

mockAppConfigurationClientListConfigurationSettings([[
createMockedFeatureFlag("Alpha_1", { conditions: { client_filters: [ { name: "Microsoft.TimeWindow" } ] } }),
createMockedFeatureFlag("Alpha_2", { conditions: { client_filters: [ { name: "Microsoft.Targeting" } ] } }),
createMockedFeatureFlag("Alpha_3", { conditions: { client_filters: [ { name: "CustomFilter" } ] } })
]], listKvCallback);
]]);

const settings = await load(createMockedConnectionString(fakeEndpoint), {
clientOptions,
featureFlagOptions: {
enabled: true,
selectors: [ {keyFilter: "*"} ],
Expand All @@ -226,34 +222,30 @@ describe("request tracing", function () {
}
});

expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Startup")).eq(true);
restoreMocks();

await sleepInMs(1_000 + 1_000);
try {
await settings.refresh();
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
const correlationContext = headerPolicy.headers.get(CORRELATION_CONTEXT_HEADER_NAME);
expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Watch")).eq(true);
expect(correlationContext?.includes("Filter=CSTM+TIME+TRGT")).eq(true);
expect(correlationContext.includes("RequestType=Watch")).eq(true);
expect(correlationContext.includes("Filter=CSTM+TIME+TRGT")).eq(true);

restoreMocks();
});

it("should have max variants in correlation-context header if feature flags use variants", async () => {
let correlationContext: string = "";
const listKvCallback = (listOptions) => {
correlationContext = listOptions?.requestOptions?.customHeaders[CORRELATION_CONTEXT_HEADER_NAME] ?? "";
};

mockAppConfigurationClientListConfigurationSettings([[
createMockedFeatureFlag("Alpha_1", { variants: [ {name: "a"}, {name: "b"}] }),
createMockedFeatureFlag("Alpha_2", { variants: [ {name: "a"}, {name: "b"}, {name: "c"}] }),
createMockedFeatureFlag("Alpha_3", { variants: [] })
]], listKvCallback);
]]);

const settings = await load(createMockedConnectionString(fakeEndpoint), {
clientOptions,
featureFlagOptions: {
enabled: true,
selectors: [ {keyFilter: "*"} ],
Expand All @@ -264,32 +256,28 @@ describe("request tracing", function () {
}
});

expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Startup")).eq(true);
restoreMocks();

await sleepInMs(1_000 + 1_000);
try {
await settings.refresh();
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
const correlationContext = headerPolicy.headers.get(CORRELATION_CONTEXT_HEADER_NAME);
expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Watch")).eq(true);
expect(correlationContext?.includes("MaxVariants=3")).eq(true);
expect(correlationContext.includes("RequestType=Watch")).eq(true);
expect(correlationContext.includes("MaxVariants=3")).eq(true);

restoreMocks();
});

it("should have telemety tag in correlation-context header if feature flags enable telemetry", async () => {
let correlationContext: string = "";
const listKvCallback = (listOptions) => {
correlationContext = listOptions?.requestOptions?.customHeaders[CORRELATION_CONTEXT_HEADER_NAME] ?? "";
};

mockAppConfigurationClientListConfigurationSettings([[
createMockedFeatureFlag("Alpha_1", { telemetry: {enabled: true} })
]], listKvCallback);
]]);

const settings = await load(createMockedConnectionString(fakeEndpoint), {
clientOptions,
featureFlagOptions: {
enabled: true,
selectors: [ {keyFilter: "*"} ],
Expand All @@ -300,33 +288,29 @@ describe("request tracing", function () {
}
});

expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Startup")).eq(true);
restoreMocks();

await sleepInMs(1_000 + 1_000);
try {
await settings.refresh();
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
const correlationContext = headerPolicy.headers.get(CORRELATION_CONTEXT_HEADER_NAME);
expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Watch")).eq(true);
expect(correlationContext?.includes("FFFeatures=Telemetry")).eq(true);
expect(correlationContext.includes("RequestType=Watch")).eq(true);
expect(correlationContext.includes("FFFeatures=Telemetry")).eq(true);

restoreMocks();
});

it("should have seed tag in correlation-context header if feature flags use allocation seed", async () => {
let correlationContext: string = "";
const listKvCallback = (listOptions) => {
correlationContext = listOptions?.requestOptions?.customHeaders[CORRELATION_CONTEXT_HEADER_NAME] ?? "";
};

mockAppConfigurationClientListConfigurationSettings([[
createMockedFeatureFlag("Alpha_1", { telemetry: {enabled: true} }),
createMockedFeatureFlag("Alpha_2", { allocation: {seed: "123"} })
]], listKvCallback);
]]);

const settings = await load(createMockedConnectionString(fakeEndpoint), {
clientOptions,
featureFlagOptions: {
enabled: true,
selectors: [ {keyFilter: "*"} ],
Expand All @@ -337,48 +321,44 @@ describe("request tracing", function () {
}
});

expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Startup")).eq(true);
restoreMocks();

await sleepInMs(1_000 + 1_000);
try {
await settings.refresh();
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
const correlationContext = headerPolicy.headers.get(CORRELATION_CONTEXT_HEADER_NAME);
expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Watch")).eq(true);
expect(correlationContext?.includes("FFFeatures=Seed+Telemetry")).eq(true);
expect(correlationContext.includes("RequestType=Watch")).eq(true);
expect(correlationContext.includes("FFFeatures=Seed+Telemetry")).eq(true);

restoreMocks();
});

it("should have AI tag in correlation-context header if key values use AI configuration", async () => {
let correlationContext: string = "";
const listKvCallback = (listOptions) => {
correlationContext = listOptions?.requestOptions?.customHeaders[CORRELATION_CONTEXT_HEADER_NAME] ?? "";
};

mockAppConfigurationClientListConfigurationSettings([[
createMockedKeyValue({ contentType: "application/json; profile=\"https://azconfig.io/mime-profiles/ai/chat-completion\"" })
]], listKvCallback);
]]);

const settings = await load(createMockedConnectionString(fakeEndpoint), {
clientOptions,
refreshOptions: {
enabled: true,
refreshIntervalInMs: 1000
}
});

expect(correlationContext).not.undefined;
expect(correlationContext?.includes("RequestType=Startup")).eq(true);
restoreMocks();

await sleepInMs(1000 + 1);
try {
await settings.refresh();
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
const correlationContext = headerPolicy.headers.get(CORRELATION_CONTEXT_HEADER_NAME);
expect(correlationContext).not.undefined;
expect(correlationContext?.includes("Features=AI+AICC")).eq(true);
expect(correlationContext.includes("Features=AI+AICC")).eq(true);

restoreMocks();
});
Expand Down
Loading