|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | | -import { EvaluationResult, VariantAssignmentReason } from "@microsoft/feature-management"; |
| 4 | +import { EvaluationResult, createFeatureEvaluationEventProperties } from "@microsoft/feature-management"; |
5 | 5 | import { ApplicationInsights, IEventTelemetry } from "@microsoft/applicationinsights-web"; |
6 | | -import { EVALUATION_EVENT_VERSION } from "./version.js"; |
7 | 6 |
|
8 | | -const VERSION = "Version"; |
9 | | -const FEATURE_NAME = "FeatureName"; |
10 | | -const ENABLED = "Enabled"; |
11 | 7 | const TARGETING_ID = "TargetingId"; |
12 | | -const VARIANT = "Variant"; |
13 | | -const VARIANT_ASSIGNMENT_REASON = "VariantAssignmentReason"; |
14 | | -const DEFAULT_WHEN_ENABLED = "DefaultWhenEnabled"; |
15 | | -const VARIANT_ASSIGNMENT_PERCENTAGE = "VariantAssignmentPercentage"; |
16 | 8 | const FEATURE_EVALUATION_EVENT_NAME = "FeatureEvaluation"; |
17 | 9 |
|
18 | 10 | /** |
19 | 11 | * Creates a telemetry publisher that sends feature evaluation events to Application Insights. |
20 | 12 | * @param client The Application Insights telemetry client. |
21 | 13 | * @returns A callback function that takes an evaluation result and tracks an event with the evaluation details. |
22 | 14 | */ |
23 | | -export function createTelemetryPublisher(client: ApplicationInsights): (event: EvaluationResult) => void { |
24 | | - return (event: EvaluationResult) => { |
25 | | - if (event.feature === undefined) { |
| 15 | +export function createTelemetryPublisher(client: ApplicationInsights): (result: EvaluationResult) => void { |
| 16 | + return (result: EvaluationResult) => { |
| 17 | + if (result.feature === undefined) { |
26 | 18 | return; |
27 | 19 | } |
28 | 20 |
|
29 | | - const eventProperties = { |
30 | | - [VERSION]: EVALUATION_EVENT_VERSION, |
31 | | - [FEATURE_NAME]: event.feature ? event.feature.id : "", |
32 | | - [ENABLED]: event.enabled.toString(), |
33 | | - // Ensure targetingId is string so that it will be placed in customDimensions |
34 | | - [TARGETING_ID]: event.targetingId ? event.targetingId.toString() : "", |
35 | | - [VARIANT]: event.variant ? event.variant.name : "", |
36 | | - [VARIANT_ASSIGNMENT_REASON]: event.variantAssignmentReason, |
37 | | - }; |
38 | | - |
39 | | - if (event.feature.allocation?.default_when_enabled) { |
40 | | - eventProperties[DEFAULT_WHEN_ENABLED] = event.feature.allocation.default_when_enabled; |
41 | | - } |
42 | | - |
43 | | - if (event.variantAssignmentReason === VariantAssignmentReason.DefaultWhenEnabled) { |
44 | | - let percentileAllocationPercentage = 0; |
45 | | - if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) { |
46 | | - for (const percentile of event.feature.allocation.percentile) { |
47 | | - percentileAllocationPercentage += percentile.to - percentile.from; |
48 | | - } |
49 | | - } |
50 | | - eventProperties[VARIANT_ASSIGNMENT_PERCENTAGE] = (100 - percentileAllocationPercentage).toString(); |
51 | | - } |
52 | | - else if (event.variantAssignmentReason === VariantAssignmentReason.Percentile) { |
53 | | - let percentileAllocationPercentage = 0; |
54 | | - if (event.variant !== undefined && event.feature.allocation !== undefined && event.feature.allocation.percentile !== undefined) { |
55 | | - for (const percentile of event.feature.allocation.percentile) { |
56 | | - if (percentile.variant === event.variant.name) { |
57 | | - percentileAllocationPercentage += percentile.to - percentile.from; |
58 | | - } |
59 | | - } |
60 | | - } |
61 | | - eventProperties[VARIANT_ASSIGNMENT_PERCENTAGE] = percentileAllocationPercentage.toString(); |
62 | | - } |
63 | | - |
64 | | - const metadata = event.feature.telemetry?.metadata; |
65 | | - if (metadata) { |
66 | | - for (const key in metadata) { |
67 | | - if (!(key in eventProperties)) { |
68 | | - eventProperties[key] = metadata[key]; |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - |
| 21 | + const eventProperties = createFeatureEvaluationEventProperties(result); |
73 | 22 | client.trackEvent({ name: FEATURE_EVALUATION_EVENT_NAME }, eventProperties); |
74 | 23 | }; |
75 | 24 | } |
|
0 commit comments