Class DedupingHook

java.lang.Object
com.launchdarkly.sdk.android.integrations.Hook
com.launchdarkly.sdk.android.integrations.DedupingHook

public final class DedupingHook extends Hook
Wraps a hook so that repeated evaluations resolving to the same result do not reach it again within a time window.

The wrapped hook is told about a flag when its result changes, and at most once per window while the result stays the same. This is useful for reducing the telemetry volume produced by frequent re-evaluations, for example a flag that is read on every redraw of a view. Deduplication is opt-in: a hook that is registered unwrapped observes every evaluation.

This class is not stable, and not subject to any backwards compatibility guarantees or semantic versioning. It is experimental.


     Components.hooks()
         .addHook(new MetricsHook())                                  // observes every evaluation
         .addHook(new DedupingHook(new ObservabilityHook()))           // default window
         .addHook(new DedupingHook(new TelemetryHook(), 60_000))
         .addHook(new DedupingHook(new ExperimentHook(), myCustomDeduper))
 

Two evaluations resolve to the same result when they agree on everything EvaluationExposureKey describes. Pass your own EvaluationExposureDeduper subclass to decide that differently.

An evaluation the SDK has no flag data for resolves to the default value, and is the same result as another that does. Evaluations made before the client has flags are of that kind, so the wrapped hook is told about one of them and then told about the flag again as soon as its data arrives.

A suppressed evaluation reaches neither Hook.beforeEvaluation(EvaluationSeriesContext, Map) nor Hook.afterEvaluation(EvaluationSeriesContext, Map, EvaluationDetail), because hooks pair their stages. The identify and track stages are always forwarded. Analytics events are unaffected: feature, debug, and summary events are still recorded for every evaluation, so the evaluation counts LaunchDarkly reports for your flags do not change.

What the wrapped hook has been told about is cleared by LDClient.identify(com.launchdarkly.sdk.LDContext), so the first evaluation of each flag after an identify always reaches it.

Give each hook its own instance unless you intend hooks to share a window: the first hook to be told about an evaluation starts the window that suppresses the rest.

Wrap outermost when you stack decorators. Suppressing an evaluation means returning series data that says so in place of what the stage was given, so a decorator outside this one does not get back what it stored in its own before stage. A decorator inside this one is unaffected, since a suppressed evaluation never reaches it.

  • Constructor Details

    • DedupingHook

      public DedupingHook(Hook delegate)
      Parameters:
      delegate - the hook to wrap
    • DedupingHook

      public DedupingHook(Hook delegate, int windowMillis)
      Parameters:
      delegate - the hook to wrap
      windowMillis - the dedupe window in milliseconds; zero or negative forwards every evaluation
    • DedupingHook

      public DedupingHook(Hook delegate, EvaluationExposureDeduper deduper)
      Parameters:
      delegate - the hook to wrap
      deduper - decides which evaluations reach the wrapped hook
  • Method Details

    • getMetadata

      public HookMetadata getMetadata()
      Overrides:
      getMetadata in class Hook
      Returns:
      the wrapped hook's metadata, so that the SDK names the hook a stage belongs to
    • beforeEvaluation

      public Map<String,Object> beforeEvaluation(EvaluationSeriesContext seriesContext, Map<String,Object> seriesData)
      Forwards the evaluation unless the wrapped hook has just been told about the same result.

      The decision is made here, before the evaluation runs, so that a suppressed evaluation reaches neither stage of the wrapped hook. An evaluation whose result the SDK did not describe, which is to say a series context built by something other than the SDK, is always forwarded.

      Overrides:
      beforeEvaluation in class Hook
      Parameters:
      seriesContext - container of parameters associated with this evaluation
      seriesData - immutable data from the previous stage in the evaluation series
      Returns:
      the wrapped hook's series data, or data marking the series as suppressed
    • afterEvaluation

      public Map<String,Object> afterEvaluation(EvaluationSeriesContext seriesContext, Map<String,Object> seriesData, EvaluationDetail<LDValue> evaluationDetail)
      Forwards the result unless this instance suppressed the series in its before stage.
      Overrides:
      afterEvaluation in class Hook
      Parameters:
      seriesContext - container of parameters associated with this evaluation
      seriesData - the data returned by this hook's before stage
      evaluationDetail - the result of the evaluation
      Returns:
      the wrapped hook's series data, unchanged if the series was suppressed
    • beforeIdentify

      public Map<String,Object> beforeIdentify(IdentifySeriesContext seriesContext, Map<String,Object> seriesData)
      Forgets which results the wrapped hook has been told about, then forwards the stage.

      Evaluations observed before an identify describe an earlier point in the application's lifecycle, so they are reported again afterwards. This happens even when the context is unchanged, so that identify is a reliable way for an application to mark a new phase of a session.

      Overrides:
      beforeIdentify in class Hook
      Parameters:
      seriesContext - container of parameters associated with this identify
      seriesData - immutable data from the previous stage in the identify series
      Returns:
      the wrapped hook's series data
    • afterIdentify

      public Map<String,Object> afterIdentify(IdentifySeriesContext seriesContext, Map<String,Object> seriesData, IdentifySeriesResult result)
      Description copied from class: Hook
      Hook.afterIdentify(IdentifySeriesContext, Map, IdentifySeriesResult) is called during the execution of the identify process, after the operation completes.

      This is currently the last stage of the identify series in the Hook, but that may not be the case in the future. To ensure forward compatibility, return the seriesData unmodified.

       
       String value = (String) seriesData.get("foo");
       doAThing(value);
       return seriesData;
       
       
      Overrides:
      afterIdentify in class Hook
      Parameters:
      seriesContext - Contains information about the evaluation being performed. This is not mutable.
      seriesData - A record associated with each stage of hook invocations. Each stage is called with the data of the previous stage for a series. The input record should not be modified.
      result - The result of the identify operation.
      Returns:
      a map containing custom data that will be carried through to the next stage of the series (if added in the future)
    • afterTrack

      public void afterTrack(TrackSeriesContext seriesContext)
      Description copied from class: Hook
      Hook.afterTrack(TrackSeriesContext) is called during the execution of the track process after the event has been enqueued.
      Overrides:
      afterTrack in class Hook
      Parameters:
      seriesContext - Contains information about the track operation being performed. This is not mutable.