-
Notifications
You must be signed in to change notification settings - Fork 46
feat: context propagation #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
beeme1mr
merged 4 commits into
open-feature:main
from
ssharaev:feat/transaction-context
Mar 29, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/dev/openfeature/sdk/NoOpTransactionContextPropagator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.openfeature.sdk; | ||
|
||
/** | ||
* A {@link TransactionContextPropagator} that simply returns empty context. | ||
*/ | ||
public class NoOpTransactionContextPropagator implements TransactionContextPropagator { | ||
|
||
/** | ||
* {@inheritDoc} | ||
* @return empty immutable context | ||
*/ | ||
@Override | ||
public EvaluationContext getTransactionContext() { | ||
return new ImmutableContext(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void setTransactionContext(EvaluationContext evaluationContext) { | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,9 +105,6 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key | |
FeatureProvider provider; | ||
|
||
try { | ||
final EvaluationContext apiContext; | ||
final EvaluationContext clientContext; | ||
|
||
// openfeatureApi.getProvider() must be called once to maintain a consistent reference | ||
provider = openfeatureApi.getProvider(this.name); | ||
|
||
|
@@ -117,19 +114,9 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key | |
hookCtx = HookContext.from(key, type, this.getMetadata(), | ||
provider.getMetadata(), ctx, defaultValue); | ||
|
||
// merge of: API.context, client.context, invocation.context | ||
apiContext = openfeatureApi.getEvaluationContext() != null | ||
? openfeatureApi.getEvaluationContext() | ||
: new ImmutableContext(); | ||
clientContext = this.getEvaluationContext() != null | ||
? this.getEvaluationContext() | ||
: new ImmutableContext(); | ||
|
||
EvaluationContext ctxFromHook = hookSupport.beforeHooks(type, hookCtx, mergedHooks, hints); | ||
|
||
EvaluationContext invocationCtx = ctx.merge(ctxFromHook); | ||
|
||
EvaluationContext mergedCtx = apiContext.merge(clientContext.merge(invocationCtx)); | ||
EvaluationContext mergedCtx = mergeEvaluationContext(ctxFromHook, ctx); | ||
|
||
ProviderEvaluation<T> providerEval = (ProviderEvaluation<T>) createProviderEvaluation(type, key, | ||
defaultValue, provider, mergedCtx); | ||
|
@@ -157,6 +144,29 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key | |
return details; | ||
} | ||
|
||
/** | ||
* Merge hook and invocation contexts with API, transaction and client contexts. | ||
* | ||
* @param hookContext hook context | ||
* @param invocationContext invocation context | ||
* @return merged evaluation context | ||
*/ | ||
private EvaluationContext mergeEvaluationContext( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice idea to extract this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! :) |
||
EvaluationContext hookContext, | ||
EvaluationContext invocationContext) { | ||
final EvaluationContext apiContext = openfeatureApi.getEvaluationContext() != null | ||
? openfeatureApi.getEvaluationContext() | ||
: new ImmutableContext(); | ||
final EvaluationContext clientContext = this.getEvaluationContext() != null | ||
? this.getEvaluationContext() | ||
: new ImmutableContext(); | ||
final EvaluationContext transactionContext = openfeatureApi.getTransactionContext() != null | ||
? openfeatureApi.getTransactionContext() | ||
: new ImmutableContext(); | ||
|
||
return apiContext.merge(transactionContext.merge(clientContext.merge(invocationContext.merge(hookContext)))); | ||
} | ||
|
||
private <T> ProviderEvaluation<?> createProviderEvaluation( | ||
FlagValueType type, | ||
String key, | ||
|
28 changes: 28 additions & 0 deletions
28
src/main/java/dev/openfeature/sdk/ThreadLocalTransactionContextPropagator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.openfeature.sdk; | ||
|
||
/** | ||
* A {@link ThreadLocalTransactionContextPropagator} is a transactional context propagator | ||
* that uses a ThreadLocal to persist a transactional context for the duration of a single thread. | ||
* | ||
* @see TransactionContextPropagator | ||
*/ | ||
public class ThreadLocalTransactionContextPropagator implements TransactionContextPropagator { | ||
|
||
private final ThreadLocal<EvaluationContext> evaluationContextThreadLocal = new ThreadLocal<>(); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public EvaluationContext getTransactionContext() { | ||
return this.evaluationContextThreadLocal.get(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void setTransactionContext(EvaluationContext evaluationContext) { | ||
this.evaluationContextThreadLocal.set(evaluationContext); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/dev/openfeature/sdk/TransactionContextPropagator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dev.openfeature.sdk; | ||
|
||
/** | ||
* {@link TransactionContextPropagator} is responsible for persisting a transactional context | ||
* for the duration of a single transaction. | ||
* Examples of potential transaction specific context include: a user id, user agent, IP. | ||
* Transaction context is merged with evaluation context prior to flag evaluation. | ||
* <p> | ||
* The precedence of merging context can be seen in | ||
* <a href=https://openfeature.dev/specification/sections/evaluation-context#requirement-323>the specification</a>. | ||
* </p> | ||
*/ | ||
public interface TransactionContextPropagator { | ||
|
||
/** | ||
* Returns the currently defined transaction context using the registered transaction | ||
* context propagator. | ||
* | ||
* @return {@link EvaluationContext} The current transaction context | ||
*/ | ||
EvaluationContext getTransactionContext(); | ||
|
||
/** | ||
* Sets the transaction context. | ||
*/ | ||
void setTransactionContext(EvaluationContext evaluationContext); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.