Skip to content

Commit 366885c

Browse files
hhan2024Codex GPT-6
andcommitted
feat(ffe): support dependent flag evaluations on the shared engine
Carry prerequisite evaluation observations and successful-path exposure candidates through the native bridge. Commit OpenFeature exposures only after application hooks succeed, preserving the current runtime lifecycle. Environment: Datadog workspace Co-Authored-By: Codex GPT-6 <noreply@localhost>
1 parent d8b8064 commit 366885c

18 files changed

Lines changed: 980 additions & 51 deletions

‎components-rs/common.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ typedef struct ddog_FfeAttribute {
652652
bool bool_value;
653653
} ddog_FfeAttribute;
654654

655+
typedef void (*ddog_FfeDependencyCallback)(void*, ddog_CharSlice, struct ddog_FfeResult, bool);
656+
655657
/**
656658
* Flags selecting which Remote Config products/capabilities to subscribe to.
657659
*

‎components-rs/datadog.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ struct ddog_FfeResult ddog_ffe_evaluate(ddog_CharSlice flag_key,
328328
const struct ddog_FfeAttribute *attributes,
329329
uintptr_t attributes_count);
330330

331+
struct ddog_FfeResult ddog_ffe_evaluate_with_dependencies(ddog_CharSlice flag_key,
332+
int32_t expected_type,
333+
ddog_CharSlice targeting_key,
334+
const struct ddog_FfeAttribute *attributes,
335+
uintptr_t attributes_count,
336+
ddog_FfeDependencyCallback callback,
337+
void *callback_context);
338+
331339
void ddog_init_remote_config(struct ddog_RemoteConfigFlags flags);
332340

333341
struct ddog_RemoteConfigState *ddog_init_remote_config_state(const struct ddog_Endpoint *endpoint,

‎components-rs/tracer_runtime/ffe.rs‎

Lines changed: 197 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,54 @@ pub extern "C" fn ddog_ffe_evaluate(
488488
targeting_key: CharSlice<'_>,
489489
attributes: *const FfeAttribute<'_>,
490490
attributes_count: usize,
491+
) -> FfeResult {
492+
evaluate_with_dependencies(
493+
flag_key,
494+
expected_type,
495+
targeting_key,
496+
attributes,
497+
attributes_count,
498+
None,
499+
std::ptr::null_mut(),
500+
)
501+
}
502+
503+
pub fn supports_dependent_flags() -> bool {
504+
true
505+
}
506+
507+
pub type FfeDependencyCallback =
508+
extern "C" fn(*mut std::ffi::c_void, CharSlice<'_>, FfeResult, bool);
509+
510+
#[no_mangle]
511+
pub extern "C" fn ddog_ffe_evaluate_with_dependencies(
512+
flag_key: CharSlice<'_>,
513+
expected_type: i32,
514+
targeting_key: CharSlice<'_>,
515+
attributes: *const FfeAttribute<'_>,
516+
attributes_count: usize,
517+
callback: FfeDependencyCallback,
518+
callback_context: *mut std::ffi::c_void,
519+
) -> FfeResult {
520+
evaluate_with_dependencies(
521+
flag_key,
522+
expected_type,
523+
targeting_key,
524+
attributes,
525+
attributes_count,
526+
Some(callback),
527+
callback_context,
528+
)
529+
}
530+
531+
fn evaluate_with_dependencies(
532+
flag_key: CharSlice<'_>,
533+
expected_type: i32,
534+
targeting_key: CharSlice<'_>,
535+
attributes: *const FfeAttribute<'_>,
536+
attributes_count: usize,
537+
callback: Option<FfeDependencyCallback>,
538+
callback_context: *mut std::ffi::c_void,
491539
) -> FfeResult {
492540
if flag_key.as_raw_parts().0.is_null() {
493541
return invalid_result();
@@ -520,15 +568,13 @@ pub extern "C" fn ddog_ffe_evaluate(
520568
let context = EvaluationContext::new(targeting_key, Arc::new(attributes));
521569
let expects_object = matches!(expected_type, ExpectedFlagType::Object);
522570

523-
FFE_STATE.with(|state| {
571+
let (result, evaluations) = FFE_STATE.with(|state| {
524572
let state = state.borrow();
525-
let assignment = ffe::get_assignment(
526-
state.config.as_ref(),
527-
flag_key,
528-
&context,
529-
expected_type,
530-
ffe::now(),
531-
);
573+
let evaluation = match state.config.as_ref() {
574+
Some(config) => config.eval_flag_with_trace(flag_key, &context, expected_type, ffe::now(), ffe::EvaluationOptions::default()),
575+
None => ffe::EvaluationResult::from_error(EvaluationError::ConfigurationMissing),
576+
};
577+
let assignment = evaluation.result;
532578
if let Err(error) = &assignment {
533579
debug!("Feature Flags: native resolution failed (flagKey={flag_key:?}, configVersion={}): {error}", state.version);
534580
}
@@ -552,8 +598,30 @@ pub extern "C" fn ddog_ffe_evaluate(
552598
.config
553599
.as_ref()
554600
.is_some_and(Configuration::observe_full_evaluation_data);
555-
result
556-
})
601+
(result, evaluation.evaluations)
602+
});
603+
// Release the configuration borrow before calling into Zend; callbacks may reenter.
604+
if let Some(callback) = callback {
605+
for record in evaluations {
606+
let mut dependency = match record.result {
607+
Err(EvaluationError::FlagDisabled | EvaluationError::DefaultAllocationNull) => {
608+
error_result(ERROR_GENERAL, REASON_ERROR)
609+
}
610+
assignment => result_from_assignment(assignment),
611+
};
612+
dependency.observe_full_evaluation_data = result.observe_full_evaluation_data;
613+
let eligible = record.exposure_eligible
614+
&& result.error_code == ERROR_NONE
615+
&& result.variant.is_some();
616+
callback(
617+
callback_context,
618+
CharSlice::from(record.flag_key.as_str()),
619+
dependency,
620+
eligible,
621+
);
622+
}
623+
}
624+
result
557625
}
558626

559627
fn is_json_container(value: &AssignmentValue) -> bool {
@@ -851,6 +919,125 @@ mod tests {
851919
}
852920
}"#;
853921

922+
extern "C" fn collect_dependency(
923+
context: *mut std::ffi::c_void,
924+
key: CharSlice<'_>,
925+
result: FfeResult,
926+
eligible: bool,
927+
) {
928+
// SAFETY: tests pass an exclusive pointer to this vector for the synchronous call.
929+
let results = unsafe { &mut *context.cast::<Vec<(String, FfeResult, bool)>>() };
930+
results.push((key.try_to_utf8().unwrap().to_owned(), result, eligible));
931+
}
932+
933+
#[test]
934+
fn prerequisite_callbacks_keep_failed_and_rejected_evaluations() {
935+
setup_zend_string_functions();
936+
let wire = include_str!("../../tests/ext/ffe/dependent_flags.json");
937+
let json = CString::new(wire).unwrap();
938+
assert!(ddog_ffe_load_config(char_slice(&json)));
939+
for (key, expected_error, prerequisite, eligible) in [
940+
("root", ERROR_NONE, "parent", true),
941+
("rejected", ERROR_NONE, "parent", false),
942+
("missing", ERROR_GENERAL, "absent", false),
943+
("requires-disabled", ERROR_GENERAL, "disabled", false),
944+
] {
945+
let mut records: Vec<(String, FfeResult, bool)> = Vec::new();
946+
let result = ddog_ffe_evaluate_with_dependencies(
947+
CharSlice::from(key),
948+
TYPE_BOOLEAN,
949+
CharSlice::from("subject"),
950+
std::ptr::null(),
951+
0,
952+
collect_dependency,
953+
std::ptr::from_mut(&mut records).cast(),
954+
);
955+
assert_eq!(result.error_code, expected_error, "{key}");
956+
assert_eq!(
957+
records.len(),
958+
1,
959+
"{key}: memoization must not duplicate records"
960+
);
961+
assert_eq!(records[0].0, prerequisite);
962+
assert_eq!(records[0].2, eligible);
963+
assert!(!records[0].1.observe_full_evaluation_data);
964+
}
965+
clear_config();
966+
}
967+
968+
#[test]
969+
fn disabled_and_unassigned_prerequisites_are_errors_but_direct_results_are_not() {
970+
setup_zend_string_functions();
971+
let wire: serde_json::Value =
972+
serde_json::from_str(include_str!("../../tests/ext/ffe/dependent_flags.json")).unwrap();
973+
for (enabled, direct_reason) in [(false, REASON_DISABLED), (true, REASON_DEFAULT)] {
974+
let mut config = wire.clone();
975+
config["flags"]["parent"]["enabled"] = enabled.into();
976+
if enabled {
977+
config["flags"]["parent"]["allocations"] = serde_json::json!([]);
978+
}
979+
let json = CString::new(config.to_string()).unwrap();
980+
assert!(ddog_ffe_load_config(char_slice(&json)));
981+
let mut records: Vec<(String, FfeResult, bool)> = Vec::new();
982+
let result = ddog_ffe_evaluate_with_dependencies(
983+
CharSlice::from("root"),
984+
TYPE_BOOLEAN,
985+
CharSlice::from("subject"),
986+
std::ptr::null(),
987+
0,
988+
collect_dependency,
989+
std::ptr::from_mut(&mut records).cast(),
990+
);
991+
assert_eq!(result.error_code, ERROR_GENERAL);
992+
assert_eq!(records.len(), 1);
993+
assert_eq!(records[0].0, "parent");
994+
assert_eq!(records[0].1.error_code, ERROR_GENERAL);
995+
assert_eq!(records[0].1.reason, REASON_ERROR);
996+
assert!(records[0].1.variant.is_none());
997+
assert!(!records[0].2);
998+
999+
let direct = ddog_ffe_evaluate(
1000+
CharSlice::from("parent"),
1001+
TYPE_BOOLEAN,
1002+
CharSlice::from("subject"),
1003+
std::ptr::null(),
1004+
0,
1005+
);
1006+
assert_eq!(direct.error_code, ERROR_NONE);
1007+
assert_eq!(direct.reason, direct_reason);
1008+
assert!(direct.variant.is_none());
1009+
}
1010+
clear_config();
1011+
}
1012+
1013+
#[test]
1014+
fn scalar_json_root_suppresses_prerequisite_exposures() {
1015+
setup_zend_string_functions();
1016+
let mut wire: serde_json::Value =
1017+
serde_json::from_str(include_str!("../../tests/ext/ffe/dependent_flags.json")).unwrap();
1018+
wire["observeFullEvaluationData"] = true.into();
1019+
wire["flags"]["root"]["variationType"] = "JSON".into();
1020+
wire["flags"]["root"]["variations"] = serde_json::json!({"on":{"key":"on","value":42}});
1021+
let json = CString::new(wire.to_string()).unwrap();
1022+
assert!(ddog_ffe_load_config(char_slice(&json)));
1023+
let mut records: Vec<(String, FfeResult, bool)> = Vec::new();
1024+
let result = ddog_ffe_evaluate_with_dependencies(
1025+
CharSlice::from("root"),
1026+
TYPE_OBJECT,
1027+
CharSlice::from("subject"),
1028+
std::ptr::null(),
1029+
0,
1030+
collect_dependency,
1031+
std::ptr::from_mut(&mut records).cast(),
1032+
);
1033+
assert_eq!(result.error_code, ERROR_TYPE_MISMATCH);
1034+
assert_eq!(records.len(), 1);
1035+
assert!(!records[0].2);
1036+
assert!(records[0].1.observe_full_evaluation_data);
1037+
assert!(supports_dependent_flags());
1038+
clear_config();
1039+
}
1040+
8541041
#[test]
8551042
fn empty_targeting_key_is_not_dropped() {
8561043
setup_zend_string_functions();

‎components-rs/tracer_runtime/remote_config.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ pub unsafe extern "C" fn ddog_init_remote_config(flags: RemoteConfigFlags) {
173173
DATADOG_REMOTE_CONFIG_PRODUCTS.push(RemoteConfigProduct::FfeFlags);
174174
DATADOG_REMOTE_CONFIG_CAPABILITIES
175175
.push(RemoteConfigCapabilities::FfeFlagConfigurationRules);
176+
if crate::ffe::supports_dependent_flags() {
177+
DATADOG_REMOTE_CONFIG_CAPABILITIES
178+
.push(RemoteConfigCapabilities::FfeFlagDependentFlagVariantKeyV1);
179+
}
176180
}
177181

178182
if live_debugging_enabled {

‎src/DDTrace/OpenFeature/DataDogProvider.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function __construct(?LoggerInterface $logger = null)
6161
} else {
6262
NativeEvaluator::submitFlagEvaluation($observation);
6363
}
64-
}, false);
64+
}, false, function (array $batch): void {
65+
$recorder = $this->observationScope()->recorder;
66+
if ($recorder !== null) $recorder->captureExposures($batch);
67+
});
6568
if (function_exists('DDTrace\\Internal\\record_ffe_exposure')) {
6669
$this->exposureRecorder = 'DDTrace\\Internal\\record_ffe_exposure';
6770
}

‎src/DDTrace/OpenFeature/FlagEvaluationHook.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ final class FlagEvaluationHook implements Hook
2222
private ?self $previous = null;
2323
private ?array $observation = null;
2424
private ?array $exposure = null;
25+
private ?array $exposures = null;
2526
private ?ResolutionDetails $details = null;
2627
private int $startedAtMs;
2728
private static ?\WeakMap $exposureEvaluations = null;
@@ -61,6 +62,11 @@ public function capture(array $observation): void
6162
$this->observation = $observation;
6263
}
6364

65+
public function captureExposures(array $exposures): void
66+
{
67+
$this->exposures = $exposures;
68+
}
69+
6470
public function captureExposure(array $exposure): void
6571
{
6672
$this->exposure = $exposure;
@@ -90,6 +96,7 @@ public function after(HookContext $context, ResolutionDetails $details, HookHint
9096

9197
public function error(HookContext $context, \Throwable $error, HookHints $hints): void
9298
{
99+
$this->exposures = null;
93100
$this->observation = $this->observation ?? $this->protectedObservation($context);
94101
// Before/after failures replace the SDK result with the caller default.
95102
// Keep evaluated consent/time, but not the successful variant/allocation.
@@ -105,6 +112,9 @@ public function error(HookContext $context, \Throwable $error, HookHints $hints)
105112
public function finally(HookContext $context, HookHints $hints): void
106113
{
107114
try {
115+
if ($this->details !== null && $this->exposures !== null && $this->exposureRecorder !== null) {
116+
NativeEvaluator::submitExposures($this->exposures, $this->exposureRecorder);
117+
}
108118
$this->decideExposure($context);
109119
if ($this->observation !== null) {
110120
NativeEvaluator::submitFlagEvaluation($this->observation);
@@ -113,6 +123,7 @@ public function finally(HookContext $context, HookHints $hints): void
113123
$this->claim();
114124
$this->observation = null;
115125
$this->exposure = null;
126+
$this->exposures = null;
116127
$this->details = null;
117128
}
118129
}

0 commit comments

Comments
 (0)