Skip to main content

Evaluation Context

The evaluation context is a container for arbitrary contextual data that can be used as a basis for dynamic evaluation. Static data such as the host or an identifier for the application can be configured globally. Dynamic evaluation context, such as the IP address of the client in a web application, can be implicitly propagated or explicitly passed to during flag evaluation, and can be merged with static values.

Providing Evaluation Context

Values relevant for flag evaluation can be included in the evaluation context at multiple points: globally (on the top level API), on the client, and at the point of flag evaluation (invocation).

// add a value to the global context
OpenFeature.setContext({ myGlobalKey: 'myGlobalValue' });

// add a value to the client context
const client = OpenFeature.getClient();
client.setContext({ myClientKey: 'myClientValue' });

// add a value to the invocation context
const context: EvaluationContext = {
myInvocationKey: 'myInvocationValue',
};
const boolValue = await client.getBooleanValue('boolFlag', false, context);

Context merging

At the point of flag evaluation, the evaluation context is merged, and duplicate values are overwritten as defined in the specification.

Circular structures in Evaluation Context

Many providers serialize the evaluation context as part of their operation. Be careful not to include circular structures in the evaluation context to avoid serialization issues.

Targeting Key

Many feature flag management systems require an identifier for the subject of flag evaluation. For many feature flag systems this is required in order to perform fractional evaluation or percentage-based rollouts deterministically. In the case of web applications or mobile apps, the subject is frequently an end user, but in other cases it could be a service or client application. The evaluation context includes an optional targeting key field for this purpose. The targeting key should contain a string uniquely identifying the subject (i.e.: a UUID, a hash of some user attribute such as an email, or the hostname of an application or service). Some providers may require this field to be set to function correctly.

Personally Identifiable Information (PII) Considerations

Be thoughtful in your inclusion of personal data in the evaluation context. Such data is useful for targeting and dynamic evaluation, but you should consider how the provider in use may handle or persist this data. Hooks (specifically hooks implementing the before stage) can be useful to restrict, filter or anonymize data in the evaluation context.