open-feature/java-sdk

[FEATURE] Notify FeatureProvider on context changed

Closed this issue · 2 comments

Requirement 2.6.1
The provider MAY define an on context changed function, which takes an argument for the previous context and the newly set context, in order to respond to an evaluation context change.

I would like to implement my own FeatureProvider and for me it is crucial to be notified when a new evaluation context is defined.
Looking at the code, I believe that only three small changes would be needed:

OpenFeatureAPI.java

    public void setEvaluationContext(EvaluationContext evaluationContext) {
        try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
            EvaluationContext previousContext = this.evaluationContext;
            this.evaluationContext = evaluationContext;
            this.providerRepository.notifyEvaluationContextChanged(previousContext, evaluationContext);
        }
    }

ProviderRepository.java

    /**
     * Notify all FeatureProviders that a new global evaluation context has been set
     */
    public void notifyEvaluationContextChanged(EvaluationContext previousContext, EvaluationContext newContext) {
        Stream.concat(Stream.of(this.defaultProvider.get()), this.providers.values().stream())
                .distinct()
                .forEach(provider -> provider.onContextChanged(previousContext, newContext));
    }

FeatureProvider.java

    /**
     * Requirement 2.6.1
     * The provider MAY define an on context changed function, which takes an argument for the previous context
     * and the newly set context, in order to respond to an evaluation context change.
     * <p>
     * This method is called every time a global evaluation context is set
     */
    default void onContextChanged(EvaluationContext previousContext, EvaluationContext newContext){
        // Intentionally left blank
    }

Is it possible to make these changes?
I would be happy to open a Pull Request, but unfortunately I don't have permission :/.

Thanks in advance!

@brunomendoncapicpay thank you for raising your concern and welcome to OpenFeature project.

Regarding Spec 2.6.1 my understanding is that this mainly concerns the static context paradigm. For example, this is implemented at js-sdk as web providers prefer static context paradigm for flag evaluations.
However, I don't see a reason to not expose such method but I would like the opinion of @toddbaert or @beeme1mr as they are the experts with regards to the specificaiton.

I would be happy to open a Pull Request, but unfortunately I don't have permission :/.

We use fork and pull request strategy for contributions 1. You can fork this repository, work on a branch in your clone and then contribute to upstream once your solution is ready.

Footnotes

  1. https://docs.github.com/en/get-started/exploring-projects-on-github/contributing-to-a-project

As @Kavindu-Dodan , this doesn't apply to the Java provider, because the Java SDK implements the Dynamic context paradigm. The context is different every evaluation; there may be a global context that doesn't change often, but it's merged with the context that's passed to the provider in every evaluation (see spec 3.2 Context levels and merging). An on context changed handler doesn't make sense in this SDK, since a fully "up-to-date" context is passed to every resolver call, and every call could represent a totally different user/client.