Skip to content

Commit dfbe35e

Browse files
Support telemetry processor & initializer (#98)
* support targeting context accessor * add test * fix lint * update * update * export targeting context * add comments * support telemetry processor & initializer * update * update
1 parent 6adbb93 commit dfbe35e

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,5 @@ examples/**/**/package-lock.json
413413

414414
# playwright test result
415415
test-results
416+
417+
**/public
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 { createTargetingTelemetryInitializer, createTelemetryPublisher, trackEvent } from "./telemetry.js";
55
export { VERSION } from "./version.js";

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

Lines changed: 19 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, ITargetingContextAccessor } 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,20 @@ 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: ITargetingContextAccessor): (item: ITelemetryItem) => void {
49+
return (item: ITelemetryItem) => {
50+
const targetingContext = targetingContextAccessor.getTargetingContext();
51+
if (targetingContext !== undefined) {
52+
if (targetingContext?.userId === undefined) {
53+
console.warn("Targeting id is undefined.");
54+
}
55+
item.data = {...item.data, [TARGETING_ID]: targetingContext?.userId || ""};
56+
}
57+
};
58+
}
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 { createTargetingTelemetryProcessor, createTelemetryPublisher, trackEvent } from "./telemetry.js";
55
export { VERSION } from "./version.js";

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

Lines changed: 20 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, ITargetingContextAccessor } from "@microsoft/feature-management";
55
import { TelemetryClient, Contracts } from "applicationinsights";
66

77
const TARGETING_ID = "TargetingId";
@@ -39,3 +39,22 @@ 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: ITargetingContextAccessor): (envelope: Contracts.EnvelopeTelemetry) => boolean {
49+
return (envelope: Contracts.EnvelopeTelemetry) => {
50+
const targetingContext = targetingContextAccessor.getTargetingContext();
51+
if (targetingContext !== undefined) {
52+
if (targetingContext?.userId === undefined) {
53+
console.warn("Targeting id is undefined.");
54+
}
55+
envelope.data.baseData = envelope.data.baseData || {};
56+
envelope.data.baseData.properties = {...envelope.data.baseData.properties, [TARGETING_ID]: targetingContext?.userId || ""};
57+
}
58+
return true;
59+
};
60+
}

0 commit comments

Comments
 (0)