Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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,7 +12,12 @@ import { IEventTelemetry } from "@microsoft/applicationinsights-web";
*/
export function createTelemetryPublisher(client: ApplicationInsights): (event: EvaluationResult) => void {
return (event: EvaluationResult) => {
if (event.feature === undefined) {
return;
}

const eventProperties = {
"Version": EVALUATION_EVENT_VERSION,
"FeatureName": event.feature ? event.feature.id : "",
"Enabled": event.enabled.toString(),
// Ensure targetingId is string so that it will be placed in customDimensions
Expand All @@ -21,7 +26,32 @@ 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) {
percentileAllocationPercentage += percentile.to - percentile.from;
}
}
eventProperties["VariantAssignmentPercentage"] = (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["VariantAssignmentPercentage"] = 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