Skip to content

Commit a94fd84

Browse files
support telemetry processor & initializer
1 parent b9bcbc9 commit a94fd84

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
export { createTelemetryPublisher, trackEvent } from "./telemetry.js";
4+
export { createTelemetryPublisher, trackEvent, createTargetingTelemetryInitializer } from "./telemetry.js";
55
export { VERSION } from "./version.js";

src/feature-management-applicationinsights-browser/src/telemetry.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { EvaluationResult, createFeatureEvaluationEventProperties } from "@microsoft/feature-management";
5-
import { ApplicationInsights, IEventTelemetry } from "@microsoft/applicationinsights-web";
4+
import { EvaluationResult, createFeatureEvaluationEventProperties, TargetingContextAccessor } from "@microsoft/feature-management";
5+
import { ApplicationInsights, IEventTelemetry, ITelemetryItem } from "@microsoft/applicationinsights-web";
66

77
const TARGETING_ID = "TargetingId";
88
const FEATURE_EVALUATION_EVENT_NAME = "FeatureEvaluation";
@@ -39,3 +39,18 @@ export function trackEvent(client: ApplicationInsights, targetingId: string, eve
3939
properties[TARGETING_ID] = targetingId ? targetingId.toString() : "";
4040
client.trackEvent(event, properties);
4141
}
42+
43+
/**
44+
* Creates a telemetry initializer that adds targeting id to telemetry item's custom properties.
45+
* @param targetingContextAccessor The accessor function to get the targeting context.
46+
* @returns A telemetry initializer that attaches targeting id to telemetry items.
47+
*/
48+
export function createTargetingTelemetryInitializer(targetingContextAccessor: TargetingContextAccessor): (item: ITelemetryItem) => void {
49+
return (item: ITelemetryItem) => {
50+
const targetingContext = targetingContextAccessor();
51+
if (targetingContext?.userId === undefined) {
52+
console.warn("Targeting id is undefined.");
53+
}
54+
item.data = {...item.data, [TARGETING_ID]: targetingContext?.userId || ""};
55+
};
56+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
export { createTelemetryPublisher, trackEvent } from "./telemetry.js";
4+
export { createTelemetryPublisher, trackEvent, createTargetingTelemetryProcessor } from "./telemetry.js";
55
export { VERSION } from "./version.js";

src/feature-management-applicationinsights-node/src/telemetry.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { EvaluationResult, createFeatureEvaluationEventProperties } from "@microsoft/feature-management";
4+
import { EvaluationResult, createFeatureEvaluationEventProperties, TargetingContextAccessor } from "@microsoft/feature-management";
55
import { TelemetryClient, Contracts } from "applicationinsights";
66

77
const TARGETING_ID = "TargetingId";
@@ -39,3 +39,20 @@ export function trackEvent(client: TelemetryClient, targetingId: string, event:
3939
};
4040
client.trackEvent(event);
4141
}
42+
43+
/**
44+
* Creates a telemetry processor that adds targeting id to telemetry envelope's custom properties.
45+
* @param targetingContextAccessor The accessor function to get the targeting context.
46+
* @returns A telemetry processor that attaches targeting id to telemetry envelopes.
47+
*/
48+
export function createTargetingTelemetryProcessor(targetingContextAccessor: TargetingContextAccessor): (envelope: Contracts.EnvelopeTelemetry) => boolean {
49+
return (envelope: Contracts.EnvelopeTelemetry) => {
50+
const targetingContext = targetingContextAccessor();
51+
if (targetingContext?.userId === undefined) {
52+
console.warn("Targeting id is undefined.");
53+
}
54+
envelope.data.baseData = envelope.data.baseData || {};
55+
envelope.data.baseData.properties = {...envelope.data.baseData.properties, [TARGETING_ID]: targetingContext.userId};
56+
return true; // If a telemetry processor returns false, that telemetry item isn't sent.
57+
};
58+
}

0 commit comments

Comments
 (0)