hyperlane_plugin_canary/struct.rs
1use super::*;
2
3/// Weighted variant arm inside an [`ExperimentRule`].
4#[derive(Clone, Debug, Eq, Getter, PartialEq)]
5pub struct VariantWeight {
6 /// Variant name (for example `"control"`, `"canary"`, `"treatment-b"`).
7 pub(crate) name: String,
8 /// Traffic share in basis points (`0..=10_000`, where `100` = `1.00%`).
9 #[get(type(copy))]
10 pub(crate) weight_bps: u16,
11}
12
13/// Rollout or A/B experiment definition scoped to a path prefix.
14#[derive(Clone, Data, Debug, Eq, PartialEq)]
15pub struct ExperimentRule {
16 /// Unique experiment or feature-flag name.
17 pub(crate) name: String,
18 /// Segment-bounded path prefix (`"/"` matches all routes).
19 pub(crate) path_prefix: String,
20 /// Weighted variant arms evaluated sequentially up to `10_000` bps.
21 pub(crate) variants: Vec<VariantWeight>,
22 /// Fallback variant returned when weights sum to less than `10_000`.
23 pub(crate) default_variant: String,
24}
25
26/// Resolved assignment stored in [`Context`] under [`ATTR_CANARY_DECISION`].
27#[derive(Clone, Debug, Eq, Getter, PartialEq)]
28pub struct CanaryDecision {
29 /// Name of the matched experiment.
30 pub(crate) experiment: String,
31 /// Selected variant name.
32 pub(crate) variant: String,
33 /// Basis-point bucket (`0..10_000`) computed for the request (`0` when overridden).
34 #[get(type(copy))]
35 pub(crate) bucket: u16,
36 /// `true` when forced via the configured override header.
37 #[get(type(copy))]
38 pub(crate) overridden: bool,
39}
40
41/// Configuration for [`CanaryPlugin`].
42#[derive(Clone, Data, Debug, Eq, PartialEq)]
43pub struct CanaryConfig {
44 /// Ordered experiment rules (first matching path prefix wins).
45 pub(crate) experiments: Vec<ExperimentRule>,
46 /// Request header supplying sticky bucketing identity (default `x-user-id`).
47 pub(crate) sticky_header: String,
48 /// Optional fallback cookie name supplying sticky bucketing identity.
49 pub(crate) sticky_cookie: Option<String>,
50 /// Optional header allowing QA/ops to force a specific variant (`x-canary-override`).
51 pub(crate) override_header: Option<String>,
52 /// Response header name emitting the chosen variant (empty disables response header).
53 pub(crate) variant_response_header: String,
54 /// Whether to append sticky header to `Vary` when a variant is emitted.
55 #[get(type(copy))]
56 pub(crate) emit_vary: bool,
57}
58
59/// Compiled pre-indexed experiment arm for fast O(V) bucket selection.
60#[derive(Clone, Debug)]
61pub(crate) struct CompiledExperiment {
62 /// Experiment name.
63 pub(crate) name: String,
64 /// Normalized path prefix.
65 pub(crate) prefix: String,
66 /// Cumulative `(upper_exclusive_bps, variant_name)` thresholds.
67 pub(crate) cumulative: Box<[(u16, String)]>,
68 /// Fallback variant name.
69 pub(crate) default_variant: String,
70}
71
72/// Compiled canary & feature-flag evaluation engine backing [`CanaryRequestHook`] and [`CanaryResponseHook`].
73#[derive(Clone, Debug)]
74pub struct CanaryPlugin {
75 /// Retained configuration.
76 pub(crate) config: CanaryConfig,
77 /// Compiled experiment rules sorted longest-prefix first.
78 pub(crate) rules: Box<[CompiledExperiment]>,
79 /// Lowercased sticky header name.
80 pub(crate) sticky_header_lower: String,
81 /// Optional lowercased forced override header name.
82 pub(crate) override_header_lower: Option<String>,
83 /// Lowercased response variant header name.
84 pub(crate) variant_header_lower: String,
85}
86
87/// Opt-in request middleware assigning deterministic experiment variants into [`Context`].
88#[derive(Clone, Copy, Debug, Default)]
89#[hyperlane::request_middleware(-88)]
90pub struct CanaryRequestHook;
91
92/// Opt-in response middleware emitting `X-Variant` and `Vary` headers for matched experiments.
93#[derive(Clone, Copy, Debug, Default)]
94#[hyperlane::response_middleware(-68)]
95pub struct CanaryResponseHook;
96
97/// Internal canary rule evaluation result prior to populating [`CanaryDecision`].
98#[derive(Debug)]
99pub(crate) struct CanaryRuleMatch {
100 /// Matched experiment index in `CanaryPlugin::rules`.
101 pub(crate) rule_idx: usize,
102 /// Optional matched cumulative arm index.
103 pub(crate) arm_idx: Option<usize>,
104 /// Optional forced override variant string.
105 pub(crate) override_variant: Option<String>,
106 /// Computed basis-point bucket.
107 pub(crate) bucket: u16,
108 /// Whether the variant came from a forced override header.
109 pub(crate) overridden: bool,
110}