relay_event_schema/protocol/
transaction.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::fmt;
use std::str::FromStr;

use relay_protocol::{Annotated, Empty, ErrorKind, FromValue, IntoValue, SkipSerialization, Value};
use serde::{Deserialize, Serialize};

use crate::processor::ProcessValue;
use crate::protocol::Timestamp;

/// Describes how the name of the transaction was determined.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TransactionSource {
    /// User-defined name set through `set_transaction_name`.
    Custom,
    /// Raw URL, potentially containing identifiers.
    Url,
    /// Parametrized URL or route.
    Route,
    /// Name of the view handling the request.
    View,
    /// Named after a software component, such as a function or class name.
    Component,
    /// The transaction name was updated to reduce the name cardinality.
    Sanitized,
    /// Name of a background task (e.g. a Celery task).
    Task,
    /// This is the default value set by Relay for legacy SDKs.
    Unknown,
    /// Any other unknown source that is not explicitly defined above.
    Other(String),
}

impl TransactionSource {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Custom => "custom",
            Self::Url => "url",
            Self::Route => "route",
            Self::View => "view",
            Self::Component => "component",
            Self::Sanitized => "sanitized",
            Self::Task => "task",
            Self::Unknown => "unknown",
            Self::Other(ref s) => s,
        }
    }
}

impl FromStr for TransactionSource {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "custom" => Ok(Self::Custom),
            "url" => Ok(Self::Url),
            "route" => Ok(Self::Route),
            "view" => Ok(Self::View),
            "component" => Ok(Self::Component),
            "sanitized" => Ok(Self::Sanitized),
            "task" => Ok(Self::Task),
            "unknown" => Ok(Self::Unknown),
            s => Ok(Self::Other(s.to_owned())),
        }
    }
}

impl fmt::Display for TransactionSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl Default for TransactionSource {
    fn default() -> Self {
        Self::Unknown
    }
}

impl Empty for TransactionSource {
    #[inline]
    fn is_empty(&self) -> bool {
        matches!(self, Self::Unknown)
    }
}

impl FromValue for TransactionSource {
    fn from_value(value: Annotated<Value>) -> Annotated<Self> {
        match String::from_value(value) {
            Annotated(Some(value), mut meta) => match value.parse() {
                Ok(source) => Annotated(Some(source), meta),
                Err(_) => {
                    meta.add_error(ErrorKind::InvalidData);
                    meta.set_original_value(Some(value));
                    Annotated(None, meta)
                }
            },
            Annotated(None, meta) => Annotated(None, meta),
        }
    }
}

impl IntoValue for TransactionSource {
    fn into_value(self) -> Value
    where
        Self: Sized,
    {
        Value::String(match self {
            Self::Other(s) => s,
            _ => self.as_str().to_owned(),
        })
    }

    fn serialize_payload<S>(&self, s: S, _behavior: SkipSerialization) -> Result<S::Ok, S::Error>
    where
        Self: Sized,
        S: serde::Serializer,
    {
        serde::Serialize::serialize(self.as_str(), s)
    }
}

impl ProcessValue for TransactionSource {}

#[derive(Clone, Debug, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
pub struct TransactionNameChange {
    /// Describes how the previous transaction name was determined.
    pub source: Annotated<TransactionSource>,

    /// The number of propagations from the start of the transaction to this change.
    pub propagations: Annotated<u64>,

    /// Timestamp when the transaction name was changed.
    ///
    /// This adheres to the event timestamp specification.
    pub timestamp: Annotated<Timestamp>,
}

/// Additional information about the name of the transaction.
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
pub struct TransactionInfo {
    /// Describes how the name of the transaction was determined.
    ///
    /// This will be used by the server to decide whether or not to scrub identifiers from the
    /// transaction name, or replace the entire name with a placeholder.
    pub source: Annotated<TransactionSource>,

    /// The unmodified transaction name as obtained by the source.
    ///
    /// This value will only be set if the transaction name was modified during event processing.
    #[metastructure(max_chars = 200, trim_whitespace = true)]
    pub original: Annotated<String>,

    /// A list of changes prior to the final transaction name.
    ///
    /// This list must be empty if the transaction name is set at the beginning of the transaction
    /// and never changed. There is no placeholder entry for the initial transaction name.
    pub changes: Annotated<Vec<Annotated<TransactionNameChange>>>,

    /// The total number of propagations during the transaction.
    pub propagations: Annotated<u64>,
}

#[cfg(test)]
mod tests {
    use chrono::{TimeZone, Utc};
    use similar_asserts::assert_eq;

    use super::*;

    #[test]
    fn test_other_source_roundtrip() {
        let json = r#""something-new""#;
        let source = Annotated::new(TransactionSource::Other("something-new".to_owned()));

        assert_eq!(source, Annotated::from_json(json).unwrap());
        assert_eq!(json, source.payload_to_json_pretty().unwrap());
    }

    #[test]
    fn test_transaction_info_roundtrip() {
        let json = r#"{
  "source": "route",
  "original": "/auth/login/john123/",
  "changes": [
    {
      "source": "url",
      "propagations": 1,
      "timestamp": 946684800.0
    }
  ],
  "propagations": 2
}"#;

        let info = Annotated::new(TransactionInfo {
            source: Annotated::new(TransactionSource::Route),
            original: Annotated::new("/auth/login/john123/".to_owned()),
            changes: Annotated::new(vec![Annotated::new(TransactionNameChange {
                source: Annotated::new(TransactionSource::Url),
                propagations: Annotated::new(1),
                timestamp: Annotated::new(
                    Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().into(),
                ),
            })]),
            propagations: Annotated::new(2),
        });

        assert_eq!(info, Annotated::from_json(json).unwrap());
        assert_eq!(json, info.to_json_pretty().unwrap());
    }
}