11--TEST--
2- FFE OpenFeature commits prerequisite exposures only after all hooks succeed
2+ FFE OpenFeature finalizes root metrics and prerequisite exposures after hooks
33--SKIPIF--
44<?php
55if (PHP_VERSION_ID < 80100 ) die ('skip: fibers require PHP 8.1+ ' );
@@ -42,7 +42,13 @@ $committed = [];
4242DDTrace \install_hook ('DDTrace \\Internal \\record_ffe_exposure ' , function ($ hook ) use (&$ committed ) {
4343 $ committed [] = $ hook ->args ;
4444});
45- $ provider = new DDTrace \OpenFeature \DataDogProvider ();
45+ $ metrics = [];
46+ $ provider = DDTrace \OpenFeature \DataDogProvider ::createWithDependencies (null , null ,
47+ function ($ metric ) use (&$ metrics ) {
48+ $ metrics [] = [$ metric ->getFlagKey (), $ metric ->getVariant (), $ metric ->getReason (),
49+ $ metric ->getErrorCode (), $ metric ->getAllocationKey ()];
50+ return true ;
51+ });
4652$ hook = new FfeOutcomeHook ();
4753$ client = ffeOutcomeClient ($ provider , $ hook );
4854$ customerExposures = [];
@@ -52,25 +58,30 @@ OpenFeature\OpenFeatureAPI::getInstance()->addHooks(new DDTrace\OpenFeature\Expo
5258 }
5359));
5460$ context = new OpenFeature \implementation \flags \EvaluationContext ('same-subject ' , new OpenFeature \implementation \flags \Attributes (['plan ' => 'pro ' ]));
55- $ hook ->onAfter = function () use (&$ committed ) {
61+ $ hook ->onAfter = function () use (&$ committed, & $ metrics ) {
5662 if ($ committed !== []) throw new LogicException ('exposures committed before after hooks ' );
63+ if ($ metrics !== []) throw new LogicException ('root metric committed before after hooks ' );
5764 throw new RuntimeException ('reject result ' );
5865};
5966$ details = $ client ->getStringDetails ('root ' , 'fallback ' , $ context );
6067echo 'after_error= ' ; var_dump ($ details ->getValue () === 'fallback ' && $ committed === [] && queuedExposureCount () === 0
6168 && array_map (function ($ r ) { return [$ r [0 ], $ r [5 ]]; }, $ ffeRecords ) === [['parent ' , null ], ['root ' , 'GENERAL ' ]]
6269 && $ customerExposures === [['root ' , false , []]]);
70+ echo 'after_error_metric= ' ; var_dump ($ metrics === [['root ' , null , 'ERROR ' , 'GENERAL ' , null ]]);
6371
6472$ ffeRecords = [];
6573$ customerExposures = [];
66- $ hook ->onAfter = function () use (&$ committed ) {
74+ $ metrics = [];
75+ $ hook ->onAfter = function () use (&$ committed , &$ metrics ) {
6776 if ($ committed !== []) throw new LogicException ('exposures committed before after hooks ' );
77+ if ($ metrics !== []) throw new LogicException ('root metric committed before after hooks ' );
6878};
6979$ details = $ client ->getStringDetails ('root ' , 'fallback ' , $ context );
7080echo 'retry= ' ; var_dump ($ details ->getValue () === 'on ' && count ($ committed ) === 2 && queuedExposureCount () === 2
7181 && $ committed [0 ] === ['parent ' , 'same-subject ' , ['plan ' => 'pro ' ], 'parent-allocation ' , 'on ' , 7 , ['parent-exposure ' => true ]]
7282 && $ committed [1 ] === ['root ' , 'same-subject ' , ['plan ' => 'pro ' ], 'root-allocation ' , 'on ' , 0 , ['root-exposure ' => true ]]);
7383echo 'root_only_hook= ' ; var_dump ($ customerExposures === [['root ' , true , ['root-hook ' => 'root ' ]]]);
84+ echo 'success_metric= ' ; var_dump ($ metrics === [['root ' , 'on ' , 'TARGETING_MATCH ' , null , 'root-allocation ' ]]);
7485
7586$ committed = [];
7687$ ffeRecords = [];
@@ -83,13 +94,16 @@ echo 'features_and_cache='; var_dump(count($committed) === 2 && queuedExposureCo
8394$ committed = [];
8495$ ffeRecords = [];
8596$ hook ->onAfter = null ;
97+ $ metrics = [];
8698$ hook ->onBefore = function () { throw new RuntimeException ('before failed ' ); };
8799$ details = $ client ->getStringDetails ('root ' , 'fallback ' , $ context );
88100echo 'before_error= ' ; var_dump ($ details ->getValue () === 'fallback ' && $ committed === []
89101 && queuedExposureCount () === 2 && count ($ ffeRecords ) === 1 && $ ffeRecords [0 ][0 ] === 'root ' && $ ffeRecords [0 ][5 ] === 'GENERAL ' );
102+ echo 'before_error_metric= ' ; var_dump ($ metrics === [['root ' , null , 'ERROR ' , 'GENERAL ' , null ]]);
90103$ hook ->onBefore = null ;
91104
92105// Direct-provider calls follow the OpenFeature contract: no exposure without its invocation hook.
106+ $ metrics = [];
93107$ hook ->onAfter = function ($ context ) use ($ provider ) {
94108 if ($ context ->getFlagKey () === 'root ' ) {
95109 $ provider ->resolveStringValue ('parent ' , 'fallback ' );
@@ -98,9 +112,14 @@ $hook->onAfter = function ($context) use ($provider) {
98112};
99113$ client ->getStringDetails ('root ' , 'fallback ' , $ context );
100114echo 'nested= ' ; var_dump ($ committed === []);
115+ echo 'nested_metrics= ' ; var_dump ($ metrics === [
116+ ['parent ' , 'on ' , 'STATIC ' , null , 'parent-allocation ' ],
117+ ['root ' , null , 'ERROR ' , 'GENERAL ' , null ],
118+ ]);
101119
102120// Suspend after native evaluation, then complete another invocation on the same provider.
103121$ committed = [];
122+ $ metrics = [];
104123$ hook ->onAfter = function ($ context ) {
105124 if ($ context ->getFlagKey () === 'root ' ) {
106125 Fiber ::suspend ();
@@ -113,28 +132,59 @@ $second = new Fiber(function () use ($client) {
113132});
114133$ first ->start ();
115134echo 'suspended= ' ; var_dump ($ committed === []);
135+ echo 'suspended_metric= ' ; var_dump ($ metrics === []);
116136$ second ->start ();
117137$ first ->resume ();
118138echo 'fibers= ' ; var_dump ($ first ->getReturn () === 'fallback ' && $ second ->getReturn () === 'on '
119139 && count ($ committed ) === 1 && $ committed [0 ][0 ] === 'parent ' && $ committed [0 ][1 ] === 'other-subject ' );
140+ echo 'fiber_metrics= ' ; var_dump ($ metrics === [
141+ ['parent ' , 'on ' , 'STATIC ' , null , 'parent-allocation ' ],
142+ ['root ' , null , 'ERROR ' , 'GENERAL ' , null ],
143+ ]);
120144
121145// A provider failure keeps its prerequisite observation without an exposure.
122146$ committed = [];
123147$ ffeRecords = [];
148+ $ metrics = [];
124149$ hook ->onAfter = null ;
125150$ details = $ client ->getStringDetails ('missing ' , 'fallback ' , $ context );
126151echo 'graph_error= ' ; var_dump ($ details ->getValue () === 'fallback ' && $ committed === []
127152 && count ($ ffeRecords ) === 2 && $ ffeRecords [0 ][5 ] === 'FLAG_NOT_FOUND ' && $ ffeRecords [1 ][5 ] === 'GENERAL ' );
153+ echo 'graph_error_metric= ' ; var_dump ($ metrics === [['missing ' , null , 'ERROR ' , 'GENERAL ' , null ]]);
154+
155+ // Preserve SDK error codes from hooks, including after a successful resolution.
156+ $ metrics = [];
157+ $ hook ->onAfter = function () {
158+ throw new class extends RuntimeException implements OpenFeature \interfaces \provider \ThrowableWithResolutionError {
159+ public function getResolutionError (): OpenFeature \interfaces \provider \ResolutionError {
160+ return new OpenFeature \implementation \provider \ResolutionError (
161+ OpenFeature \interfaces \provider \ErrorCode ::INVALID_CONTEXT ()
162+ );
163+ }
164+ };
165+ };
166+ $ details = $ client ->getStringDetails ('root ' , 'fallback ' , $ context );
167+ echo 'typed_hook_error= ' ; var_dump ($ details ->getValue () === 'fallback '
168+ && $ details ->getError ()->getResolutionErrorCode ()->getValue () === 'INVALID_CONTEXT '
169+ && $ metrics === [['root ' , null , 'ERROR ' , 'INVALID_CONTEXT ' , null ]]);
128170?>
129171--EXPECT--
130172after_error=bool(true)
173+ after_error_metric=bool(true)
131174retry=bool(true)
132175root_only_hook=bool(true)
176+ success_metric=bool(true)
133177features_and_cache=bool(true)
134178before_error=bool(true)
179+ before_error_metric=bool(true)
135180nested=bool(true)
181+ nested_metrics=bool(true)
136182suspended=bool(true)
183+ suspended_metric=bool(true)
137184fibers=bool(true)
185+ fiber_metrics=bool(true)
138186graph_error=bool(true)
187+ graph_error_metric=bool(true)
188+ typed_hook_error=bool(true)
139189--CLEAN--
140190<?php @unlink (__DIR__ . '/dependent_exposures_final_outcome.debug.log ' ); ?>
0 commit comments