Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iambriccardo committed Aug 13, 2024
2 parents 05deaee + 6d01e72 commit 9c4ad3b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 74 deletions.
69 changes: 35 additions & 34 deletions relay-event-normalization/src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,52 @@ pub static TRANSACTION_NAME_NORMALIZER_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
(?P<uuid>[^/\\]*
\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b
(?-u:\b)[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(?-u:\b)
[^/\\]*) |
(?P<sha1>[^/\\]*
\b[0-9a-fA-F]{40}\b
(?-u:\b)[0-9a-fA-F]{40}(?-u:\b)
[^/\\]*) |
(?P<md5>[^/\\]*
\b[0-9a-fA-F]{32}\b
(?-u:\b)[0-9a-fA-F]{32}(?-u:\b)
[^/\\]*) |
(?P<date>[^/\\]*
(?:
(?:\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|
(?:\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|
(?:\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))
(?:[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]+([+-][0-2][0-9]:[0-5][0-9]|Z))|
(?:[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]([+-][0-2][0-9]:[0-5][0-9]|Z))|
(?:[0-9]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\b
(?-u:\b)0[xX][0-9a-fA-F]+(?-u:\b)
[^/\\]*) |
(?:^|[/\\])
(?P<int>
(:?[^%/\\]|%[0-9a-fA-F]{2})*\d{2,}
(:?[^%/\\]|%[0-9a-fA-F]{2})*[0-9]{2,}
[^/\\]*)",
)
.unwrap()
});

/// Captures initial all-caps words as redis command, the rest as arguments.
pub static REDIS_COMMAND_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\s*(?P<command>[A-Z]+(\s+[A-Z]+)*\b)(?P<args>.+)?").unwrap());
pub static REDIS_COMMAND_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?-u:\s)*(?P<command>[A-Z]+((?-u:\s)+[A-Z]+)*(?-u:\b))(?P<args>.+)?").unwrap()
});

/// Regex with multiple capture groups for resource tokens we should scrub.
///
Expand All @@ -65,44 +66,44 @@ pub static REDIS_COMMAND_REGEX: Lazy<Regex> =
/// <https://github.com/getsentry/sentry/blob/de5949a9a313d7ef0bf0685f84fe6e981ac38558/src/sentry/utils/performance_issues/base.py#L292-L306>
pub static RESOURCE_NORMALIZER_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?xi)
r"(?x)
# UUIDs.
(?P<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}) |
(?P<uuid>[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}) |
# Version strings.
(?P<version>(v[0-9]+(?:\.[0-9]+)*)) |
# Hexadecimal strings with more than 5 digits.
(?P<hex>[a-f0-9]{5}[a-f0-9]+) |
(?P<hex>[a-fA-F0-9]{5}[a-fA-F0-9]+) |
# Integer IDs with more than one digit.
(?P<int>\d\d+)
(?P<int>[0-9][0-9]+)
",
)
.unwrap()
});

pub static DB_SQL_TRANSACTION_CORE_DATA_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?P<int>\d+)").unwrap());
Lazy::new(|| Regex::new(r"(?P<int>[0-9]+)").unwrap());

pub static DB_SUPABASE_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?xi)
r"(?x)
# UUIDs.
(?P<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}) |
(?P<uuid>[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}) |
# Hexadecimal strings with more than 5 digits.
(?P<hex>[a-f0-9]{5}[a-f0-9]+) |
(?P<hex>[a-fA-F0-9]{5}[a-fA-F0-9]+) |
# Integer IDs with more than one digit.
(?P<int>\d\d+)
(?P<int>[0-9][0-9]+)
",
)
.unwrap()
});

pub static FUNCTION_NORMALIZER_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?xi)
r"(?x)
# UUIDs.
(?P<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}) |
(?P<uuid>[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}) |
# Hexadecimal strings with more than 5 digits.
(?P<hex>[a-f0-9]{5}[a-f0-9]+)
(?P<hex>[a-fA-F0-9]{5}[a-fA-F0-9]+)
",
)
.unwrap()
Expand Down
103 changes: 63 additions & 40 deletions relay-server/src/services/buffer/envelope_buffer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use relay_base_schema::project::ProjectKey;
use relay_config::Config;
use stack_key::StackKey;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::convert::Infallible;
use std::time::Instant;

use relay_base_schema::project::ProjectKey;
use relay_config::Config;

