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