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
115 changes: 95 additions & 20 deletions libs/providers/multi-provider-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@ the final result. Different evaluation strategies can be defined to control whic

The Multi-Provider is a powerful tool for performing migrations between flag providers, or combining multiple providers into a single
feature flagging interface. For example:

- *Migration*: When migrating between two providers, you can run both in parallel under a unified flagging interface. As flags are added to the
new provider, the Multi-Provider will automatically find and return them, falling back to the old provider if the new provider does not have
- *Multiple Data Sources*: The Multi-Provider allows you to seamlessly combine many sources of flagging data, such as environment variables,
local files, database values and SaaS hosted feature management systems.

## Installation

```
```bash
$ npm install @openfeature/multi-provider-web
```

> [!TIP]
> This provider is designed to be used with the [Web SDK](https://openfeature.dev/docs/reference/technologies/client/web/).

## Usage

The Multi-Provider is initialized with an array of providers it should evaluate:

```typescript
import { WebMultiProvider } from '@openfeature/multi-provider-web'
import { OpenFeature } from '@openfeature/web-sdk'

const multiProvider = new WebMultiProvider([
{
provider: new ProviderA()
},
{
provider: new ProviderB()
}
{ provider: new ProviderA() },
{ provider: new ProviderB() }
])

await OpenFeature.setProviderAndWait(multiProvider)
Expand All @@ -56,18 +54,16 @@ import { WebMultiProvider, FirstSuccessfulStrategy } from '@openfeature/multi-pr

const multiProvider = new WebMultiProvider(
[
{
provider: new ProviderA()
},
{
provider: new ProviderB()
}
{ provider: new ProviderA() },
{ provider: new ProviderB() }
],
new FirstSuccessfulStrategy()
)
```

The Multi-Provider comes with three strategies out of the box:
`FirstMatchStrategy` (default): Evaluates all providers in order and returns the first successful result. Providers that indicate FLAG_NOT_FOUND error will be skipped and the next provider will be evaluated. Any other error will cause the operation to fail and the set of errors to be thrown.

- `FirstMatchStrategy` (default): Evaluates all providers in order and returns the first successful result. Providers that indicate FLAG_NOT_FOUND error will be skipped and the next provider will be evaluated. Any other error will cause the operation to fail and the set of errors to be thrown.
- `FirstSuccessfulStrategy`: Evaluates all providers in order and returns the first successful result. Any error will cause that provider to be skipped.
If no successful result is returned, the set of errors will be thrown.
- `ComparisonStrategy`: Evaluates all providers in parallel. If every provider returns a successful result with the same value, then that result is returned.
Expand All @@ -83,22 +79,21 @@ import { WebMultiProvider, ComparisonStrategy } from '@openfeature/multi-provide
const providerA = new ProviderA()
const multiProvider = new WebMultiProvider(
[
{
provider: providerA
},
{
provider: new ProviderB()
}
{ provider: providerA },
{ provider: new ProviderB() }
],
new ComparisonStrategy(providerA, (details) => {
console.log("Mismatch detected", details)
})
)
```

The first argument is the "fallback provider" whose value to use in the event that providers do not agree. It should be the same object reference as one of the providers in the list. The second argument is a callback function that will be executed when a mismatch is detected. The callback will be passed an object containing the details of each provider's resolution, including the flag key, the value returned, and any errors that were thrown.

## Custom Strategies

It is also possible to implement your own strategy if the above options do not fit your use case. To do so, create a class which implements the "BaseEvaluationStrategy":

```typescript
export abstract class BaseEvaluationStrategy {
public runMode: 'parallel' | 'sequential' = 'sequential';
Expand All @@ -111,13 +106,21 @@ export abstract class BaseEvaluationStrategy {
result: ProviderResolutionResult<T>,
): boolean;

abstract shouldTrackWithThisProvider(
strategyContext: StrategyProviderContext,
context: EvaluationContext,
trackingEventName: string,
trackingEventDetails: TrackingEventDetails,
): boolean;

abstract determineFinalResult<T extends FlagValue>(
strategyContext: StrategyEvaluationContext,
context: EvaluationContext,
resolutions: ProviderResolutionResult<T>[],
): FinalResult<T>;
}
```

The `runMode` property determines whether the list of providers will be evaluated sequentially or in parallel.

