You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/quote-of-the-day/README.md
+138Lines changed: 138 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,4 +14,142 @@ npm run build
14
14
15
15
```cmd
16
16
npm run start
17
+
```
18
+
19
+
## Telemetry
20
+
21
+
The Quote of the Day example implements telemetry using Azure Application Insights to track feature flag evaluations. This helps monitor and analyze how feature flags are being used in your application.
22
+
23
+
### Application Insights Integration
24
+
25
+
The application uses the `@microsoft/feature-management-applicationinsights-node` package to integrate Feature Management with Application Insights:
The `onFeatureEvaluated` option registers a callback that automatically sends telemetry events to Application Insights whenever a feature flag is evaluated.
39
+
40
+
### Targeting Context in Telemetry
41
+
42
+
The telemetry implementation also captures the targeting context, which includes user ID and groups, in the telemetry data:
43
+
44
+
```javascript
45
+
// Initialize Application Insights with targeting context
This ensures that every telemetry event sent to Application Insights includes the targeting identity information, allowing you to correlate feature flag usage with specific users or groups in your analytics.
52
+
53
+
### Experimentation and A/B Testing
54
+
55
+
Telemetry is particularly valuable for running experiments like A/B tests. Here's how you can use telemetry to track whether different variants of a feature influence user behavior.
56
+
57
+
In this example, a variant feature flag is used to track the like button click rate of a web application:
58
+
59
+
```json
60
+
{
61
+
"id": "Greeting",
62
+
"enabled": true,
63
+
"variants": [
64
+
{
65
+
"name": "Default"
66
+
},
67
+
{
68
+
"name": "Simple",
69
+
"configuration_value": "Hello!"
70
+
},
71
+
{
72
+
"name": "Long",
73
+
"configuration_value": "I hope this makes your day!"
74
+
}
75
+
],
76
+
"allocation": {
77
+
"percentile": [
78
+
{
79
+
"variant": "Default",
80
+
"from": 0,
81
+
"to": 50
82
+
},
83
+
{
84
+
"variant": "Simple",
85
+
"from": 50,
86
+
"to": 75
87
+
},
88
+
{
89
+
"variant": "Long",
90
+
"from": 75,
91
+
"to": 100
92
+
}
93
+
],
94
+
"default_when_enabled": "Default",
95
+
"default_when_disabled": "Default"
96
+
},
97
+
"telemetry": {
98
+
"enabled": true
99
+
}
100
+
}
101
+
```
102
+
103
+
## Targeting
104
+
105
+
The targeting mechanism uses the `exampleTargetingContextAccessor` to extract the targeting context from the request. This function retrieves the userId and groups from the query parameters of the request.
106
+
107
+
```javascript
108
+
consttargetingContextAccessor= {
109
+
getTargetingContext: () => {
110
+
constreq=requestAccessor.getStore();
111
+
if (req ===undefined) {
112
+
returnundefined;
113
+
}
114
+
// read user and groups from request
115
+
constuserId=req.query.userId??req.body.userId;
116
+
constgroups=req.query.groups??req.body.groups;
117
+
// return an ITargetingContext with the appropriate user info
118
+
return { userId: userId, groups: groups ?groups.split(",") : [] };
119
+
}
120
+
};
121
+
```
122
+
123
+
The `FeatureManager` is configured with this targeting context accessor:
This allows you to get ambient targeting context while doing feature flag evaluation and variant allocation.
135
+
136
+
### Request Accessor
137
+
138
+
The `requestAccessor` is an instance of `AsyncLocalStorage` from the `async_hooks` module. It is used to store the request object in asynchronous local storage, allowing it to be accessed throughout the lifetime of the request. This is particularly useful for accessing request-specific data in asynchronous operations. For more information, please go to https://nodejs.org/api/async_context.html
139
+
140
+
```javascript
141
+
import { AsyncLocalStorage } from"async_hooks";
142
+
constrequestAccessor=newAsyncLocalStorage();
143
+
```
144
+
145
+
Middleware is used to store the request object in the AsyncLocalStorage:
0 commit comments