Skip to main content

relay_filter/
transaction_name.rs

1//! Implements event filtering based on whether the endpoint called is a healthcheck endpoint.
2//!
3//! If this filter is enabled transactions from healthcheck endpoints will be filtered out.
4
5use relay_pattern::{Gas, Patterns};
6
7use crate::{FilterStatKey, Filterable, IgnoreTransactionsFilterConfig};
8
9fn matches(transaction: Option<&str>, patterns: &Patterns) -> bool {
10    transaction.is_some_and(|transaction| {
11        patterns
12            .is_match(transaction, Gas::CONSTRAINED)
13            .unwrap_or(false)
14    })
15}
16
17/// Filters [Transaction](relay_event_schema::protocol::EventType::Transaction) events based on a list of provided transaction
18/// name globs.
19pub fn should_filter<F: Filterable>(
20    item: &F,
21    config: &IgnoreTransactionsFilterConfig,
22) -> Result<(), FilterStatKey> {
23    if config.is_empty() {
24        return Ok(());
25    }
26
27    if matches(item.transaction(), &config.patterns) {
28        return Err(FilterStatKey::FilteredTransactions);
29    }
30    Ok(())
31}
32
33#[cfg(test)]
34mod tests {
35    use relay_event_schema::protocol::{Event, EventType};
36    use relay_pattern::TypedPatterns;
37    use relay_protocol::Annotated;
38
39    use super::*;
40
41    fn _get_config() -> IgnoreTransactionsFilterConfig {
42        let patterns_raw = [
43            "*healthcheck*",
44            "*healthy*",
45            "live",
46            "live[z/-]*",
47            "*[/-]live",
48            "*[/-]live[z/-]*",
49            "ready",
50            "ready[z/-]*",
51            "*[/-]ready",
52            "*[/-]ready[z/-]*",
53            "*heartbeat*",
54            "*/health{/,}",
55            "*/healthz",
56            "*/ping",
57            "*/up",
58            "*/actuator/health{/*,}",
59            "*/manage/health{/*,}",
60        ]
61        .map(|val| val.to_owned())
62        .to_vec();
63
64        IgnoreTransactionsFilterConfig {
65            patterns: TypedPatterns::from(patterns_raw),
66            is_enabled: true,
67        }
68    }
69
70    /// tests matching for various transactions
71    #[test]
72    fn test_matches() {
73        let config = _get_config();
74
75        let transaction_names = [
76            "a/b/healthcheck/c",
77            "a_HEALTHCHECK_b",
78            "healthcheck",
79            "/health",
80            "a/HEALTH",
81            "/health",
82            "a/HEALTH",
83            "live",
84            "livez",
85            "live/123",
86            "live-33",
87            "x-live",
88            "abc/live",
89            "abc/live-23",
90            "ready",
91            "123/ready",
92            "readyz",
93            "ready/123",
94            "abc/ready/1234",
95            "abcheartbeat123",
96            "123/health",
97            "123/healthz",
98            "123/ping",
99            "/up",
100            "123/up",
101            "/actuator/health",
102            "/actuator/health/",
103            "foo/actuator/health/db",
104            "/manage/health",
105            "/manage/health/",
106            "foo/manage/health/db",
107        ];
108
109        for name in transaction_names {
110            let event = Event {
111                transaction: Annotated::new(name.into()),
112                ty: Annotated::new(EventType::Transaction),
113                ..Event::default()
114            };
115            assert!(
116                should_filter(&event, &config).is_err(),
117                "Did not match `{name}`"
118            )
119        }
120    }
121    /// tests non matching transactions transactions
122    #[test]
123    fn test_does_not_match() {
124        let transaction_names = [
125            "bad",
126            "/bad",
127            "a/b/c",
128            "health",
129            "healthz",
130            "/healthz/",
131            "/healthx",
132            "/healthzx",
133            "service-delivery",
134            "delivery",
135            "notready",
136            "already",
137            "/upload",
138            "foo/health/bar",
139        ];
140        let config = _get_config();
141
142        for name in transaction_names {
143            let event = Event {
144                transaction: Annotated::new(name.into()),
145                ty: Annotated::new(EventType::Transaction),
146                ..Event::default()
147            };
148            assert!(
149                should_filter(&event, &config).is_ok(),
150                "Did match `{name}` but it shouldn't have."
151            )
152        }
153    }
154
155    // test it doesn't match when the transaction name is missing
156    #[test]
157    fn test_does_not_match_missing_transaction() {
158        let event = Event {
159            ty: Annotated::new(EventType::Transaction),
160            ..Event::default()
161        };
162        let config = _get_config();
163        assert!(
164            should_filter(&event, &config).is_ok(),
165            "Did match with empty transaction but it shouldn't have."
166        )
167    }
168
169    #[test]
170    fn test_filters_when_matching() {
171        let event = Event {
172            transaction: Annotated::new("/health".into()),
173            ty: Annotated::new(EventType::Transaction),
174            ..Event::default()
175        };
176        let config = _get_config();
177
178        let filter_result = should_filter(&event, &config);
179        assert_eq!(
180            filter_result,
181            Err(FilterStatKey::FilteredTransactions),
182            "Event was not filtered "
183        )
184    }
185
186    #[test]
187    fn test_does_not_filter_when_disabled() {
188        let event = Event {
189            transaction: Annotated::new("/health".into()),
190            ty: Annotated::new(EventType::Transaction),
191            ..Event::default()
192        };
193        let filter_result = should_filter(
194            &event,
195            &IgnoreTransactionsFilterConfig {
196                patterns: TypedPatterns::default(),
197                is_enabled: true,
198            },
199        );
200        assert_eq!(
201            filter_result,
202            Ok(()),
203            "Event filtered although filter should have been disabled"
204        )
205    }
206    #[test]
207    // Tests that is_enabled flag disables the transaction name filter
208    fn test_does_not_filter_when_disabled_with_flag() {
209        let event = Event {
210            transaction: Annotated::new("/health".into()),
211            ty: Annotated::new(EventType::Transaction),
212            ..Event::default()
213        };
214        let mut config = _get_config();
215        config.is_enabled = false;
216        let filter_result = should_filter(&event, &config);
217        assert_eq!(
218            filter_result,
219            Ok(()),
220            "Event filtered although filter should have been disabled"
221        )
222    }
223
224    #[test]
225    fn test_does_not_filter_when_not_matching() {
226        let event = Event {
227            transaction: Annotated::new("/a/b/c".into()),
228            ty: Annotated::new(EventType::Transaction),
229            ..Event::default()
230        };
231        let filter_result = should_filter(&event, &_get_config());
232        assert_eq!(
233            filter_result,
234            Ok(()),
235            "Event filtered although filter should have not matched"
236        )
237    }
238
239    #[test]
240    fn test_only_filters_transactions_not_anything_else() {
241        let config = _get_config();
242
243        for event_type in [EventType::Transaction, EventType::Error, EventType::Csp] {
244            let expect_to_filter = event_type == EventType::Transaction;
245            let event = Event {
246                transaction: Annotated::new("/health".into()),
247                ty: Annotated::new(event_type),
248                ..Event::default()
249            };
250            let filter_result = should_filter(&event, &config);
251
252            if expect_to_filter {
253                assert_eq!(
254                    filter_result,
255                    Err(FilterStatKey::FilteredTransactions),
256                    "Event was not filtered "
257                );
258            } else {
259                assert_eq!(
260                    filter_result,
261                    Ok(()),
262                    "Event filtered for event_type={event_type} although filter should have not matched"
263                )
264            }
265        }
266    }
267}