The `shouldEvaluateThisProvider` method is called just before a provider is evaluated by the Multi-Provider. If the function returns `false`, then
Expand All @@ -127,9 +130,81 @@ Check the type definitions for the full list.
The `shouldEvaluateNextProvider` function is called after a provider is evaluated. If it returns `true`, the next provider in the sequence will be called,
otherwise no more providers will be evaluated. It is called with the same data as `shouldEvaluateThisProvider` as well as the details about the evaluation result. This function is not called when the `runMode` is `parallel`.

The `shouldTrackWithThisProvider` method is called before tracking an event with each provider. If the function returns `false`, then
the provider will be skipped for that tracking event. The method includes the tracking event name and details,
allowing for fine-grained control over which providers receive which events. By default, providers in `NOT_READY` or `FATAL` status are skipped.

The `determineFinalResult` function is called after all providers have been called, or the `shouldEvaluateNextProvider` function returned false. It is called
with a list of results from all the individual providers' evaluations. It returns the final decision for evaluation result, or throws an error if needed.

## Tracking Support

The Multi-Provider supports tracking events across multiple providers, allowing you to send analytics events to all configured providers simultaneously.

### Basic Tracking Usage

```typescript
import { WebMultiProvider } from '@openfeature/multi-provider-web'
import { OpenFeature } from '@openfeature/web-sdk'

const multiProvider = new WebMultiProvider([
{ provider: new ProviderA() },
{ provider: new ProviderB() }
])

await OpenFeature.setProviderAndWait(multiProvider)
const client = OpenFeature.getClient()

// Tracked events will be sent to all providers by default
client.track('user-conversion', {
value: 99.99,
currency: 'USD',
conversionType: 'purchase'
})

client.track('page-view', {
page: '/checkout',
source: 'direct'
})
```

### Tracking Behavior

- **Default**: All providers receive tracking calls by default
- **Error Handling**: If one provider fails to track, others continue normally and errors are logged
- **Provider Status**: Providers in `NOT_READY` or `FATAL` status are automatically skipped
- **Optional Method**: Providers without a `track` method are gracefully skipped

### Customizing Tracking with Strategies

You can customize which providers receive tracking calls by overriding the `shouldTrackWithThisProvider` method in your custom strategy:

```typescript
import { BaseEvaluationStrategy, StrategyProviderContext } from '@openfeature/multi-provider-web'

class CustomTrackingStrategy extends BaseEvaluationStrategy {
// Override tracking behavior
shouldTrackWithThisProvider(
strategyContext: StrategyProviderContext,
context: EvaluationContext,
trackingEventName: string,
trackingEventDetails: TrackingEventDetails,
): boolean {
// Only track with the primary provider
if (strategyContext.providerName === 'primary-provider') {
return true;
}

// Skip tracking for analytics events on backup providers
if (trackingEventName.startsWith('analytics.')) {
return false;
}

return super.shouldTrackWithThisProvider(strategyContext, context, trackingEventName, trackingEventDetails);
}
}
```

## Building

Run `nx package providers-multi-provider` to build the library.
Expand Down
168 changes: 168 additions & 0 deletions libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Provider,
ProviderEmittableEvents,
ProviderMetadata,
TrackingEventDetails,
} from '@openfeature/web-sdk';
import {
DefaultLogger,
Expand All @@ -20,13 +21,15 @@ import {
import { FirstMatchStrategy } from './strategies/FirstMatchStrategy';
import { FirstSuccessfulStrategy } from './strategies/FirstSuccessfulStrategy';
import { ComparisonStrategy } from './strategies/ComparisonStrategy';
import type { BaseEvaluationStrategy } from './strategies/BaseEvaluationStrategy';

class TestProvider implements Provider {
public metadata: ProviderMetadata = {
name: 'TestProvider',
};
public events = new OpenFeatureEventEmitter();
public hooks: Hook[] = [];
public track = jest.fn();
constructor(
public resolveBooleanEvaluation = jest.fn().mockReturnValue({ value: false }),
public resolveStringEvaluation = jest.fn().mockReturnValue({ value: 'default' }),
Expand Down Expand Up @@ -718,5 +721,170 @@ describe('MultiProvider', () => {
});
});
});

