Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -12,16 +12,48 @@ 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(),
"Variant": event.variant?.name,
"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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the percentage seems to include percentrage of all other unassigned variants, please confirm the design

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I referenced the old pr. There was a bug. It is fixed in the latest PR: microsoft/FeatureManagement-Python#45

}
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Loading