Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/express-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ The targeting mechanism uses the `exampleTargetingContextAccessor` to extract th
const exampleTargetingContextAccessor = {
getTargetingContext: () => {
const req = requestAccessor.getStore();
if (req === undefined) {
return undefined;
}
// read user and groups from request query data
const { userId, groups } = req.query;
// return aa ITargetingContext with the appropriate user info
Expand Down
3 changes: 3 additions & 0 deletions examples/express-app/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const requestAccessor = new AsyncLocalStorage();
const exampleTargetingContextAccessor = {
getTargetingContext: () => {
const req = requestAccessor.getStore();
if (req === undefined) {
return undefined;
}
// read user and groups from request query data
const { userId, groups } = req.query;
// return an ITargetingContext with the appropriate user info
Expand Down
6 changes: 6 additions & 0 deletions examples/quote-of-the-day/.env.temlate
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# You can define environment variables in .env file and load them with 'dotenv' package.
# This is a template of related environment variables in examples.
# To use this file directly, please rename it to .env
APPCONFIG_CONNECTION_STRING=<app-configuration-connection-string>
APPLICATIONINSIGHTS_CONNECTION_STRING=<application-insights-connection-string>
USE_APP_CONFIG=true
155 changes: 155 additions & 0 deletions examples/quote-of-the-day/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Quote of the day - JavaScript

These examples show how to use the Microsoft Feature Management in an express application.

## Setup & Run

1. Build the project.

```cmd
npm run build
```

1. Start the application.

```cmd
npm run start
```

## Telemetry

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.

### Application Insights Integration

The application uses the `@microsoft/feature-management-applicationinsights-node` package to integrate Feature Management with Application Insights:

```javascript
const { createTelemetryPublisher } = require("@microsoft/feature-management-applicationinsights-node");

// When initializing Feature Management
const publishTelemetry = createTelemetryPublisher(appInsightsClient);
featureManager = new FeatureManager(featureFlagProvider, {
onFeatureEvaluated: publishTelemetry,
targetingContextAccessor: targetingContextAccessor
});
```

The `onFeatureEvaluated` option registers a callback that automatically sends telemetry events to Application Insights whenever a feature flag is evaluated.

### Targeting Context in Telemetry

The telemetry implementation also captures the targeting context, which includes user ID and groups, in the telemetry data:

```javascript
// Initialize Application Insights with targeting context
applicationInsights.defaultClient.addTelemetryProcessor(
createTargetingTelemetryProcessor(targetingContextAccessor)
);
```

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.

### Experimentation and A/B Testing

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.

In this example, a variant feature flag is used to track the like button click rate of a web application:

```json
{
"id": "Greeting",
"enabled": true,
"variants": [
{
"name": "Default"
},
{
"name": "Simple",
"configuration_value": "Hello!"
},
{
"name": "Long",
"configuration_value": "I hope this makes your day!"
}
],
"allocation": {
"percentile": [
{
"variant": "Default",
"from": 0,
"to": 50
},
{
"variant": "Simple",
"from": 50,
"to": 75
},
{
"variant": "Long",
"from": 75,
"to": 100
}
],
"default_when_enabled": "Default",
"default_when_disabled": "Default"
},
"telemetry": {
"enabled": true
}
}
```

## Targeting

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.

```javascript
const targetingContextAccessor = {
getTargetingContext: () => {
const req = requestAccessor.getStore();
if (req === undefined) {
return undefined;
}
// read user and groups from request
const userId = req.query.userId ?? req.body.userId;
const groups = req.query.groups ?? req.body.groups;
// return an ITargetingContext with the appropriate user info
return { userId: userId, groups: groups ? groups.split(",") : [] };
}
};
```

The `FeatureManager` is configured with this targeting context accessor:

```javascript
const featureManager = new FeatureManager(
featureProvider,
{
targetingContextAccessor: exampleTargetingContextAccessor
}
);
```

This allows you to get ambient targeting context while doing feature flag evaluation and variant allocation.

### Request Accessor

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

```javascript
import { AsyncLocalStorage } from "async_hooks";
const requestAccessor = new AsyncLocalStorage();
```

Middleware is used to store the request object in the AsyncLocalStorage:

```javascript
const requestStorageMiddleware = (req, res, next) => {
requestAccessor.run(req, next);
};

...

server.use(requestStorageMiddleware);
```
13 changes: 13 additions & 0 deletions examples/quote-of-the-day/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Quote of the Day</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions examples/quote-of-the-day/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "quoteoftheday",
"type": "module",
"scripts": {
"build": "vite build --emptyOutDir"
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.27.0",
"react-icons": "5.3.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.3.1",
"vite": "^5.4.1"
}
}
Loading