|
| 1 | +""" |
| 2 | +FFAndE (Feature Flagging and Experimentation) product implementation. |
| 3 | +
|
| 4 | +This product receives feature flag configuration rules from Remote Configuration |
| 5 | +and processes them through the native FFAndE processor. |
| 6 | +""" |
| 7 | +import enum |
| 8 | +import json |
| 9 | +import typing as t |
| 10 | + |
| 11 | +from ddtrace import config |
| 12 | +from ddtrace.internal.ffande._native import process_ffe_configuration |
| 13 | +from ddtrace.internal.logger import get_logger |
| 14 | +from ddtrace.internal.remoteconfig import Payload |
| 15 | +from ddtrace.internal.remoteconfig._connectors import PublisherSubscriberConnector |
| 16 | +from ddtrace.internal.remoteconfig._publishers import RemoteConfigPublisher |
| 17 | +from ddtrace.internal.remoteconfig._pubsub import PubSub |
| 18 | +from ddtrace.internal.remoteconfig._subscribers import RemoteConfigSubscriber |
| 19 | + |
| 20 | + |
| 21 | +requires = ["remote-configuration"] |
| 22 | + |
| 23 | +log = get_logger(__name__) |
| 24 | + |
| 25 | +# FFAndE product name constant |
| 26 | +FFE_FLAGS_PRODUCT = "FFE_FLAGS" |
| 27 | + |
| 28 | + |
| 29 | +class FFAndECapabilities(enum.IntFlag): |
| 30 | + """FFAndE Remote Configuration capabilities.""" |
| 31 | + |
| 32 | + FFE_FLAG_CONFIGURATION_RULES = 1 << 46 |
| 33 | + |
| 34 | + |
| 35 | +class FFAndEAdapter(PubSub): |
| 36 | + """ |
| 37 | + FFAndE Remote Configuration adapter. |
| 38 | +
|
| 39 | + Receives feature flag configuration rules and forwards raw bytes to native processor. |
| 40 | + """ |
| 41 | + |
| 42 | + __publisher_class__ = RemoteConfigPublisher |
| 43 | + __subscriber_class__ = RemoteConfigSubscriber |
| 44 | + __shared_data__ = PublisherSubscriberConnector() |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + self._publisher = self.__publisher_class__(self.__shared_data__) |
| 48 | + self._subscriber = self.__subscriber_class__(self.__shared_data__, self.rc_callback, FFE_FLAGS_PRODUCT) |
| 49 | + |
| 50 | + # Register with Remote Configuration poller |
| 51 | + if config._remote_config_enabled: |
| 52 | + from ddtrace.internal.remoteconfig.worker import remoteconfig_poller |
| 53 | + |
| 54 | + remoteconfig_poller.register( |
| 55 | + FFE_FLAGS_PRODUCT, |
| 56 | + self, |
| 57 | + restart_on_fork=True, |
| 58 | + capabilities=[FFAndECapabilities.FFE_FLAG_CONFIGURATION_RULES], |
| 59 | + ) |
| 60 | + |
| 61 | + def rc_callback(self, payloads: t.Sequence[Payload]) -> None: |
| 62 | + """ |
| 63 | + Process FFE configuration payloads from Remote Configuration. |
| 64 | +
|
| 65 | + Args: |
| 66 | + payloads: Sequence of configuration payloads |
| 67 | + """ |
| 68 | + for payload in payloads: |
| 69 | + if payload.metadata is None: |
| 70 | + log.debug("Ignoring invalid FFE payload with no metadata, path: %s", payload.path) |
| 71 | + continue |
| 72 | + |
| 73 | + log.debug("Received FFE config payload: %s", payload.metadata.id) |
| 74 | + |
| 75 | + if payload.content is None: |
| 76 | + log.debug( |
| 77 | + "Received FFE config deletion, product: %s, path: %s", |
| 78 | + payload.metadata.product_name, |
| 79 | + payload.path, |
| 80 | + ) |
| 81 | + # Handle deletion/removal of configuration |
| 82 | + continue |
| 83 | + |
| 84 | + try: |
| 85 | + # Serialize payload content to bytes for native processing |
| 86 | + # The native function expects raw bytes, so we convert the dict to JSON |
| 87 | + config_json = json.dumps(payload.content, ensure_ascii=False) |
| 88 | + config_bytes = config_json.encode("utf-8") |
| 89 | + |
| 90 | + log.debug("Processing FFE config ID: %s, size: %d bytes", payload.metadata.id, len(config_bytes)) |
| 91 | + |
| 92 | + success = process_ffe_configuration(config_bytes) |
| 93 | + if success: |
| 94 | + log.debug("Successfully processed FFE config ID: %s", payload.metadata.id) |
| 95 | + else: |
| 96 | + log.warning("Failed to process FFE config ID: %s", payload.metadata.id) |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + log.error("Error processing FFE config payload: %s", e, exc_info=True) |
| 100 | + |
| 101 | + |
| 102 | +_adapter: t.Optional[FFAndEAdapter] = None |
| 103 | + |
| 104 | + |
| 105 | +def post_preload(): |
| 106 | + """Called during preload phase.""" |
| 107 | + pass |
| 108 | + |
| 109 | + |
| 110 | +def start(): |
| 111 | + """Start the FFAndE product and register with Remote Configuration.""" |
| 112 | + global _adapter |
| 113 | + |
| 114 | + if not config._remote_config_enabled: |
| 115 | + log.debug("Remote configuration disabled, FFAndE not started") |
| 116 | + return |
| 117 | + |
| 118 | + _adapter = FFAndEAdapter() |
| 119 | + |
| 120 | + log.debug("FFAndE product registered with Remote Configuration") |
| 121 | + |
| 122 | + |
| 123 | +def restart(join=False): |
| 124 | + """Restart the FFAndE product.""" |
| 125 | + pass |
| 126 | + |
| 127 | + |
| 128 | +def stop(join=False): |
| 129 | + """Stop the FFAndE product.""" |
| 130 | + global _adapter |
| 131 | + |
| 132 | + if not config._remote_config_enabled: |
| 133 | + return |
| 134 | + |
| 135 | + from ddtrace.internal.remoteconfig.worker import remoteconfig_poller |
| 136 | + |
| 137 | + remoteconfig_poller.unregister(FFE_FLAGS_PRODUCT) |
| 138 | + _adapter = None |
| 139 | + |
| 140 | + log.debug("FFAndE product unregistered from Remote Configuration") |
| 141 | + |
| 142 | + |
| 143 | +def at_exit(join=False): |
| 144 | + """Called at exit.""" |
| 145 | + stop(join=join) |
0 commit comments