-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy path_provider.py
More file actions
546 lines (457 loc) · 21.5 KB
/
_provider.py
File metadata and controls
546 lines (457 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
"""
Feature Flagging and Experimentation (FFE) product module.
This module handles Feature Flag configuration rules from Remote Configuration
and forwards the raw bytes to the native FFE processor.
"""
from collections import OrderedDict
from collections.abc import MutableMapping
from importlib.metadata import version
import threading
import typing
from openfeature.evaluation_context import EvaluationContext
from openfeature.event import ProviderEventDetails
from openfeature.exception import ErrorCode
from openfeature.flag_evaluation import FlagResolutionDetails
from openfeature.flag_evaluation import Reason
from openfeature.provider import Metadata
from openfeature.provider import ProviderStatus
from ddtrace.internal.logger import get_logger
from ddtrace.internal.native._native import ffe
from ddtrace.internal.openfeature._config import _get_ffe_config
from ddtrace.internal.openfeature._exposure import build_exposure_event
from ddtrace.internal.openfeature._flageval_metrics import METADATA_ALLOCATION_KEY
from ddtrace.internal.openfeature._flageval_metrics import FlagEvalHook
from ddtrace.internal.openfeature._flageval_metrics import FlagEvalMetrics
from ddtrace.internal.openfeature._native import VariationType
from ddtrace.internal.openfeature._native import resolve_flag
from ddtrace.internal.openfeature.writer import get_exposure_writer
from ddtrace.internal.openfeature.writer import start_exposure_writer
from ddtrace.internal.openfeature.writer import stop_exposure_writer
from ddtrace.internal.service import ServiceStatusError
from ddtrace.internal.settings.openfeature import config as ffe_config
# Handle different import paths between openfeature-sdk versions
# Versions 0.7.0+ reorganized submodules
pkg_version = version("openfeature-sdk")
if pkg_version >= "0.7.0":
from openfeature.provider import AbstractProvider
else:
from openfeature.provider.provider import AbstractProvider
T = typing.TypeVar("T", covariant=True)
K = typing.TypeVar("K")
V = typing.TypeVar("V")
logger = get_logger(__name__)
class LRUCache(MutableMapping, typing.Generic[K, V]):
"""LRU cache implementation using OrderedDict that implements the Mapping interface."""
def __init__(self, maxsize: int = 128):
self._cache: typing.OrderedDict[K, V] = OrderedDict()
self._maxsize = maxsize
def __getitem__(self, key: K) -> V:
"""Get value from cache, moving it to end (most recently used)."""
self._cache.move_to_end(key)
return self._cache[key]
def __setitem__(self, key: K, value: V) -> None:
"""Put value in cache, evicting least recently used if at capacity."""
if key in self._cache:
self._cache.move_to_end(key)
self._cache[key] = value
if len(self._cache) > self._maxsize:
self._cache.popitem(last=False) # Remove least recently used (first item)
def __delitem__(self, key: K) -> None:
"""Delete key from cache."""
del self._cache[key]
def __iter__(self) -> typing.Iterator[K]:
"""Iterate over cache keys."""
return iter(self._cache)
def __len__(self) -> int:
"""Return number of items in cache."""
return len(self._cache)
class DataDogProvider(AbstractProvider):
"""
Datadog OpenFeature Provider.
Implements the OpenFeature provider interface for Datadog's
Feature Flags and Experimentation (FFE) product.
"""
def __init__(self, *args: typing.Any, initialization_timeout: typing.Optional[float] = None, **kwargs: typing.Any):
super().__init__(*args, **kwargs)
self._metadata = Metadata(name="Datadog")
self._status = ProviderStatus.NOT_READY
# Initialization timeout: constructor arg takes priority, then env var (default 30s)
if initialization_timeout is not None:
self._initialization_timeout = initialization_timeout
else:
self._initialization_timeout = ffe_config.initialization_timeout_ms / 1000.0
# Event used to block initialize() until config arrives.
# Also serves as the "config received" flag via is_set().
self._config_received = threading.Event()
# Cache for reported exposures to prevent duplicates
# Stores mapping of (flag_key, subject_id) -> (allocation_key, variant_key)
# Using LRU cache with maxsize of 65536 to prevent unbounded memory growth
self._exposure_cache: LRUCache[tuple[str, str], tuple[typing.Optional[str], typing.Optional[str]]] = LRUCache(
maxsize=65536
)
# Check if experimental flagging provider is enabled
self._enabled = ffe_config.experimental_flagging_provider_enabled
if not self._enabled:
logger.warning(
"openfeature: experimental flagging provider is not enabled, "
"please set DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true to enable it",
)
# Initialize flag evaluation metrics tracking
# Metrics are emitted via OTel when DD_METRICS_OTEL_ENABLED=true
self._flag_eval_metrics: typing.Optional[FlagEvalMetrics] = None
self._flag_eval_hook: typing.Optional[FlagEvalHook] = None
if self._enabled:
self._flag_eval_metrics = FlagEvalMetrics()
self._flag_eval_hook = FlagEvalHook(self._flag_eval_metrics)
def get_metadata(self) -> Metadata:
"""Returns provider metadata."""
return self._metadata
def get_provider_hooks(self) -> list[typing.Any]:
"""
Returns provider-level hooks.
The flag evaluation hook is registered here to track metrics for
every flag evaluation via the finally_after hook stage.
"""
hooks: list[typing.Any] = []
if self._flag_eval_hook is not None:
hooks.append(self._flag_eval_hook)
return hooks
def initialize(self, evaluation_context: EvaluationContext) -> None:
"""
Initialize the provider.
Blocks until Remote Config delivers the first FFE configuration or
the initialization timeout expires.
The timeout is configurable via:
- Constructor: DataDogProvider(initialization_timeout=10.0) # seconds
- Env var: DD_EXPERIMENTAL_FLAGGING_PROVIDER_INITIALIZATION_TIMEOUT_MS=10000
Provider lifecycle:
NOT_READY -> initialize() blocks -> config arrives -> READY
NOT_READY -> initialize() blocks -> timeout -> raises ProviderNotReadyError
"""
if not self._enabled:
return
# Register for RC config callbacks (in initialize, not __init__, so
# re-initialization after shutdown re-registers the provider)
_register_provider(self)
try:
# Start the exposure writer for reporting
start_exposure_writer()
except ServiceStatusError:
logger.debug("Exposure writer is already running", exc_info=True)
# Fast path: config already available (RC delivered before set_provider)
config = _get_ffe_config()
if config is not None:
logger.debug("FFE configuration already available, provider is READY")
self._config_received.set()
self._status = ProviderStatus.READY
return # SDK will dispatch PROVIDER_READY
# Block until config arrives or timeout expires
logger.debug(
"Waiting up to %.1fs for initial FFE configuration from Remote Config", self._initialization_timeout
)
if not self._config_received.wait(timeout=self._initialization_timeout):
# Timeout expired without receiving config
from openfeature.exception import ProviderNotReadyError
raise ProviderNotReadyError(
f"Provider timed out after {self._initialization_timeout:.1f}s waiting for "
"initial configuration from Remote Config"
)
# Config received during wait -- on_configuration_received() already set status
def shutdown(self) -> None:
"""
Shutdown the provider.
Called by the OpenFeature SDK when the provider is being replaced or shutdown.
"""
if not self._enabled:
return
try:
# Stop the exposure writer
stop_exposure_writer()
except ServiceStatusError:
logger.debug("Exposure writer has already stopped", exc_info=True)
# Shutdown flag evaluation metrics
if self._flag_eval_metrics is not None:
self._flag_eval_metrics.shutdown()
self._flag_eval_metrics = None
self._flag_eval_hook = None
# Clear exposure cache
self.clear_exposure_cache()
# Unregister provider
_unregister_provider(self)
self._status = ProviderStatus.NOT_READY
self._config_received.clear()
def resolve_boolean_details(
self,
flag_key: str,
default_value: bool,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[bool]:
return self._resolve_details(flag_key, default_value, evaluation_context, VariationType.Boolean)
def resolve_string_details(
self,
flag_key: str,
default_value: str,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[str]:
return self._resolve_details(flag_key, default_value, evaluation_context, VariationType.String)
def resolve_integer_details(
self,
flag_key: str,
default_value: int,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[int]:
return self._resolve_details(flag_key, default_value, evaluation_context, VariationType.Integer)
def resolve_float_details(
self,
flag_key: str,
default_value: float,
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[float]:
return self._resolve_details(flag_key, default_value, evaluation_context, VariationType.Float)
def resolve_object_details(
self,
flag_key: str,
default_value: typing.Union[dict, list],
evaluation_context: typing.Optional[EvaluationContext] = None,
) -> FlagResolutionDetails[typing.Union[dict, list]]:
return self._resolve_details(flag_key, default_value, evaluation_context, VariationType.Object)
def _resolve_details(
self,
flag_key: str,
default_value: typing.Any,
evaluation_context: typing.Optional[EvaluationContext] = None,
variation_type: VariationType = VariationType.Boolean,
) -> FlagResolutionDetails[T]:
"""
Core resolution logic for all flag types.
Follows OpenFeature spec:
- Returns flag value with reason and variant on success
- Returns default value with DEFAULT reason when no configuration is available
- Returns default value with ERROR reason and FLAG_NOT_FOUND error_code when
flag is not found in the configuration
- Returns error with error_code and error_message on other errors
"""
# If provider is not enabled, return default value
if not self._enabled:
return FlagResolutionDetails(
value=default_value,
reason=Reason.DISABLED,
variant=None,
)
try:
# Get the native Configuration object
config = _get_ffe_config()
# Resolve flag using native implementation
details = resolve_flag(
config,
flag_key=flag_key,
context=evaluation_context,
expected_type=variation_type,
)
# No configuration available - return error with PROVIDER_NOT_READY code
# Note: No exposure logging when configuration is missing
if details is None:
return FlagResolutionDetails(
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.PROVIDER_NOT_READY,
error_message="No FFE configuration loaded",
)
# Handle errors from native evaluation
if details.error_code is not None:
# Map native error code to OpenFeature error code
openfeature_error_code = self._map_error_code_to_openfeature(details.error_code)
# Flag not found - return default with ERROR reason and error_code
if details.error_code == ffe.ErrorCode.FlagNotFound:
# Only report exposure if do_log is explicitly True
if details.do_log:
self._report_exposure(
flag_key=flag_key,
variant_key=None,
allocation_key=None,
evaluation_context=evaluation_context,
)
return FlagResolutionDetails(
value=default_value,
reason=Reason.ERROR,
error_code=openfeature_error_code,
error_message="Flag not found",
)
# Other errors - return default with ERROR reason
return FlagResolutionDetails(
value=default_value,
reason=Reason.ERROR,
error_code=openfeature_error_code,
error_message=details.error_message or "Unknown error",
)
# Map native ffe.Reason to OpenFeature Reason
reason = self._map_reason_to_openfeature(details.reason)
# Report exposure event only if do_log flag is True
if details.do_log:
self._report_exposure(
flag_key=flag_key,
variant_key=details.variant,
allocation_key=details.allocation_key,
evaluation_context=evaluation_context,
)
# Build flag_metadata with allocation_key if present
flag_metadata: dict[str, typing.Any] = {}
if details.allocation_key:
flag_metadata[METADATA_ALLOCATION_KEY] = details.allocation_key
# Check if variant is None/empty to determine if we should use default value.
# For JSON flags, value can be null which is valid, so we check variant instead.
# We preserve the reason from evaluation (could be DEFAULT, DISABLED, etc.)
if not details.variant:
return FlagResolutionDetails(
value=default_value,
reason=reason,
variant=None,
flag_metadata=flag_metadata,
)
# Success - return resolved value (which may be None for JSON flags)
return FlagResolutionDetails(
value=details.value,
reason=reason,
variant=details.variant,
flag_metadata=flag_metadata,
)
except Exception as e:
# Unexpected errors
return FlagResolutionDetails(
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.GENERAL,
error_message=f"Unexpected error during flag evaluation: {str(e)}",
)
def _report_exposure(
self,
flag_key: str,
variant_key: typing.Optional[str],
allocation_key: typing.Optional[str],
evaluation_context: typing.Optional[EvaluationContext],
) -> None:
"""
Report a feature flag exposure event to the EVP proxy intake.
Uses caching to prevent duplicate exposure events for the same
(flag_key, subject_id, variant_key, allocation_key) combination.
Note: This method should only be called when exposure logging is enabled.
Callers must check the do_log flag before invoking this method.
Args:
flag_key: The feature flag key
variant_key: The variant key returned by evaluation
allocation_key: The allocation key
evaluation_context: The evaluation context with subject information
"""
try:
exposure_event = build_exposure_event(
flag_key=flag_key,
variant_key=variant_key,
allocation_key=allocation_key,
evaluation_context=evaluation_context,
)
if not exposure_event:
return
# Check cache to prevent duplicate exposure events
key = (flag_key, exposure_event["subject"]["id"])
value = (allocation_key, variant_key)
cached_value = self._exposure_cache.get(key, None)
if cached_value and cached_value == value:
logger.debug("Skipping duplicate exposure event for %s->%s", key, value)
return
writer = get_exposure_writer()
writer.enqueue(exposure_event)
# Add to cache only after successful enqueue
self._exposure_cache[key] = value
except Exception as e:
logger.debug("Failed to report exposure event: %s", e, exc_info=True)
def _map_reason_to_openfeature(self, native_reason) -> Reason:
"""Map native ffe.Reason to OpenFeature Reason."""
# Handle string reasons from fallback dict implementation
if isinstance(native_reason, str):
string_map = {
"STATIC": Reason.STATIC,
"TARGETING_MATCH": Reason.TARGETING_MATCH,
"SPLIT": Reason.SPLIT,
}
return string_map.get(native_reason, Reason.UNKNOWN)
# Map native ffe.Reason enum to OpenFeature Reason
if native_reason == ffe.Reason.Static:
return Reason.STATIC
elif native_reason == ffe.Reason.TargetingMatch:
return Reason.TARGETING_MATCH
elif native_reason == ffe.Reason.Split:
return Reason.SPLIT
elif native_reason == ffe.Reason.Default:
return Reason.DEFAULT
elif native_reason == ffe.Reason.Cached:
return Reason.CACHED
elif native_reason == ffe.Reason.Disabled:
return Reason.DISABLED
elif native_reason == ffe.Reason.Error:
return Reason.ERROR
elif native_reason == ffe.Reason.Stale:
return Reason.STALE
else:
return Reason.UNKNOWN
def _map_error_code_to_openfeature(self, native_error_code) -> ErrorCode:
"""Map native ffe.ErrorCode to OpenFeature ErrorCode."""
if native_error_code == ffe.ErrorCode.TypeMismatch:
return ErrorCode.TYPE_MISMATCH
elif native_error_code == ffe.ErrorCode.ParseError:
return ErrorCode.PARSE_ERROR
elif native_error_code == ffe.ErrorCode.FlagNotFound:
return ErrorCode.FLAG_NOT_FOUND
elif native_error_code == ffe.ErrorCode.TargetingKeyMissing:
return ErrorCode.TARGETING_KEY_MISSING
elif native_error_code == ffe.ErrorCode.InvalidContext:
return ErrorCode.INVALID_CONTEXT
elif native_error_code == ffe.ErrorCode.ProviderNotReady:
return ErrorCode.PROVIDER_NOT_READY
elif native_error_code == ffe.ErrorCode.General:
return ErrorCode.GENERAL
else:
return ErrorCode.GENERAL
def on_configuration_received(self) -> None:
"""
Called when a Remote Configuration payload is received and processed.
Updates status first, then signals the event to unblock initialize().
Emits PROVIDER_READY for late arrivals (config received after initialize() timed out).
"""
if not self._config_received.is_set():
self._status = ProviderStatus.READY
logger.debug("First FFE configuration received, provider is now READY")
# Emit READY for late recovery: config arrived after init timed out
self._emit_ready_event()
# Signal the event last to unblock initialize() after status is updated
self._config_received.set()
def _emit_ready_event(self) -> None:
"""
Safely emit PROVIDER_READY event.
Handles SDK version compatibility - emit_provider_ready() only exists in SDK 0.7.0+.
"""
if hasattr(self, "emit_provider_ready") and ProviderEventDetails is not None:
self.emit_provider_ready(ProviderEventDetails())
else:
# SDK 0.6.0 doesn't have emit methods
logger.debug("Provider status is READY (event emission not supported in SDK 0.6.0)")
def clear_exposure_cache(self) -> None:
"""
Clear the exposure event cache.
This method is useful for testing to ensure fresh exposure events are sent.
"""
self._exposure_cache.clear()
logger.debug("Exposure cache cleared")
# Module-level registry for active provider instances
_provider_instances: list[DataDogProvider] = []
def _register_provider(provider: DataDogProvider) -> None:
"""Register a provider instance for configuration callbacks."""
if provider not in _provider_instances:
_provider_instances.append(provider)
def _unregister_provider(provider: DataDogProvider) -> None:
"""Unregister a provider instance."""
if provider in _provider_instances:
_provider_instances.remove(provider)
def _notify_providers_config_received() -> None:
"""Notify all registered providers that configuration was received."""
for provider in _provider_instances:
provider.on_configuration_received()