Skip to content

Commit 8fa989a

Browse files
add node.js package
1 parent 6b16e60 commit 8fa989a

File tree

6 files changed

+74
-30
lines changed

6 files changed

+74
-30
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ jobs:
5959

6060
- name: Run lint check for feature-management-applicationinsights-browser
6161
run: npm run lint
62-
working-directory: sdk/feature-management-applicationinsights-browser
62+
working-directory: sdk/feature-management-applicationinsights-browser
63+
64+
- name: Build feature-management-applicationinsights-node
65+
run: npm run build
66+
working-directory: sdk/feature-management-applicationinsights-node
67+
68+
- name: Run lint check for feature-management-applicationinsights-node
69+
run: npm run lint
70+
working-directory: sdk/feature-management-applicationinsights-node

scripts/build-and-pack.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,19 @@ echo "npm pack in $PACKAGE_DIR"
4141
npm pack
4242

4343
echo "copy $PACKAGE package to $PROJECT_BASE_DIR"
44-
cp "$PACKAGE_DIR"/*.tgz "$PROJECT_BASE_DIR"
44+
cp "$PACKAGE_DIR"/*.tgz "$PROJECT_BASE_DIR"
45+
46+
PACKAGE="feature-management-applicationinsights-node"
47+
PACKAGE_DIR="$SDK_DIR/$PACKAGE"
48+
49+
echo "Building package $PACKAGE in $PACKAGE_DIR"
50+
cd "$PACKAGE_DIR"
51+
52+
echo "npm run build in $PACKAGE_DIR"
53+
npm run build
54+
55+
echo "npm pack in $PACKAGE_DIR"
56+
npm pack
57+
58+
echo "copy $PACKAGE package to $PROJECT_BASE_DIR"
59+
cp "$PACKAGE_DIR"/*.tgz "$PROJECT_BASE_DIR"

sdk/feature-management-applicationinsights-node/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ Feature Management Application Insights Plugin for Browser provides a solution f
1111
### Usage
1212

1313
``` javascript
14-
import applicationInsights from "applicationinsights";
14+
import appInsights from "applicationinsights";
1515
import { FeatureManager, ConfigurationObjectFeatureFlagProvider } from "@microsoft/feature-management";
1616
import { trackEvent, publishTelemetry } from "@microsoft/feature-management-applicationinsights-node";
1717

18-
applicationInsights.setup(CONNECTION_STRING).start();
18+
appInsights.setup(CONNECTION_STRING)
19+
.start();
1920

21+
const publishTelemetry = createTelemetryPublisher(appInsights.defaultClient);
2022
const provider = new ConfigurationObjectFeatureFlagProvider(jsonObject);
2123
const featureManager = new FeatureManager(provider, {onFeatureEvaluated: publishTelemetry});
2224

2325
// FeatureEvaluation event will be emitted when a feature flag is evaluated
2426
featureManager.getVariant("TestFeature", {userId : TARGETING_ID}).then((variant) => { /* do something*/ });
2527

2628
// Emit a custom event with targeting id attached.
27-
trackEvent(TARGETING_ID, {name: "TestEvent"});
29+
trackEvent(appInsights.defaultClient, TARGETING_ID, {name: "TestEvent"});
2830
```
2931

3032
## Contributing

sdk/feature-management-applicationinsights-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@microsoft/feature-management-applicationinsights-node",
33
"version": "2.0.0-preview.2",
4-
"description": "Feature Management Application Insights Plugin for Browser provides a solution for sending feature flag evaluation events produced by the Feature Management library.",
4+
"description": "Feature Management Application Insights Plugin for Node.js provides a solution for sending feature flag evaluation events produced by the Feature Management library.",
55
"main": "./dist/commonjs/index.js",
66
"module": "./dist/esm/index.js",
77
"types": "types/index.d.ts",
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 { publishTelemetry, trackEvent } from "./telemetry.js";
4+
export { createTelemetryPublisher, trackEvent } from "./telemetry.js";
55
export { VERSION } from "./version.js";

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,50 @@
22
// Licensed under the MIT license.
33

44
import { EvaluationResult } from "@microsoft/feature-management";
5-
import { applicationInsights } from "applicationinsights";
5+
import { TelemetryClient, Contracts } from "applicationinsights";
66

7-
export function publishTelemetry(event: EvaluationResult): void {
8-
if (applicationInsights.defaultClient === undefined) {
9-
console.warn(`Application Insights default client is not found.`);
10-
return;
11-
}
7+
/**
8+
* Creates a telemetry publisher that sends feature evaluation events to Application Insights.
9+
* @param client The Application Insights telemetry client.
10+
* @returns A callback function that takes an evaluation result and tracks an event with the evaluation details.
11+
*/
12+
export function createTelemetryPublisher(client: TelemetryClient): (event: EvaluationResult) => void {
13+
return (event: EvaluationResult) => {
14+
const eventProperties = {
15+
"FeatureName": event.feature ? event.feature.id : "",
16+
"Enabled": event.enabled.toString(),
17+
// Ensure targetingId is string so that it will be placed in customDimensions
18+
"TargetingId": event.targetingId ? event.targetingId.toString() : "",
19+
"Variant": event.variant ? event.variant.name : "",
20+
"VariantAssignmentReason": event.variantAssignmentReason,
21+
};
1222

13-
const eventProperties = {
14-
"FeatureName": event.feature ? event.feature.id : "",
15-
"Enabled": event.enabled.toString(),
16-
// Ensure targetingId is string so that it will be placed in customDimensions
17-
"TargetingId": event.targetingId ? event.targetingId.toString() : "",
18-
"Variant": event.variant ? event.variant.name : "",
19-
"VariantAssignmentReason": event.variantAssignmentReason,
20-
};
21-
22-
const metadata = event.feature?.telemetry?.metadata;
23-
if (metadata) {
24-
for (const key in metadata) {
25-
if (!(key in eventProperties)) {
26-
eventProperties[key] = metadata[key];
23+
const metadata = event.feature?.telemetry?.metadata;
24+
if (metadata) {
25+
for (const key in metadata) {
26+
if (!(key in eventProperties)) {
27+
eventProperties[key] = metadata[key];
28+
}
2729
}
2830
}
29-
}
3031

31-
applicationInsights.defaultClient.trackEvent({ name: "FeatureEvaluation", properties: eventProperties});
32-
}
32+
client.trackEvent({ name: "FeatureEvaluation", properties: eventProperties });
33+
};
34+
}
35+
36+
/**
37+
* Tracks a custom event using Application Insights, ensuring that the "TargetingId"
38+
* is included in the custom properties. If the "TargetingId" already exists in
39+
* the provided custom properties, it will be overwritten.
40+
*
41+
* @param client The Application Insights client instance used to track the event.
42+
* @param targetingId The unique targeting identifier that will be included in the custom properties.
43+
* @param event The event telemetry object to be tracked, containing event details.
44+
*/
45+
export function trackEvent(client: TelemetryClient, targetingId: string, event: Contracts.EventTelemetry): void {
46+
event.properties = {
47+
...event.properties,
48+
TargetingId: targetingId ? targetingId.toString() : ""
49+
};
50+
client.trackEvent(event);
51+
}

0 commit comments

Comments
 (0)