|
19 | 19 |
|
20 | 20 | <!-- x-release-please-start-version --> |
21 | 21 |
|
22 | | - <a href="https://github.com/open-feature/python-sdk/releases/tag/v0.7.2"> |
23 | | - <img alt="Latest version" src="https://img.shields.io/static/v1?label=release&message=v0.7.2&color=blue&style=for-the-badge" /> |
| 22 | + <a href="https://github.com/open-feature/python-sdk/releases/tag/v0.7.4"> |
| 23 | + <img alt="Latest version" src="https://img.shields.io/static/v1?label=release&message=v0.7.4&color=blue&style=for-the-badge" /> |
24 | 24 | </a> |
25 | 25 |
|
26 | 26 | <!-- x-release-please-end --> |
|
60 | 60 | #### Pip install |
61 | 61 |
|
62 | 62 | ```bash |
63 | | -pip install openfeature-sdk==0.7.2 |
| 63 | +pip install openfeature-sdk==0.7.4 |
64 | 64 | ``` |
65 | 65 |
|
66 | 66 | #### requirements.txt |
67 | 67 |
|
68 | 68 | ```bash |
69 | | -openfeature-sdk==0.7.2 |
| 69 | +openfeature-sdk==0.7.4 |
70 | 70 | ``` |
71 | 71 |
|
72 | 72 | ```python |
@@ -99,16 +99,17 @@ print("Value: " + str(flag_value)) |
99 | 99 |
|
100 | 100 | ## 🌟 Features |
101 | 101 |
|
102 | | -| Status | Features | Description | |
103 | | -| ------ | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | |
104 | | -| ✅ | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. | |
105 | | -| ✅ | [Targeting](#targeting) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). | |
106 | | -| ✅ | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. | |
107 | | -| ✅ | [Logging](#logging) | Integrate with popular logging packages. | |
108 | | -| ✅ | [Domains](#domains) | Logically bind clients with providers. | |
109 | | -| ✅ | [Eventing](#eventing) | React to state changes in the provider or flag management system. | |
110 | | -| ✅ | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. | |
111 | | -| ✅ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. | |
| 102 | +| Status | Features | Description | |
| 103 | +|--------|---------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------| |
| 104 | +| ✅ | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. | |
| 105 | +| ✅ | [Targeting](#targeting) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). | |
| 106 | +| ✅ | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. | |
| 107 | +| ✅ | [Logging](#logging) | Integrate with popular logging packages. | |
| 108 | +| ✅ | [Domains](#domains) | Logically bind clients with providers. | |
| 109 | +| ✅ | [Eventing](#eventing) | React to state changes in the provider or flag management system. | |
| 110 | +| ✅ | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. | |
| 111 | +| ✅ | [Transaction Context Propagation](#transaction-context-propagation) | Set a specific [evaluation context](/docs/reference/concepts/evaluation-context) for a transaction (e.g. an HTTP request or a thread) | |
| 112 | +| ✅ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. | |
112 | 113 |
|
113 | 114 | <sub>Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌</sub> |
114 | 115 |
|
@@ -235,6 +236,86 @@ def on_provider_ready(event_details: EventDetails): |
235 | 236 | client.add_handler(ProviderEvent.PROVIDER_READY, on_provider_ready) |
236 | 237 | ``` |
237 | 238 |
|
| 239 | +### Transaction Context Propagation |
| 240 | + |
| 241 | +Transaction context is a container for transaction-specific evaluation context (e.g. user id, user agent, IP). |
| 242 | +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). |
| 243 | + |
| 244 | +You can implement a different transaction context propagator by implementing the `TransactionContextPropagator` class exported by the OpenFeature SDK. |
| 245 | +In most cases you can use `ContextVarsTransactionContextPropagator` as it works for `threads` and `asyncio` using [Context Variables](https://peps.python.org/pep-0567/). |
| 246 | + |
| 247 | +The following example shows a **multithreaded** Flask application using transaction context propagation to propagate the request ip and user id into request scoped transaction context. |
| 248 | + |
| 249 | +```python |
| 250 | +from flask import Flask, request |
| 251 | +from openfeature import api |
| 252 | +from openfeature.evaluation_context import EvaluationContext |
| 253 | +from openfeature.transaction_context import ContextVarsTransactionContextPropagator |
| 254 | + |
| 255 | +# Initialize the Flask app |
| 256 | +app = Flask(__name__) |
| 257 | + |
| 258 | +# Set the transaction context propagator |
| 259 | +api.set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) |
| 260 | + |
| 261 | +# Middleware to set the transaction context |
| 262 | +# You can call api.set_transaction_context anywhere you have information, |
| 263 | +# you want to have available in the code-paths below the current one. |
| 264 | +@app.before_request |
| 265 | +def set_request_transaction_context(): |
| 266 | + ip = request.headers.get("X-Forwarded-For", request.remote_addr) |
| 267 | + user_id = request.headers.get("User-Id") # Assuming we're getting the user ID from a header |
| 268 | + evaluation_context = EvaluationContext(targeting_key=user_id, attributes={"ipAddress": ip}) |
| 269 | + api.set_transaction_context(evaluation_context) |
| 270 | + |
| 271 | +def create_response() -> str: |
| 272 | + # This method can be anywhere in our code. |
| 273 | + # The feature flag evaluation will automatically contain the transaction context merged with other context |
| 274 | + new_response = api.get_client().get_string_value("response-message", "Hello User!") |
| 275 | + return f"Message from server: {new_response}" |
| 276 | + |
| 277 | +# Example route where we use the transaction context |
| 278 | +@app.route('/greeting') |
| 279 | +def some_endpoint(): |
| 280 | + return create_response() |
| 281 | +``` |
| 282 | + |
| 283 | +This also works for asyncio based implementations e.g. FastApi as seen in the following example: |
| 284 | + |
| 285 | +```python |
| 286 | +from fastapi import FastAPI, Request |
| 287 | +from openfeature import api |
| 288 | +from openfeature.evaluation_context import EvaluationContext |
| 289 | +from openfeature.transaction_context import ContextVarsTransactionContextPropagator |
| 290 | + |
| 291 | +# Initialize the FastAPI app |
| 292 | +app = FastAPI() |
| 293 | + |
| 294 | +# Set the transaction context propagator |
| 295 | +api.set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) |
| 296 | + |
| 297 | +# Middleware to set the transaction context |
| 298 | +@app.middleware("http") |
| 299 | +async def set_request_transaction_context(request: Request, call_next): |
| 300 | + ip = request.headers.get("X-Forwarded-For", request.client.host) |
| 301 | + user_id = request.headers.get("User-Id") # Assuming we're getting the user ID from a header |
| 302 | + evaluation_context = EvaluationContext(targeting_key=user_id, attributes={"ipAddress": ip}) |
| 303 | + api.set_transaction_context(evaluation_context) |
| 304 | + response = await call_next(request) |
| 305 | + return response |
| 306 | + |
| 307 | +def create_response() -> str: |
| 308 | + # This method can be located anywhere in our code. |
| 309 | + # The feature flag evaluation will automatically include the transaction context merged with other context. |
| 310 | + new_response = api.get_client().get_string_value("response-message", "Hello User!") |
| 311 | + return f"Message from server: {new_response}" |
| 312 | + |
| 313 | +# Example route where we use the transaction context |
| 314 | +@app.get('/greeting') |
| 315 | +async def some_endpoint(): |
| 316 | + return create_response() |
| 317 | +``` |
| 318 | + |
238 | 319 | ### Shutdown |
239 | 320 |
|
240 | 321 | The OpenFeature API provides a shutdown function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down. |
|
0 commit comments