relay_filter/
lib.rs

1//! Implements event filtering.
2//!
3//! Events may be filtered base on the following configurable criteria.
4//!
5//! * localhost (filter events originating from the local machine)
6//! * browser extensions (filter events caused by known problematic browser extensions)
7//! * web crawlers (filter events sent by user agents known to be web crawlers)
8//! * legacy browsers (filter events originating from legacy browsers, can be configured)
9#![warn(missing_docs)]
10#![doc(
11    html_logo_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png",
12    html_favicon_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png"
13)]
14
15use std::net::IpAddr;
16
17use relay_protocol::Getter;
18
19pub mod browser_extensions;
20pub mod client_ips;
21pub mod csp;
22pub mod error_messages;
23pub mod generic;
24pub mod legacy_browsers;
25pub mod localhost;
26pub mod transaction_name;
27pub mod web_crawlers;
28
29mod common;
30mod config;
31mod interface;
32mod releases;
33
34#[cfg(test)]
35mod testutils;
36
37pub use crate::common::*;
38pub use crate::config::*;
39pub use crate::csp::matches_any_origin;
40pub use crate::generic::are_generic_filters_supported;
41pub use interface::Filterable;
42
43/// Checks whether an event should be filtered for a particular configuration.
44///
45/// If the event should be filtered, the `Err` returned contains a filter reason.
46/// The reason is the message returned by the first filter that didn't pass.
47pub fn should_filter<F: Filterable + Getter>(
48    item: &F,
49    client_ip: Option<IpAddr>,
50    config: &ProjectFiltersConfig,
51    global_config: Option<&GenericFiltersConfig>,
52) -> Result<(), FilterStatKey> {
53    // In order to maintain backwards compatibility, we still want to run the old matching logic,
54    // but we will try to match generic filters first, since the goal is to eventually fade out
55    // the normal filters except for the ones that have complex conditions.
56    generic::should_filter(item, &config.generic, global_config)?;
57
58    // The order of applying filters should not matter as they are additive. Still, be careful
59    // when making changes to this order.
60    csp::should_filter(item, &config.csp)?;
61    client_ips::should_filter(client_ip, &config.client_ips)?;
62    releases::should_filter(item, &config.releases)?;
63    error_messages::should_filter(item, &config.error_messages)?;
64    localhost::should_filter(item, &config.localhost)?;
65    browser_extensions::should_filter(item, &config.browser_extensions)?;
66    legacy_browsers::should_filter(item, &config.legacy_browsers)?;
67    web_crawlers::should_filter(item, &config.web_crawlers)?;
68    transaction_name::should_filter(item, &config.ignore_transactions)?;
69
70    Ok(())
71}