|
| 1 | +# SPDX-FileCopyrightText: 2025-present Datadog, Inc. <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from functools import cached_property |
| 7 | +from typing import TYPE_CHECKING, Any, Optional |
| 8 | + |
| 9 | +from dda.feature_flags.client import DatadogFeatureFlag |
| 10 | +from dda.utils.ci import running_in_ci |
| 11 | +from dda.utils.user import User |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from dda.cli.application import Application |
| 15 | + from dda.config.model import RootConfig |
| 16 | + |
| 17 | + |
| 18 | +class FeatureFlagUser(User): |
| 19 | + def __init__(self, config: RootConfig) -> None: |
| 20 | + super().__init__(config) |
| 21 | + |
| 22 | + |
| 23 | +class FeatureFlagManager: |
| 24 | + """ |
| 25 | + A class for querying feature flags. This is available as the |
| 26 | + [`Application.ff`][dda.cli.application.Application.ff] property. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, app: Application) -> None: |
| 30 | + self.__app = app |
| 31 | + |
| 32 | + self.__started = False |
| 33 | + |
| 34 | + if self.client_token is not None: |
| 35 | + self.__ff_client = DatadogFeatureFlag(self.client_token) |
| 36 | + self.__started = True |
| 37 | + |
| 38 | + self.__cache: dict[tuple[str, str, tuple[tuple[str, str],...]], Any] = {} |
| 39 | + |
| 40 | + @cached_property |
| 41 | + def client_token(self) -> str | None: |
| 42 | + from contextlib import suppress |
| 43 | + |
| 44 | + from dda.utils.secrets.secrets import fetch_client_token, read_client_token, save_client_token |
| 45 | + |
| 46 | + client_token: str | None = None |
| 47 | + with suppress(Exception): |
| 48 | + client_token = read_client_token() |
| 49 | + if not client_token: |
| 50 | + client_token = fetch_client_token() |
| 51 | + save_client_token(client_token) |
| 52 | + |
| 53 | + return client_token |
| 54 | + |
| 55 | + @property |
| 56 | + def user(self) -> FeatureFlagUser: |
| 57 | + return FeatureFlagUser(self.__app.config) |
| 58 | + |
| 59 | + def get_targeting_key(self) -> str: |
| 60 | + if running_in_ci(): |
| 61 | + import os |
| 62 | + |
| 63 | + return os.getenv("CI_JOB_ID", "default_job_id") |
| 64 | + |
| 65 | + return self.user.machine_id |
| 66 | + |
| 67 | + def check_flag(self, flag_key: str, default_value: Any, extra_attributes: Optional[dict[str, str]] = None) -> bool: |
| 68 | + if not self.__started: |
| 69 | + return default_value |
| 70 | + |
| 71 | + targeting_key = self.get_targeting_key() |
| 72 | + targeting_attributes = self._get_base_context() |
| 73 | + if extra_attributes is not None: |
| 74 | + targeting_attributes.update(extra_attributes) |
| 75 | + |
| 76 | + attributes_items = targeting_attributes.items() |
| 77 | + tuple_attributes = tuple(((key, value) for key, value in sorted(attributes_items))) |
| 78 | + |
| 79 | + self.__app.display_debug( |
| 80 | + f"Checking flag {flag_key} with targeting key {targeting_key} and targeting attributes {tuple_attributes}" |
| 81 | + ) |
| 82 | + flag_value = self._check_flag(flag_key, targeting_key, tuple_attributes) |
| 83 | + if flag_value is None: |
| 84 | + return default_value |
| 85 | + return flag_value |
| 86 | + |
| 87 | + def _check_flag(self, flag_key: str, targeting_key: str, targeting_attributes: tuple[tuple[str, str],...]) -> bool: |
| 88 | + cache_key = (flag_key, targeting_key, targeting_attributes) |
| 89 | + if cache_key in self.__cache: |
| 90 | + return self.__cache[cache_key] |
| 91 | + |
| 92 | + context = { |
| 93 | + "targeting_key": targeting_key, |
| 94 | + "targeting_attributes": dict(targeting_attributes), |
| 95 | + } |
| 96 | + |
| 97 | + flag_value = self.__ff_client.get_flag_value(flag_key, context) |
| 98 | + self.__cache[cache_key] = flag_value |
| 99 | + return flag_value |
| 100 | + |
| 101 | + def _get_base_context(self) -> dict[str, str]: |
| 102 | + return { |
| 103 | + "platform": "toto", |
| 104 | + "ci": "true" if running_in_ci() else "false", |
| 105 | + "env": "prod", |
| 106 | + "user": self.user.email, |
| 107 | + } |
0 commit comments