use crate::envelope::Envelope;
use crate::services::buffer::envelope_stack::sqlite::SqliteEnvelopeStackError;
use crate::services::buffer::envelope_stack::{EnvelopeStack, StackProvider};
Expand Down Expand Up @@ -229,10 +229,13 @@ where
self.priority_queue.change_priority_by(stack_key, |stack| {
let mut found = false;
for (subkey, readiness) in [
(stack_key.lesser(), &mut stack.readiness.0),
(stack_key.greater(), &mut stack.readiness.1),
(stack_key.own_key, &mut stack.readiness.own_project_ready),
(
stack_key.sampling_key,
&mut stack.readiness.sampling_project_ready,
),
] {
if subkey == project {
if subkey == *project {
found = true;
if *readiness != is_ready {
changed = true;
Expand Down Expand Up @@ -277,46 +280,38 @@ where
.remove(&stack_key);
}
self.priority_queue.remove(&stack_key);

relay_statsd::metric!(
gauge(RelayGauges::BufferStackCount) = self.priority_queue.len() as u64
);
}
}

mod stack_key {
use super::*;
/// Sorted stack key.
///
/// Contains a pair of project keys. The lower key is always the first
/// element in the pair, such that `(k1, k2)` and `(k2, k1)` map to the same
/// stack key.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct StackKey(ProjectKey, ProjectKey);

impl StackKey {
pub fn from_envelope(envelope: &Envelope) -> Self {
let own_key = envelope.meta().public_key();
let sampling_key = envelope.sampling_key().unwrap_or(own_key);
Self::new(own_key, sampling_key)
}

pub fn lesser(&self) -> &ProjectKey {
&self.0
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct StackKey {
own_key: ProjectKey,
sampling_key: ProjectKey,
}

pub fn greater(&self) -> &ProjectKey {
&self.1
}
impl StackKey {
pub fn from_envelope(envelope: &Envelope) -> Self {
let own_key = envelope.meta().public_key();
let sampling_key = envelope.sampling_key().unwrap_or(own_key);
Self::new(own_key, sampling_key)
}

pub fn iter(&self) -> impl Iterator<Item = ProjectKey> {
std::iter::once(self.0).chain((self.0 != self.1).then_some(self.1))
}
pub fn iter(&self) -> impl Iterator<Item = ProjectKey> {
let Self {
own_key,
sampling_key,
} = self;
std::iter::once(*own_key).chain((own_key != sampling_key).then_some(*sampling_key))
}

fn new(mut key1: ProjectKey, mut key2: ProjectKey) -> Self {
if key2 < key1 {
std::mem::swap(&mut key1, &mut key2);
}
Self(key1, key2)
fn new(own_key: ProjectKey, sampling_key: ProjectKey) -> Self {
Self {
own_key,
sampling_key,
}
}
}
Expand Down Expand Up @@ -404,15 +399,21 @@ impl Ord for Priority {
}

#[derive(Debug)]
struct Readiness(bool, bool);
struct Readiness {
own_project_ready: bool,
sampling_project_ready: bool,
}

impl Readiness {
fn new() -> Self {
Self(false, false)
Self {
own_project_ready: false,
sampling_project_ready: false,
}
}

fn ready(&self) -> bool {
self.0 && self.1
self.own_project_ready && self.sampling_project_ready
}
}

Expand Down Expand Up @@ -641,6 +642,28 @@ mod tests {
assert!(buffer.pop().await.unwrap().is_none());
}

#[tokio::test]
async fn test_project_keys_distinct() {
let project_key1 = ProjectKey::parse("a94ae32be2584e0bbd7a4cbb95971fed").unwrap();
let project_key2 = ProjectKey::parse("a94ae32be2584e0bbd7a4cbb95971fef").unwrap();

let stack_key1 = StackKey::new(project_key1, project_key2);
let stack_key2 = StackKey::new(project_key2, project_key1);

assert_ne!(stack_key1, stack_key2);

let mut buffer = EnvelopeBuffer::<MemoryStackProvider>::new();
buffer
.push(new_envelope(project_key1, Some(project_key2), None))
.await
.unwrap();
buffer
.push(new_envelope(project_key2, Some(project_key1), None))
.await
.unwrap();
assert_eq!(buffer.priority_queue.len(), 2);
}

#[tokio::test]
async fn test_last_peek_internal_order() {
let mut buffer = EnvelopeBuffer::<MemoryStackProvider>::new();
Expand Down

0 comments on commit 9c4ad3b

Please sign in to comment.