describe('tracking', () => {
const context: EvaluationContext = { targetingKey: 'user123' };
const trackingEventDetails: TrackingEventDetails = { value: 100, currency: 'USD' };

it('calls track on all providers by default', () => {
const provider1 = new TestProvider();
const provider2 = new TestProvider();
const provider3 = new TestProvider();

const multiProvider = new WebMultiProvider([
{ provider: provider1 },
{ provider: provider2 },
{ provider: provider3 },
]);

multiProvider.track('purchase', context, trackingEventDetails);

expect(provider1.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider2.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider3.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
});

it('skips providers without track method', () => {
const provider1 = new TestProvider();
const provider2 = new InMemoryProvider(); // Doesn't have track method
const provider3 = new TestProvider();

const multiProvider = new WebMultiProvider([
{ provider: provider1 },
{ provider: provider2 },
{ provider: provider3 },
]);

expect(() => multiProvider.track('purchase', context, trackingEventDetails)).not.toThrow();
expect(provider1.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider3.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
});

it('continues tracking with other providers when one throws an error', () => {
const provider1 = new TestProvider();
const provider2 = new TestProvider();
const provider3 = new TestProvider();

provider2.track.mockImplementation(() => {
throw new Error('Tracking failed');
});

const mockLogger = { error: jest.fn(), warn: jest.fn(), info: jest.fn(), debug: jest.fn() };
const multiProvider = new WebMultiProvider(
[{ provider: provider1 }, { provider: provider2 }, { provider: provider3 }],
undefined,
mockLogger,
);

expect(() => multiProvider.track('purchase', context, trackingEventDetails)).not.toThrow();

expect(provider1.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider2.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider3.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(mockLogger.error).toHaveBeenCalledWith(
'Error tracking event "purchase" with provider "TestProvider-2":',
expect.any(Error),
);
});

it('respects strategy shouldTrackWithThisProvider decision', () => {
const provider1 = new TestProvider();
const provider2 = new TestProvider();
const provider3 = new TestProvider();

// Create a custom strategy that only allows the second provider to track
class MockStrategy extends FirstMatchStrategy {
override shouldTrackWithThisProvider = jest.fn().mockImplementation((strategyContext) => {
return strategyContext.providerName === 'TestProvider-2';
});
}

const mockStrategy = new MockStrategy();

const multiProvider = new WebMultiProvider(
[{ provider: provider1 }, { provider: provider2 }, { provider: provider3 }],
mockStrategy,
);

multiProvider.track('purchase', context, trackingEventDetails);

expect(mockStrategy.shouldTrackWithThisProvider).toHaveBeenCalledTimes(3);
expect(provider1.track).not.toHaveBeenCalled();
expect(provider2.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider3.track).not.toHaveBeenCalled();
});

it('does not track with providers in NOT_READY or FATAL status by default', () => {
const provider1 = new TestProvider();
const provider2 = new TestProvider();
const provider3 = new TestProvider();

const multiProvider = new WebMultiProvider([
{ provider: provider1 },
{ provider: provider2 },
{ provider: provider3 },
]);

// Mock the status tracker to return different statuses
const mockStatusTracker = {
providerStatus: jest.fn().mockImplementation((name) => {
if (name === 'TestProvider-1') return 'NOT_READY';
if (name === 'TestProvider-2') return 'READY';
if (name === 'TestProvider-3') return 'FATAL';
return 'READY'; // Default fallback
}),
};
(multiProvider as any).statusTracker = mockStatusTracker;

multiProvider.track('purchase', context, trackingEventDetails);

expect(provider1.track).not.toHaveBeenCalled();
expect(provider2.track).toHaveBeenCalledWith('purchase', context, trackingEventDetails);
expect(provider3.track).not.toHaveBeenCalled();
});

it('passes correct strategy context to shouldTrackWithThisProvider', () => {
const provider1 = new TestProvider();
const provider2 = new TestProvider();

class MockStrategy extends FirstMatchStrategy {
override shouldTrackWithThisProvider = jest.fn().mockReturnValue(true);
}

const mockStrategy = new MockStrategy();

const multiProvider = new WebMultiProvider([{ provider: provider1 }, { provider: provider2 }], mockStrategy);

// Mock the status tracker to return READY status
const mockStatusTracker = {
providerStatus: jest.fn().mockReturnValue('READY'),
};
(multiProvider as any).statusTracker = mockStatusTracker;

multiProvider.track('purchase', context, trackingEventDetails);

expect(mockStrategy.shouldTrackWithThisProvider).toHaveBeenCalledWith(
{
provider: provider1,
providerName: 'TestProvider-1',
providerStatus: 'READY',
},
context,
'purchase',
trackingEventDetails,
);

expect(mockStrategy.shouldTrackWithThisProvider).toHaveBeenCalledWith(
{
provider: provider2,
providerName: 'TestProvider-2',
providerStatus: 'READY',
},
context,
'purchase',
trackingEventDetails,
);
});
});
});
});
Loading