|
1 |
| -use std::borrow::Cow; |
| 1 | +use std::collections::BTreeSet; |
2 | 2 |
|
3 | 3 | use serde::{Deserialize, Serialize};
|
4 | 4 |
|
5 | 5 | /// Features exposed by project config.
|
6 |
| -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] |
| 6 | +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
7 | 7 | pub enum Feature {
|
8 | 8 | /// Enables ingestion of Session Replays (Replay Recordings and Replay Events).
|
| 9 | + #[serde(rename = "organizations:session-replay")] |
9 | 10 | SessionReplay,
|
10 | 11 | /// Enables data scrubbing of replay recording payloads.
|
| 12 | + #[serde(rename = "organizations:session-replay-recording-scrubbing")] |
11 | 13 | SessionReplayRecordingScrubbing,
|
12 | 14 | /// Enables device.class synthesis
|
13 | 15 | ///
|
14 | 16 | /// Enables device.class tag synthesis on mobile events.
|
| 17 | + #[serde(rename = "organizations:device-class-synthesis")] |
15 | 18 | DeviceClassSynthesis,
|
16 | 19 | /// Enables metric extraction from spans.
|
| 20 | + #[serde(rename = "projects:span-metrics-extraction")] |
17 | 21 | SpanMetricsExtraction,
|
18 | 22 | /// Apply transaction normalization rules to transactions from legacy SDKs.
|
| 23 | + #[serde(rename = "organizations:transaction-name-normalize-legacy")] |
19 | 24 | NormalizeLegacyTransactions,
|
| 25 | + |
| 26 | + /// Deprecated, still forwarded for older downstream Relays. |
| 27 | + #[serde(rename = "organizations:transaction-name-mark-scrubbed-as-sanitized")] |
| 28 | + Deprecated1, |
| 29 | + /// Deprecated, still forwarded for older downstream Relays. |
| 30 | + #[serde(rename = "organizations:transaction-name-normalize")] |
| 31 | + Deprecated2, |
| 32 | + /// Deprecated, still forwarded for older downstream Relays. |
| 33 | + #[serde(rename = "organizations:profiling")] |
| 34 | + Deprecated3, |
| 35 | + |
20 | 36 | /// Forward compatibility.
|
21 |
| - Unknown(String), |
| 37 | + #[serde(other)] |
| 38 | + Unknown, |
| 39 | +} |
| 40 | + |
| 41 | +/// A set of [`Feature`]s. |
| 42 | +#[derive(Clone, Debug, Default, Serialize, PartialEq, Eq)] |
| 43 | +pub struct FeatureSet(pub BTreeSet<Feature>); |
| 44 | + |
| 45 | +impl FeatureSet { |
| 46 | + /// Returns `true` if the set of features is empty. |
| 47 | + pub fn is_empty(&self) -> bool { |
| 48 | + self.0.is_empty() |
| 49 | + } |
22 | 50 | }
|
23 | 51 |
|
24 |
| -impl<'de> Deserialize<'de> for Feature { |
| 52 | +impl<'de> Deserialize<'de> for FeatureSet { |
25 | 53 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
26 | 54 | where
|
27 | 55 | D: serde::Deserializer<'de>,
|
28 | 56 | {
|
29 |
| - let feature_name = Cow::<str>::deserialize(deserializer)?; |
30 |
| - Ok(match feature_name.as_ref() { |
31 |
| - "organizations:session-replay" => Feature::SessionReplay, |
32 |
| - "organizations:session-replay-recording-scrubbing" => { |
33 |
| - Feature::SessionReplayRecordingScrubbing |
34 |
| - } |
35 |
| - "organizations:device-class-synthesis" => Feature::DeviceClassSynthesis, |
36 |
| - "projects:span-metrics-extraction" => Feature::SpanMetricsExtraction, |
37 |
| - _ => Feature::Unknown(feature_name.to_string()), |
38 |
| - }) |
| 57 | + let mut set = BTreeSet::<Feature>::deserialize(deserializer)?; |
| 58 | + set.remove(&Feature::Unknown); |
| 59 | + Ok(Self(set)) |
39 | 60 | }
|
40 | 61 | }
|
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use super::*; |
41 | 65 |
|
42 |
| -impl Serialize for Feature { |
43 |
| - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
44 |
| - where |
45 |
| - S: serde::Serializer, |
46 |
| - { |
47 |
| - serializer.serialize_str(match self { |
48 |
| - Feature::SessionReplay => "organizations:session-replay", |
49 |
| - Feature::SessionReplayRecordingScrubbing => { |
50 |
| - "organizations:session-replay-recording-scrubbing" |
51 |
| - } |
52 |
| - Feature::DeviceClassSynthesis => "organizations:device-class-synthesis", |
53 |
| - Feature::SpanMetricsExtraction => "projects:span-metrics-extraction", |
54 |
| - Feature::NormalizeLegacyTransactions => { |
55 |
| - "organizations:transaction-name-normalize-legacy" |
56 |
| - } |
57 |
| - Feature::Unknown(s) => s, |
58 |
| - }) |
| 66 | + #[test] |
| 67 | + fn roundtrip() { |
| 68 | + let features: FeatureSet = |
| 69 | + serde_json::from_str(r#"["organizations:session-replay", "foo"]"#).unwrap(); |
| 70 | + assert_eq!( |
| 71 | + &features, |
| 72 | + &FeatureSet(BTreeSet::from([Feature::SessionReplay])) |
| 73 | + ); |
| 74 | + assert_eq!( |
| 75 | + serde_json::to_string(&features).unwrap(), |
| 76 | + r#"["organizations:session-replay"]"# |
| 77 | + ); |
59 | 78 | }
|
60 | 79 | }
|
0 commit comments