Skip to main content

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;
33mod statsd;
34
35#[cfg(test)]
36mod testutils;
37
38pub use crate::common::*;
39pub use crate::config::*;
40pub use crate::csp::matches_any_origin;
41pub use crate::generic::are_generic_filters_supported;
42pub use interface::{Filterable, UserAgent};
43
44/// Checks whether an event should be filtered for a particular configuration.
45///
46/// If the event should be filtered, the `Err` returned contains a filter reason.
47/// The reason is the message returned by the first filter that didn't pass.
48///
49/// The `client_ip` parameter is the "client IP" extracted from the envelope. It's
50/// used for client IP filtering and should not be confused with a "user IP" that may
51/// be contained in the item, which is used for localhost filtering.
52pub fn should_filter<F: Filterable + Getter>(
53    item: &F,
54    client_ip: Option<IpAddr>,
55    config: &ProjectFiltersConfig,
56    global_config: Option<&GenericFiltersConfig>,
57) -> Result<(), FilterStatKey> {
58    relay_statsd::metric!(
59        timer(statsd::FilterTimers::ShouldFilter),
60        item = std::any::type_name::<F>(),
61        {
62            // In order to maintain backwards compatibility, we still want to run the old matching logic,
63            // but we will try to match generic filters first, since the goal is to eventually fade out
64            // the normal filters except for the ones that have complex conditions.
65            generic::should_filter(item, &config.generic, global_config)?;
66
67            // The order of applying filters should not matter as they are additive. Still, be careful
68            // when making changes to this order.
69            csp::should_filter(item, &config.csp)?;
70            client_ips::should_filter(client_ip, &config.client_ips)?;
71            releases::should_filter(item, &config.releases)?;
72            error_messages::should_filter(item, &config.error_messages)?;
73            localhost::should_filter(item, &config.localhost)?;
74            browser_extensions::should_filter(item, &config.browser_extensions)?;
75            legacy_browsers::should_filter(item, &config.legacy_browsers)?;
76            web_crawlers::should_filter(item, &config.web_crawlers)?;
77            transaction_name::should_filter(item, &config.ignore_transactions)?;
78        }
79    );
80
81    Ok(())
82}