hyperlane-plugin-canary 23.3.2

A deterministic feature-flag, A/B testing, and progressive canary rollout plugin for the Hyperlane framework with XxHash3_64 basis-point sticky bucketing, header overrides, Context CanaryDecision injection, and X-Variant/Vary headers.
Documentation
use super::*;

/// Weighted variant arm inside an [`ExperimentRule`].
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct VariantWeight {
    /// Variant name (for example `"control"`, `"canary"`, `"treatment-b"`).
    pub(crate) name: String,
    /// Traffic share in basis points (`0..=10_000`, where `100` = `1.00%`).
    #[get(type(copy))]
    pub(crate) weight_bps: u16,
}

/// Rollout or A/B experiment definition scoped to a path prefix.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct ExperimentRule {
    /// Unique experiment or feature-flag name.
    pub(crate) name: String,
    /// Segment-bounded path prefix (`"/"` matches all routes).
    pub(crate) path_prefix: String,
    /// Weighted variant arms evaluated sequentially up to `10_000` bps.
    pub(crate) variants: Vec<VariantWeight>,
    /// Fallback variant returned when weights sum to less than `10_000`.
    pub(crate) default_variant: String,
}

/// Resolved assignment stored in [`Context`] under [`ATTR_CANARY_DECISION`].
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct CanaryDecision {
    /// Name of the matched experiment.
    pub(crate) experiment: String,
    /// Selected variant name.
    pub(crate) variant: String,
    /// Basis-point bucket (`0..10_000`) computed for the request (`0` when overridden).
    #[get(type(copy))]
    pub(crate) bucket: u16,
    /// `true` when forced via the configured override header.
    #[get(type(copy))]
    pub(crate) overridden: bool,
}

/// Configuration for [`CanaryPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct CanaryConfig {
    /// Ordered experiment rules (first matching path prefix wins).
    pub(crate) experiments: Vec<ExperimentRule>,
    /// Request header supplying sticky bucketing identity (default `x-user-id`).
    pub(crate) sticky_header: String,
    /// Optional fallback cookie name supplying sticky bucketing identity.
    pub(crate) sticky_cookie: Option<String>,
    /// Optional header allowing QA/ops to force a specific variant (`x-canary-override`).
    pub(crate) override_header: Option<String>,
    /// Response header name emitting the chosen variant (empty disables response header).
    pub(crate) variant_response_header: String,
    /// Whether to append sticky header to `Vary` when a variant is emitted.
    #[get(type(copy))]
    pub(crate) emit_vary: bool,
}

/// Compiled pre-indexed experiment arm for fast O(V) bucket selection.
#[derive(Clone, Debug)]
pub(crate) struct CompiledExperiment {
    /// Experiment name.
    pub(crate) name: String,
    /// Normalized path prefix.
    pub(crate) prefix: String,
    /// Cumulative `(upper_exclusive_bps, variant_name)` thresholds.
    pub(crate) cumulative: Box<[(u16, String)]>,
    /// Fallback variant name.
    pub(crate) default_variant: String,
}

/// Compiled canary & feature-flag evaluation engine backing [`CanaryRequestHook`] and [`CanaryResponseHook`].
#[derive(Clone, Debug)]
pub struct CanaryPlugin {
    /// Retained configuration.
    pub(crate) config: CanaryConfig,
    /// Compiled experiment rules sorted longest-prefix first.
    pub(crate) rules: Box<[CompiledExperiment]>,
    /// Lowercased sticky header name.
    pub(crate) sticky_header_lower: String,
    /// Optional lowercased forced override header name.
    pub(crate) override_header_lower: Option<String>,
    /// Lowercased response variant header name.
    pub(crate) variant_header_lower: String,
}

/// Opt-in request middleware assigning deterministic experiment variants into [`Context`].
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-88)]
pub struct CanaryRequestHook;

/// Opt-in response middleware emitting `X-Variant` and `Vary` headers for matched experiments.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::response_middleware(-68)]
pub struct CanaryResponseHook;