From 850418a16f5c66f30953bd506f7322384b8824d2 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Fri, 18 Oct 2024 15:35:01 +0800 Subject: [PATCH 1/4] init --- .../src/telemetry.ts | 42 ++++++++++++++++--- .../src/version.ts | 1 + 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/sdk/feature-management-applicationinsights-browser/src/telemetry.ts b/sdk/feature-management-applicationinsights-browser/src/telemetry.ts index 9323c7b..9fcc692 100644 --- a/sdk/feature-management-applicationinsights-browser/src/telemetry.ts +++ b/sdk/feature-management-applicationinsights-browser/src/telemetry.ts @@ -1,9 +1,9 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { EvaluationResult } from "@microsoft/feature-management"; -import { ApplicationInsights } from "@microsoft/applicationinsights-web"; -import { IEventTelemetry } from "@microsoft/applicationinsights-web"; +import { EvaluationResult, VariantAssignmentReason } from "@microsoft/feature-management"; +import { ApplicationInsights, IEventTelemetry } from "@microsoft/applicationinsights-web"; +import { EVALUATION_EVENT_VERSION } from "./version.js"; /** * Creates a telemetry publisher that sends feature evaluation events to Application Insights. @@ -12,8 +12,13 @@ import { IEventTelemetry } from "@microsoft/applicationinsights-web"; */ export function createTelemetryPublisher(client: ApplicationInsights): (event: EvaluationResult) => void { return (event: EvaluationResult) => { + if (event.feature === undefined) { + return; + } + const eventProperties = { - "FeatureName": event.feature?.id, + "Version": EVALUATION_EVENT_VERSION, + "FeatureName": event.feature.id, "Enabled": event.enabled.toString(), // Ensure targetingId is string so that it will be placed in customDimensions "TargetingId": event.targetingId?.toString(), @@ -21,7 +26,34 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E "VariantAssignmentReason": event.variantAssignmentReason, }; - const metadata = event.feature?.telemetry?.metadata; + if (event.feature.allocation?.default_when_enabled) { + eventProperties["DefaultWhenEnabled"] = event.feature.allocation.default_when_enabled; + } + + if (event.variantAssignmentReason === VariantAssignmentReason.DefaultWhenEnabled) { + let percentileAllocationPercentage = 0; + if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) { + for (const percentile of event.feature.allocation.percentile) { + if (percentile.variant === event.variant.name) { + percentileAllocationPercentage += percentile.to - percentile.from; + } + } + } + eventProperties["PercentileAllocationPercentage"] = (100 - percentileAllocationPercentage).toString(); + } + else if (event.variantAssignmentReason === VariantAssignmentReason.Percentile) { + let percentileAllocationPercentage = 0; + if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) { + for (const percentile of event.feature.allocation.percentile) { + if (percentile.variant === event.variant.name) { + percentileAllocationPercentage += percentile.to - percentile.from; + } + } + } + eventProperties["PercentileAllocationPercentage"] = percentileAllocationPercentage.toString(); + } + + const metadata = event.feature.telemetry?.metadata; if (metadata) { for (const key in metadata) { if (!(key in eventProperties)) { diff --git a/sdk/feature-management-applicationinsights-browser/src/version.ts b/sdk/feature-management-applicationinsights-browser/src/version.ts index a4e8a51..f3ba8bb 100644 --- a/sdk/feature-management-applicationinsights-browser/src/version.ts +++ b/sdk/feature-management-applicationinsights-browser/src/version.ts @@ -2,3 +2,4 @@ // Licensed under the MIT license. export const VERSION = "2.0.0-preview.1"; +export const EVALUATION_EVENT_VERSION = "1.0.0"; \ No newline at end of file From 1dca54fe95fefc1d1940baea2d18b73717ee69f0 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Fri, 18 Oct 2024 15:40:55 +0800 Subject: [PATCH 2/4] fix lint --- .../src/version.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/feature-management-applicationinsights-browser/src/version.ts b/sdk/feature-management-applicationinsights-browser/src/version.ts index f3ba8bb..06b1d01 100644 --- a/sdk/feature-management-applicationinsights-browser/src/version.ts +++ b/sdk/feature-management-applicationinsights-browser/src/version.ts @@ -2,4 +2,4 @@ // Licensed under the MIT license. export const VERSION = "2.0.0-preview.1"; -export const EVALUATION_EVENT_VERSION = "1.0.0"; \ No newline at end of file +export const EVALUATION_EVENT_VERSION = "1.0.0"; From c516a63c75eca04b06428a6afd5170b567a66c37 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 23 Oct 2024 15:41:33 +0800 Subject: [PATCH 3/4] fix bug --- .../src/telemetry.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk/feature-management-applicationinsights-browser/src/telemetry.ts b/sdk/feature-management-applicationinsights-browser/src/telemetry.ts index 9fcc692..a114809 100644 --- a/sdk/feature-management-applicationinsights-browser/src/telemetry.ts +++ b/sdk/feature-management-applicationinsights-browser/src/telemetry.ts @@ -34,9 +34,7 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E let percentileAllocationPercentage = 0; if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) { for (const percentile of event.feature.allocation.percentile) { - if (percentile.variant === event.variant.name) { - percentileAllocationPercentage += percentile.to - percentile.from; - } + percentileAllocationPercentage += percentile.to - percentile.from; } } eventProperties["PercentileAllocationPercentage"] = (100 - percentileAllocationPercentage).toString(); From ee47659d49f56559dea64f0b3f62436f7c4162dd Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 23 Oct 2024 15:44:22 +0800 Subject: [PATCH 4/4] update property name --- .../src/telemetry.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/feature-management-applicationinsights-browser/src/telemetry.ts b/sdk/feature-management-applicationinsights-browser/src/telemetry.ts index a114809..2302be7 100644 --- a/sdk/feature-management-applicationinsights-browser/src/telemetry.ts +++ b/sdk/feature-management-applicationinsights-browser/src/telemetry.ts @@ -37,7 +37,7 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E percentileAllocationPercentage += percentile.to - percentile.from; } } - eventProperties["PercentileAllocationPercentage"] = (100 - percentileAllocationPercentage).toString(); + eventProperties["VariantAssignmentPercentage"] = (100 - percentileAllocationPercentage).toString(); } else if (event.variantAssignmentReason === VariantAssignmentReason.Percentile) { let percentileAllocationPercentage = 0; @@ -48,7 +48,7 @@ export function createTelemetryPublisher(client: ApplicationInsights): (event: E } } } - eventProperties["PercentileAllocationPercentage"] = percentileAllocationPercentage.toString(); + eventProperties["VariantAssignmentPercentage"] = percentileAllocationPercentage.toString(); } const metadata = event.feature.telemetry?.metadata;