Skip to content
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

[FEATURE] Implement transaction context #256

Open
lukas-reining opened this issue Feb 25, 2024 · 5 comments
Open

[FEATURE] Implement transaction context #256

lukas-reining opened this issue Feb 25, 2024 · 5 comments
Labels
contribfest A good issue for Contribfest KubeCon EU '24 enhancement New feature or request good first issue Good for newcomers v0.8.0

Comments

@lukas-reining
Copy link
Member

lukas-reining commented Feb 25, 2024

We should implement transaction context propagation

What is transaction context?

From the spec:
Transaction context is a container for transaction-specific evaluation context (e.g. user id, user agent, IP).
Transaction context can be set where specific data is available (e.g. an auth service or request handler) and by using the transaction context propagator it will automatically be applied to all flag evaluations within a transaction (e.g. a request or thread).

Transaction context has been added in the following PR: open-feature/spec#227

Examples:

Implementation

The JS SDK implementation is implemented as a POC that is currently being hardened.
https://github.com/open-feature/js-sdk/blob/main/packages/server/src/transaction-context/transaction-context.ts

Usage

An example usage for the JS SDK implementation of the feature is the usage for propagating HTTP request information in the NestJS SDK.
https://github.com/open-feature/js-sdk/blob/main/packages/nest/src/evaluation-context-interceptor.ts#L31

@lukas-reining lukas-reining added enhancement New feature or request good first issue Good for newcomers labels Feb 25, 2024
@toddbaert toddbaert added the contribfest A good issue for Contribfest KubeCon EU '24 label Mar 11, 2024
@beeme1mr beeme1mr added this to the Spec 0.8 Compliance milestone Mar 15, 2024
@yardenlaif
Copy link

yardenlaif commented Apr 2, 2024

Is it expected that the user pass down the context? Because I'm not sure there's a trivial way in Go to have a context for a function call which isn't passed to it.
It's possible to create a context.Context which has our TransactionContext as a value and require the user to use the context.Context when getting the feature flag:

// Creating the context
ctx := NewTransactionContext(context.Background(), map[string]int{"userID": 12345})
next(ctx)

// Using the context
client.BooleanValue(ctx, flag, defaultValue)

The implementation would be something like:

func NewTransactionContext(ctx context.Context, attributes map[string]interface{}) {
    return context.WithValue(ctx, "transactioncontext", attributes)
}

func (c *Client) BooleanValue(ctx context.Context, flag string, defaultValue bool, options ...Option) {
    transactionContext := ctx.Value("transactioncontext")
   ...
}

@Kavindu-Dodan
Copy link
Contributor

Kavindu-Dodan commented Apr 2, 2024

@yardenlaif thanks for the interest in this issue :)

Is it expected that the user pass down the context?

Yes, context should be expected to be provided by the user. This maps with our current flag evaluation methods.

It's possible to create a context.Context which has our TransactionContext

+1 and we should expose it from openfeature api [1]

  // Derive transaction context
 openfeature.SetTransactionContext(ctx, value)

Regarding the key, use an empty struct instead of a string. For example, context.WithValue(ctx, key{}, "bla") where key is an empty struct to avoid hijacking.

[1] - https://github.com/open-feature/go-sdk?tab=readme-ov-file#usage

@beeme1mr
Copy link
Member

beeme1mr commented Apr 4, 2024

Thanks @Kavindu-Dodan! @lukas-reining @thomaspoignant do you have anything to add?

@lukas-reining
Copy link
Member Author

To me this looks like a plan @beeme1mr @Kavindu-Dodan @yardenlaif :)

@toddbaert
Copy link
Member

Yep, agreed. I think this is the idiom I would expect in Go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribfest A good issue for Contribfest KubeCon EU '24 enhancement New feature or request good first issue Good for newcomers v0.8.0
Projects
None yet
Development

No branches or pull requests

5 participants