Skip to content

Commit 2f85057

Browse files
cupofcatgruebel
andauthored
chore(flagd): Add sync metadata disabled (#211)
* chore(flagd): add disable metadata option Signed-off-by: Maks Osowski <[email protected]> * chore(flagd): Minimally refactor GrpcWatcher.listen() to pass ruff complexity pre-commit Signed-off-by: Maks Osowski <[email protected]> * Update providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/config.py Co-authored-by: Anton Grübel <[email protected]> Signed-off-by: cupofcat <[email protected]> --------- Signed-off-by: Maks Osowski <[email protected]> Signed-off-by: cupofcat <[email protected]> Co-authored-by: Anton Grübel <[email protected]>
1 parent 345e793 commit 2f85057

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def __init__( # noqa: PLR0913
100100
cert_path: typing.Optional[str] = None,
101101
default_authority: typing.Optional[str] = None,
102102
channel_credentials: typing.Optional[grpc.ChannelCredentials] = None,
103+
sync_metadata_disabled: typing.Optional[bool] = None,
103104
):
104105
self.host = env_or_default(ENV_VAR_HOST, DEFAULT_HOST) if host is None else host
105106

@@ -248,3 +249,11 @@ def __init__( # noqa: PLR0913
248249
)
249250

250251
self.channel_credentials = channel_credentials
252+
253+
# TODO: remove the metadata call entirely after https://github.com/open-feature/flagd/issues/1584
254+
# This is a temporary stop-gap solutions to support servers that don't implement sync.GetMetadata
255+
# (see: https://buf.build/open-feature/flagd/docs/main:flagd.sync.v1#flagd.sync.v1.FlagSyncService.GetMetadata).
256+
# Using this option disables call to sync.GetMetadata
257+
# Disabling will prevent static context from flagd being used in evaluations.
258+
# GetMetadata and this option will be removed.
259+
self.sync_metadata_disabled = sync_metadata_disabled

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/provider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__( # noqa: PLR0913
6464
cert_path: typing.Optional[str] = None,
6565
default_authority: typing.Optional[str] = None,
6666
channel_credentials: typing.Optional[grpc.ChannelCredentials] = None,
67+
sync_metadata_disabled: typing.Optional[bool] = None,
6768
):
6869
"""
6970
Create an instance of the FlagdProvider
@@ -106,6 +107,7 @@ def __init__( # noqa: PLR0913
106107
cert_path=cert_path,
107108
default_authority=default_authority,
108109
channel_credentials=channel_credentials,
110+
sync_metadata_disabled=sync_metadata_disabled,
109111
)
110112
self.enriched_context: dict = {}
111113

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process/connector/grpc_watcher.py

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

77
import grpc
88
from google.protobuf.json_format import MessageToDict
9+
from google.protobuf.struct_pb2 import Struct
910

1011
from openfeature.evaluation_context import EvaluationContext
1112
from openfeature.event import ProviderEventDetails
@@ -162,24 +163,36 @@ def shutdown(self) -> None:
162163
self.active = False
163164
self.channel.close()
164165

166+
def _create_request_args(self) -> dict:
167+
request_args = {}
168+
if self.selector is not None:
169+
request_args["selector"] = self.selector
170+
if self.provider_id is not None:
171+
request_args["provider_id"] = self.provider_id
172+
173+
return request_args
174+
165175
def listen(self) -> None:
166176
call_args = (
167177
{"timeout": self.streamline_deadline_seconds}
168178
if self.streamline_deadline_seconds > 0
169179
else {}
170180
)
171-
request_args = {}
172-
if self.selector is not None:
173-
request_args["selector"] = self.selector
174-
if self.provider_id is not None:
175-
request_args["provider_id"] = self.provider_id
181+
request_args = self._create_request_args()
176182

177183
while self.active:
178184
try:
179-
context_values_request = sync_pb2.GetMetadataRequest()
180-
context_values_response: sync_pb2.GetMetadataResponse = (
181-
self.stub.GetMetadata(context_values_request, wait_for_ready=True)
182-
)
185+
context_values_response: sync_pb2.GetMetadataResponse
186+
if self.config.sync_metadata_disabled:
187+
context_values_response = sync_pb2.GetMetadataResponse(
188+
metadata=Struct()
189+
)
190+
else:
191+
context_values_request = sync_pb2.GetMetadataRequest()
192+
context_values_response = self.stub.GetMetadata(
193+
context_values_request, wait_for_ready=True
194+
)
195+
183196
context_values = MessageToDict(context_values_response)
184197

185198
request = sync_pb2.SyncFlagsRequest(**request_args)

0 commit comments

Comments
 (0)