|
| 1 | +package io.sentry.launchdarkly.android; |
| 2 | + |
| 3 | +import com.launchdarkly.sdk.EvaluationDetail; |
| 4 | +import com.launchdarkly.sdk.LDValue; |
| 5 | +import com.launchdarkly.sdk.LDValueType; |
| 6 | +import com.launchdarkly.sdk.android.integrations.EvaluationSeriesContext; |
| 7 | +import com.launchdarkly.sdk.android.integrations.Hook; |
| 8 | +import io.sentry.IScopes; |
| 9 | +import io.sentry.ScopesAdapter; |
| 10 | +import io.sentry.SentryIntegrationPackageStorage; |
| 11 | +import io.sentry.SentryLevel; |
| 12 | +import io.sentry.util.IntegrationUtils; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Objects; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +public final class SentryLaunchDarklyAndroidHook extends Hook { |
| 19 | + private final IScopes scopes; |
| 20 | + |
| 21 | + static { |
| 22 | + SentryIntegrationPackageStorage.getInstance() |
| 23 | + .addPackage("maven:io.sentry:sentry-launchdarkly-android", BuildConfig.VERSION_NAME); |
| 24 | + } |
| 25 | + |
| 26 | + public SentryLaunchDarklyAndroidHook() { |
| 27 | + this(ScopesAdapter.getInstance()); |
| 28 | + } |
| 29 | + |
| 30 | + public SentryLaunchDarklyAndroidHook(final @NotNull IScopes scopes) { |
| 31 | + super("SentryLaunchDarklyAndroidHook"); |
| 32 | + this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); |
| 33 | + addPackageAndIntegrationInfo(); |
| 34 | + } |
| 35 | + |
| 36 | + private void addPackageAndIntegrationInfo() { |
| 37 | + IntegrationUtils.addIntegrationToSdkVersion("LaunchDarkly-Android"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Map<String, Object> afterEvaluation( |
| 42 | + final EvaluationSeriesContext seriesContext, |
| 43 | + final Map<String, Object> seriesData, |
| 44 | + final EvaluationDetail<LDValue> evaluationDetail) { |
| 45 | + if (evaluationDetail == null || seriesContext == null) { |
| 46 | + return seriesData; |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + final @Nullable String flagKey = seriesContext.flagKey; |
| 51 | + final @Nullable LDValue value = evaluationDetail.getValue(); |
| 52 | + |
| 53 | + if (flagKey == null || value == null) { |
| 54 | + return seriesData; |
| 55 | + } |
| 56 | + |
| 57 | + if (LDValueType.BOOLEAN.equals(value.getType())) { |
| 58 | + final boolean flagValue = value.booleanValue(); |
| 59 | + scopes.addFeatureFlag(flagKey, flagValue); |
| 60 | + } |
| 61 | + } catch (final Exception e) { |
| 62 | + scopes |
| 63 | + .getOptions() |
| 64 | + .getLogger() |
| 65 | + .log(SentryLevel.ERROR, "Failed to capture feature flag evaluation", e); |
| 66 | + } |
| 67 | + |
| 68 | + return seriesData; |
| 69 | + } |
| 70 | +} |
0 commit comments