|
| 1 | +""" |
| 2 | +Options backed feature flagging. |
| 3 | +
|
| 4 | +Entry backed options. Will consume option data that is structured like |
| 5 | +
|
| 6 | +```yaml |
| 7 | +features: |
| 8 | + organizations:fury-mode: |
| 9 | + enabled: True |
| 10 | + name: sentry organizations |
| 11 | + owner: hybrid-cloud |
| 12 | + segments: |
| 13 | + - name: sentry orgs |
| 14 | + rollout: 50 |
| 15 | + conditions: |
| 16 | + - property: organization_slug |
| 17 | + name: internal organizations |
| 18 | + operator: |
| 19 | + kind: in |
| 20 | + value: ["sentry-test", "sentry"] |
| 21 | + - name: free accounts |
| 22 | + conditions: |
| 23 | + - property: subscription_is_free |
| 24 | + name: free subscriptions |
| 25 | + operator: |
| 26 | + kind: equals |
| 27 | + value: True |
| 28 | +``` |
| 29 | +
|
| 30 | +Each feature flag has a list of segments, each of which contain a list of conditions. |
| 31 | +If all the conditions for a segment match the evaluation context, a feature is granted. |
| 32 | +A segment with multiple conditions looks like: |
| 33 | +
|
| 34 | +```yaml |
| 35 | +features: |
| 36 | + organizations:fury-mode: |
| 37 | + enabled: True |
| 38 | + owner: hybrid-cloud |
| 39 | + description: sentry organizations |
| 40 | + segments: |
| 41 | + - name: sentry organizations |
| 42 | + rollout: 50 |
| 43 | + conditions: |
| 44 | + - name: internal orgs |
| 45 | + property: organization_slug |
| 46 | + operator: |
| 47 | + kind: in |
| 48 | + value: ["sentry-test", "sentry"] |
| 49 | + - name: allowed users |
| 50 | + property: user_email |
| 51 | + operator: |
| 52 | + kind: in |
| 53 | + value: ["mark@sentry.io", "gabe@sentry.io"] |
| 54 | +``` |
| 55 | +
|
| 56 | +Property names are arbitrary and read from an evaluation context |
| 57 | +prepared by the application. |
| 58 | +
|
| 59 | +Each condition has a single operator. An operator takes a kind (`OperatorKind` enum) |
| 60 | +and a value, the type of which depends on the operator specified. |
| 61 | +""" |
| 62 | +from __future__ import annotations |
| 63 | + |
| 64 | +from datetime import datetime |
| 65 | +from typing import Any |
| 66 | + |
| 67 | +from pydantic import BaseModel, Field, ValidationError, constr |
| 68 | + |
| 69 | +from flagpole.conditions import Segment |
| 70 | +from flagpole.evaluation_context import ContextBuilder, EvaluationContext |
| 71 | +from sentry.utils import json |
| 72 | + |
| 73 | + |
| 74 | +class InvalidFeatureFlagConfiguration(Exception): |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +class Feature(BaseModel): |
| 79 | + name: constr(min_length=1, to_lower=True) # type:ignore[valid-type] |
| 80 | + owner: constr(min_length=1) # type:ignore[valid-type] |
| 81 | + segments: list[Segment] |
| 82 | + """A list of segments to evaluate against the provided data""" |
| 83 | + enabled: bool = True |
| 84 | + """Defines whether or not the feature is enabled.""" |
| 85 | + created_at: datetime = Field(default_factory=datetime.now) |
| 86 | + """This datetime is when this instance was created. It can be used to decide when to re-read configuration data""" |
| 87 | + |
| 88 | + def match(self, context: EvaluationContext) -> bool: |
| 89 | + if self.enabled: |
| 90 | + for segment in self.segments: |
| 91 | + if segment.match(context): |
| 92 | + return True |
| 93 | + |
| 94 | + return False |
| 95 | + |
| 96 | + def dump_schema_to_file(self, file_path: str) -> None: |
| 97 | + with open(file_path, "w") as file: |
| 98 | + file.write(self.schema_json()) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def from_feature_dictionary(cls, name: str, config_dict: dict[str, Any]) -> Feature: |
| 102 | + try: |
| 103 | + feature = cls(name=name, **config_dict) |
| 104 | + except ValidationError as exc: |
| 105 | + raise InvalidFeatureFlagConfiguration("Provided JSON is not a valid feature") from exc |
| 106 | + |
| 107 | + return feature |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def from_feature_config_json( |
| 111 | + cls, name: str, config_json: str, context_builder: ContextBuilder | None = None |
| 112 | + ) -> Feature: |
| 113 | + try: |
| 114 | + config_data_dict = json.loads(config_json) |
| 115 | + except json.JSONDecodeError as decode_error: |
| 116 | + raise InvalidFeatureFlagConfiguration("Invalid feature json provided") from decode_error |
| 117 | + |
| 118 | + if not isinstance(config_data_dict, dict): |
| 119 | + raise InvalidFeatureFlagConfiguration("Feature JSON is not a valid feature") |
| 120 | + |
| 121 | + return cls.from_feature_dictionary(name=name, config_dict=config_data_dict) |
| 122 | + |
| 123 | + |
| 124 | +__all__ = ["Feature", "InvalidFeatureFlagConfiguration", "ContextBuilder", "EvaluationContext"] |
0 commit comments