@@ -160,6 +160,7 @@ func main() {
160
160
}
161
161
162
162
openfeature .SetProvider (flagd .NewProvider ())
163
+ openfeature .AddHooks (otelhooks .NewTracesHook ())
163
164
164
165
tracer = tp .Tracer ("checkoutservice" )
165
166
@@ -316,6 +317,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
316
317
317
318
// send to kafka only if kafka broker address is set
318
319
if cs .kafkaBrokerSvcAddr != "" {
320
+ log .Infof ("sending to postProcessor" )
319
321
cs .sendToPostProcessor (ctx , orderResult )
320
322
}
321
323
@@ -439,7 +441,7 @@ func (cs *checkoutService) convertCurrency(ctx context.Context, from *pb.Money,
439
441
440
442
func (cs * checkoutService ) chargeCard (ctx context.Context , amount * pb.Money , paymentInfo * pb.CreditCardInfo ) (string , error ) {
441
443
paymentService := cs .paymentSvcClient
442
- if cs .checkPaymentFailure (ctx ) {
444
+ if cs .isFeatureFlagEnabled (ctx , "paymentServiceUnreachable" ) {
443
445
badAddress := "badAddress:50051"
444
446
c := mustCreateClient (context .Background (), badAddress )
445
447
paymentService = pb .NewPaymentServiceClient (c )
@@ -505,6 +507,18 @@ func (cs *checkoutService) sendToPostProcessor(ctx context.Context, result *pb.O
505
507
cs .KafkaProducerClient .Input () <- & msg
506
508
successMsg := <- cs .KafkaProducerClient .Successes ()
507
509
log .Infof ("Successful to write message. offset: %v" , successMsg .Offset )
510
+
511
+ ffValue := cs .getIntFeatureFlag (ctx , "kafkaQueueProblems" )
512
+ if ffValue > 0 {
513
+ log .Infof ("Warning: FeatureFlag 'kafkaQueueProblems' is activated, overloading queue now." )
514
+ for i := 0 ; i < ffValue ; i ++ {
515
+ go func (i int ) {
516
+ cs .KafkaProducerClient .Input () <- & msg
517
+ _ = <- cs .KafkaProducerClient .Successes ()
518
+ }(i )
519
+ }
520
+ log .Infof ("Done with #%d messages for overload simulation." , ffValue )
521
+ }
508
522
}
509
523
510
524
func createProducerSpan (ctx context.Context , msg * sarama.ProducerMessage ) trace.Span {
@@ -533,11 +547,30 @@ func createProducerSpan(ctx context.Context, msg *sarama.ProducerMessage) trace.
533
547
return span
534
548
}
535
549
536
- func (cs * checkoutService ) checkPaymentFailure (ctx context.Context ) bool {
537
- openfeature .AddHooks (otelhooks .NewTracesHook ())
538
- client := openfeature .NewClient ("checkout" )
539
- failureEnabled , _ := client .BooleanValue (
540
- ctx , "paymentServiceUnreachable" , false , openfeature.EvaluationContext {},
541
- )
542
- return failureEnabled
550
+ func (cs * checkoutService ) isFeatureFlagEnabled (ctx context.Context , featureFlagName string ) bool {
551
+ client := openfeature .NewClient ("checkout" )
552
+
553
+ // Default value is set to false, but you could also make this a parameter.
554
+ featureEnabled , _ := client .BooleanValue (
555
+ ctx ,
556
+ featureFlagName ,
557
+ false ,
558
+ openfeature.EvaluationContext {},
559
+ )
560
+
561
+ return featureEnabled
562
+ }
563
+
564
+ func (cs * checkoutService ) getIntFeatureFlag (ctx context.Context , featureFlagName string ) int {
565
+ client := openfeature .NewClient ("checkout" )
566
+
567
+ // Default value is set to 0, but you could also make this a parameter.
568
+ featureFlagValue , _ := client .IntValue (
569
+ ctx ,
570
+ featureFlagName ,
571
+ 0 ,
572
+ openfeature.EvaluationContext {},
573
+ )
574
+
575
+ return int (featureFlagValue )
543
576
}
0 commit comments