|
| 1 | +package io.sentry.spring.boot.jakarta.integrationtest |
| 2 | + |
| 3 | +import dev.openfeature.sdk.OpenFeatureAPI |
| 4 | +import dev.openfeature.sdk.providers.memory.Flag |
| 5 | +import dev.openfeature.sdk.providers.memory.InMemoryProvider |
| 6 | +import io.sentry.openfeature.SentryOpenFeatureHook |
| 7 | +import org.springframework.context.annotation.Bean |
| 8 | +import org.springframework.context.annotation.Configuration |
| 9 | +import org.springframework.web.bind.annotation.GetMapping |
| 10 | +import org.springframework.web.bind.annotation.PathVariable |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping |
| 12 | +import org.springframework.web.bind.annotation.RestController |
| 13 | + |
| 14 | +open class FeatureFlagResponse(var flagKey: String? = null, var value: Boolean = false) |
| 15 | + |
| 16 | +@RestController |
| 17 | +@RequestMapping("/feature-flag/") |
| 18 | +open class FeatureFlagController(private val openFeature: OpenFeatureAPI) { |
| 19 | + @GetMapping("check/{flagKey}") |
| 20 | + open fun check(@PathVariable("flagKey") flagKey: String): FeatureFlagResponse = |
| 21 | + FeatureFlagResponse(flagKey, evaluate(flagKey)) |
| 22 | + |
| 23 | + @GetMapping("error/{flagKey}") |
| 24 | + open fun error(@PathVariable("flagKey") flagKey: String): String { |
| 25 | + val value = evaluate(flagKey) |
| 26 | + throw RuntimeException("Error occurred with feature flag: $flagKey = $value") |
| 27 | + } |
| 28 | + |
| 29 | + private fun evaluate(flagKey: String): Boolean = |
| 30 | + openFeature.client.getBooleanValue(flagKey, false) |
| 31 | +} |
| 32 | + |
| 33 | +@Configuration |
| 34 | +open class OpenFeatureConfig { |
| 35 | + /** Evaluations go through [SentryOpenFeatureHook], which records them on the current scope. */ |
| 36 | + @Bean |
| 37 | + open fun openFeatureAPI(): OpenFeatureAPI = |
| 38 | + OpenFeatureAPI.getInstance().apply { |
| 39 | + setProviderAndWait( |
| 40 | + InMemoryProvider( |
| 41 | + mapOf( |
| 42 | + "new-checkout-flow" to booleanFlag(true), |
| 43 | + "experimental-feature" to booleanFlag(false), |
| 44 | + "beta-features" to booleanFlag(true), |
| 45 | + ) |
| 46 | + ) |
| 47 | + ) |
| 48 | + addHooks(SentryOpenFeatureHook()) |
| 49 | + } |
| 50 | + |
| 51 | + private fun booleanFlag(value: Boolean): Flag<Boolean> = |
| 52 | + Flag.builder<Boolean>().variant("value", value).defaultVariant("value").build() |
| 53 | +} |
0 commit comments