Skip to content

Commit 95fcf02

Browse files
docs: use set_provider_and_wait in OpenFeature docs and example
1 parent 9b6d713 commit 95fcf02

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

OpenFeature.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ from devcycle_python_sdk import DevCycleLocalClient, DevCycleLocalOptions
2424
devcycle_client = DevCycleLocalClient("DEVCYCLE_SERVER_SDK_KEY", DevCycleLocalOptions())
2525

2626
# Set the initialzed DevCycle client as the provider for OpenFeature
27-
api.set_provider(devcycle_client.get_openfeature_provider())
27+
api.set_provider_and_wait(devcycle_client.get_openfeature_provider())
2828

2929
# Get the OpenFeature client
3030
open_feature_client = api.get_client()
@@ -37,6 +37,25 @@ api.set_evaluation_context(EvaluationContext(targeting_key="test-1234"))
3737
bool_flag = open_feature_client.get_boolean_value("bool-flag", False)
3838
```
3939

40+
#### Provider Initialization
41+
42+
Use `api.set_provider_and_wait()` rather than `api.set_provider()`. The DevCycle provider waits for the
43+
underlying DevCycle client to finish initializing and raises a `GeneralError` if it does not become ready
44+
in time. `set_provider_and_wait()` blocks until initialization completes and surfaces that error to the
45+
caller, so a failed startup is visible immediately.
46+
47+
`api.set_provider()` returns before the provider is ready. Initialization failures are reported only as a
48+
`PROVIDER_ERROR` event on a background thread, and until the provider is ready every evaluation returns
49+
your default value with the `PROVIDER_NOT_READY` error code. If you use `set_provider()`, register an error
50+
handler so those failures are not silently discarded:
51+
52+
```python
53+
from openfeature.event import ProviderEvent
54+
55+
api.add_handler(ProviderEvent.PROVIDER_ERROR, lambda details: logger.error(details.message))
56+
api.set_provider(devcycle_client.get_openfeature_provider())
57+
```
58+
4059
#### Required Targeting Key
4160

4261
For DevCycle SDK to work we require either a `targeting_key` or `user_id` attribute to be set on the OpenFeature context.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ This SDK provides an alpha implementation of the [OpenFeature](https://openfeatu
4848
from openfeature import api
4949

5050
devcycle_client = DevCycleLocalClient('DEVCYCLE_SERVER_SDK_KEY', options)
51-
api.set_provider(devcycle_client.get_openfeature_provider())
51+
api.set_provider_and_wait(devcycle_client.get_openfeature_provider())
5252
```
5353

54+
Use `set_provider_and_wait()` so that initialization failures are raised to the caller. `set_provider()` returns before the provider is ready, which means a failed initialization is only reported as a background `PROVIDER_ERROR` event while evaluations return their default values.
55+
5456
More details are in the [DevCycle Python SDK OpenFeature Provider](OpenFeature.md) guide.
5557

5658
> :warning: **OpenFeature support is in an early release and may have some rough edges**. Please report any issues to us and we'll be happy to help!

example/openfeature_example.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from openfeature import api
88
from openfeature.evaluation_context import EvaluationContext
9+
from openfeature.exception import GeneralError
910

1011
logger = logging.getLogger(__name__)
1112

@@ -48,7 +49,14 @@ def main():
4849
logger.info("DevCycle initialized successfully!\n")
4950

5051
# set the provider for OpenFeature
51-
api.set_provider(devcycle_client.get_openfeature_provider())
52+
# set_provider_and_wait blocks until the provider is ready and raises if it fails to
53+
# initialize. set_provider would return immediately and evaluations would silently
54+
# fall back to their default values until the provider became ready.
55+
try:
56+
api.set_provider_and_wait(devcycle_client.get_openfeature_provider())
57+
except GeneralError as e:
58+
logger.error(f"DevCycle OpenFeature provider failed to initialize: {e}")
59+
exit(1)
5260

5361
# get the OpenFeature client
5462
open_feature_client = api.get_client()

0 commit comments

Comments
 (0)