nimbus/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::{enrollment::ExperimentEnrollment, EnrolledFeature, EnrollmentStatus};
use serde_derive::{Deserialize, Serialize};

pub trait MetricsHandler: Send + Sync {
    fn record_enrollment_statuses(&self, enrollment_status_extras: Vec<EnrollmentStatusExtraDef>);

    #[cfg(feature = "stateful")]
    fn record_feature_activation(&self, event: FeatureExposureExtraDef);

    #[cfg(feature = "stateful")]
    fn record_feature_exposure(&self, event: FeatureExposureExtraDef);

    #[cfg(feature = "stateful")]
    fn record_malformed_feature_config(&self, event: MalformedFeatureConfigExtraDef);
}

#[derive(Serialize, Deserialize, Clone)]
pub struct EnrollmentStatusExtraDef {
    pub branch: Option<String>,
    pub conflict_slug: Option<String>,
    pub error_string: Option<String>,
    pub reason: Option<String>,
    pub slug: Option<String>,
    pub status: Option<String>,
    #[cfg(not(feature = "stateful"))]
    pub user_id: Option<String>,
}

#[cfg(test)]
impl EnrollmentStatusExtraDef {
    pub fn branch(&self) -> &str {
        self.branch.as_ref().unwrap()
    }

    pub fn conflict_slug(&self) -> &str {
        self.conflict_slug.as_ref().unwrap()
    }

    pub fn error_string(&self) -> &str {
        self.error_string.as_ref().unwrap()
    }

    pub fn reason(&self) -> &str {
        self.reason.as_ref().unwrap()
    }

    pub fn slug(&self) -> &str {
        self.slug.as_ref().unwrap()
    }

    pub fn status(&self) -> &str {
        self.status.as_ref().unwrap()
    }

    #[cfg(not(feature = "stateful"))]
    pub fn user_id(&self) -> &str {
        self.user_id.as_ref().unwrap()
    }
}

impl From<ExperimentEnrollment> for EnrollmentStatusExtraDef {
    fn from(enrollment: ExperimentEnrollment) -> Self {
        let mut branch_value: Option<String> = None;
        let mut reason_value: Option<String> = None;
        let mut error_value: Option<String> = None;
        match &enrollment.status {
            EnrollmentStatus::Enrolled { reason, branch, .. } => {
                branch_value = Some(branch.to_owned());
                reason_value = Some(reason.to_string());
            }
            EnrollmentStatus::Disqualified { reason, branch, .. } => {
                branch_value = Some(branch.to_owned());
                reason_value = Some(reason.to_string());
            }
            EnrollmentStatus::NotEnrolled { reason } => {
                reason_value = Some(reason.to_string());
            }
            EnrollmentStatus::WasEnrolled { branch, .. } => branch_value = Some(branch.to_owned()),
            EnrollmentStatus::Error { reason } => {
                error_value = Some(reason.to_owned());
            }
        }
        EnrollmentStatusExtraDef {
            branch: branch_value,
            conflict_slug: None,
            error_string: error_value,
            reason: reason_value,
            slug: Some(enrollment.slug),
            status: Some(enrollment.status.name()),
            #[cfg(not(feature = "stateful"))]
            user_id: None,
        }
    }
}

#[derive(Clone)]
pub struct FeatureExposureExtraDef {
    pub branch: Option<String>,
    pub slug: String,
    pub feature_id: String,
}

impl From<EnrolledFeature> for FeatureExposureExtraDef {
    fn from(value: EnrolledFeature) -> Self {
        Self {
            feature_id: value.feature_id,
            branch: value.branch,
            slug: value.slug,
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct MalformedFeatureConfigExtraDef {
    pub slug: Option<String>,
    pub branch: Option<String>,
    pub feature_id: String,
    pub part: String,
}

#[cfg(feature = "stateful")]
impl MalformedFeatureConfigExtraDef {
    pub(crate) fn from(value: EnrolledFeature, part: String) -> Self {
        Self {
            slug: Some(value.slug),
            branch: value.branch,
            feature_id: value.feature_id,
            part,
        }
    }

    pub(crate) fn new(feature_id: String, part: String) -> Self {
        Self {
            feature_id,
            part,
            ..Default::default()
        }
    }
}