Skip to content

chore(flagd): Add sync metadata disabled #211

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

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__( # noqa: PLR0913
cert_path: typing.Optional[str] = None,
default_authority: typing.Optional[str] = None,
channel_credentials: typing.Optional[grpc.ChannelCredentials] = None,
sync_metadata_disabled: typing.Optional[bool] = None,
):
self.host = env_or_default(ENV_VAR_HOST, DEFAULT_HOST) if host is None else host

Expand Down Expand Up @@ -248,3 +249,11 @@ def __init__( # noqa: PLR0913
)

self.channel_credentials = channel_credentials

# TODO: remove the metadata call entirely after https://github.com/open-feature/flagd/issues/1584
# This is a temporary stop-gap solutions to support servers that don't implement sync.GetMetadata
# (see: https://buf.build/open-feature/flagd/docs/main:flagd.sync.v1#flagd.sync.v1.FlagSyncService.GetMetadata).
# Using this option disables call to sync.GetMetadata
# Disabling will prevent static context from flagd being used in evaluations.
# GetMetadata and this option will be removed.
self.sync_metadata_disabled = sync_metadata_disabled
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__( # noqa: PLR0913
cert_path: typing.Optional[str] = None,
default_authority: typing.Optional[str] = None,
channel_credentials: typing.Optional[grpc.ChannelCredentials] = None,
sync_metadata_disabled: typing.Optional[bool] = None,
):
"""
Create an instance of the FlagdProvider
Expand Down Expand Up @@ -106,6 +107,7 @@ def __init__( # noqa: PLR0913
cert_path=cert_path,
default_authority=default_authority,
channel_credentials=channel_credentials,
sync_metadata_disabled=sync_metadata_disabled,
)
self.enriched_context: dict = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import grpc
from google.protobuf.json_format import MessageToDict
from google.protobuf.struct_pb2 import Struct

from openfeature.evaluation_context import EvaluationContext
from openfeature.event import ProviderEventDetails
Expand Down Expand Up @@ -162,24 +163,36 @@
self.active = False
self.channel.close()

def _create_request_args(self) -> dict:
request_args = {}
if self.selector is not None:
request_args["selector"] = self.selector
if self.provider_id is not None:
request_args["provider_id"] = self.provider_id

Check warning on line 171 in providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process/connector/grpc_watcher.py

View check run for this annotation

Codecov / codecov/patch

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

Added line #L171 was not covered by tests

return request_args

def listen(self) -> None:
call_args = (
{"timeout": self.streamline_deadline_seconds}
if self.streamline_deadline_seconds > 0
else {}
)
request_args = {}
if self.selector is not None:
request_args["selector"] = self.selector
if self.provider_id is not None:
request_args["provider_id"] = self.provider_id
request_args = self._create_request_args()

while self.active:
try:
context_values_request = sync_pb2.GetMetadataRequest()
context_values_response: sync_pb2.GetMetadataResponse = (
self.stub.GetMetadata(context_values_request, wait_for_ready=True)
)
context_values_response: sync_pb2.GetMetadataResponse
if self.config.sync_metadata_disabled:
context_values_response = sync_pb2.GetMetadataResponse(

Check warning on line 187 in providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/process/connector/grpc_watcher.py

View check run for this annotation

Codecov / codecov/patch

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

Added line #L187 was not covered by tests
metadata=Struct()
)
else:
context_values_request = sync_pb2.GetMetadataRequest()
context_values_response = self.stub.GetMetadata(
context_values_request, wait_for_ready=True
)

context_values = MessageToDict(context_values_response)

request = sync_pb2.SyncFlagsRequest(**request_args)
Expand Down