The Octopus Deploy Python OpenFeature provider, to be used with the OpenFeature Python SDK. It evaluates Octopus feature flags in your Python application.
The provider is for server-side applications, such as web services, workers and command-line tools. It isn't intended for clients such as desktop applications.
The provider supports Python 3.11 and later. It's tested on every maintained CPython version.
pip install octopus-openfeature-providerThis also installs the OpenFeature SDK (openfeature-sdk), which your application uses to evaluate flags.
from openfeature import api
from octopus_openfeature_provider import (
OctopusFeatureConfiguration,
OctopusFeatureProvider,
ProductMetadata,
)
provider = OctopusFeatureProvider(
OctopusFeatureConfiguration(
client_identifier="YourClientIdentifier",
product_metadata=ProductMetadata("YourProductName", "1.0.0"),
)
)
# Wait for the provider at startup, so your first evaluations don't get the default value.
api.set_provider_and_wait(provider)
client = api.get_client()
if client.get_boolean_value("to-the-moon-feature", False):
print("🚀🚀🚀")Octopus feature flags are boolean, so evaluate them with get_boolean_value or get_boolean_details. Evaluating a flag as a string, integer, float or object returns your default value with an error code.
If the provider can't fetch your flags, for example because the client identifier is wrong or Octopus is unreachable, it still reports itself ready, and keeps trying every cache_duration. Until a fetch succeeds, every evaluation returns your default value with the PROVIDER_NOT_READY error code.
Call api.shutdown() when your application stops, so the provider releases its resources.
For more on evaluating flags, see the OpenFeature evaluation API documentation.
The provider checks for flag changes on a background thread, and a forked process doesn't get a copy of that thread. If you set the provider and then fork, flags in the forked processes stop updating. Instead, create and set a new provider in each process after it's forked:
from openfeature import api
from octopus_openfeature_provider import (
OctopusFeatureConfiguration,
OctopusFeatureProvider,
ProductMetadata,
)
# Call this from your server's per-worker startup hook, which runs after the fork.
def set_up_feature_flags() -> None:
provider = OctopusFeatureProvider(
OctopusFeatureConfiguration(
client_identifier="YourClientIdentifier",
product_metadata=ProductMetadata("YourProductName", "1.0.0"),
)
)
api.set_provider_and_wait(provider)If a process is forked while the provider is polling for changes, the provider logs a warning the first time that process evaluates a flag.
OctopusFeatureConfiguration takes these arguments. Pass everything after product_metadata by keyword.
| Argument | Required | Description |
|---|---|---|
client_identifier |
Yes | The client identifier from the Octopus variable Octopus.FeatureFlags.ClientIdentifier. |
product_metadata |
Yes | A ProductMetadata with the name and, optionally, version of your application, sent to Octopus for telemetry. |
cache_duration |
No | A timedelta for how often the provider checks for changed flags. Defaults to one minute. Must be positive. |
request_timeout |
No | A timedelta for how long each phase of a request to Octopus may take before it's abandoned. It doesn't limit a request's total time, so keep it well under cache_duration. Defaults to 10 seconds. Must be positive. |
release_version_override |
No | Overrides the release version embedded in the client identifier. |
http_client |
No | An httpx.Client (not an httpx.AsyncClient) for requests to Octopus, for example to configure a proxy, TLS or instrumentation. The provider never closes a client you supply. If the client has its own timeouts, the shorter of those and request_timeout applies. By default, the provider creates and manages its own client. |
The provider takes a copy of the configuration when it's created, so later changes to the configuration don't affect it. It removes leading and trailing whitespace, such as a newline from a file or mounted secret, from client_identifier and release_version_override. It raises ValueError or TypeError if either of those is empty, isn't a string, or contains characters other than printable ASCII, if cache_duration or request_timeout isn't a positive timedelta, or if http_client isn't an httpx.Client. ProductMetadata removes characters that aren't valid in an HTTP header token, and raises ValueError if the name, or a version you supply, has none left.
The provider logs through the standard logging module, under the octopus_openfeature_provider logger. It's silent unless your application configures logging.
Octopus Deploy is a sophisticated, best-of-breed continuous delivery (CD) platform for modern software teams. Octopus offers powerful release orchestration, deployment automation, and runbook automation, while handling the scale, complexity and governance expectations of even the largest organizations with the most complex deployment challenges.
See CONTRIBUTING.md to build and test locally.