Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spans): Parse supabase span descriptions #3153

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Extend GPU context with data for Unreal Engine crash reports. ([#3144](https://github.com/getsentry/relay/pull/3144))
- Parametrize transaction in dynamic sampling context. ([#3141](https://github.com/getsentry/relay/pull/3141))
- Parse & scrub span description for supabase. ([#3153](https://github.com/getsentry/relay/pull/3153))

**Bug Fixes**:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use relay_event_schema::protocol::Span;
use url::Url;

use crate::regexes::{
DB_SQL_TRANSACTION_CORE_DATA_REGEX, REDIS_COMMAND_REGEX, RESOURCE_NORMALIZER_REGEX,
DB_SQL_TRANSACTION_CORE_DATA_REGEX, DB_SUPABASE_REGEX, REDIS_COMMAND_REGEX,
RESOURCE_NORMALIZER_REGEX,
};
use crate::span::description::resource::COMMON_PATH_SEGMENTS;
use crate::span::tag_extraction::HTTP_METHOD_EXTRACTOR_REGEX;
Expand Down Expand Up @@ -70,6 +71,11 @@ pub(crate) fn scrub_span_description(
// The description will only contain the entity queried and
// the query type ("User find" for example).
Some(description.to_owned())
} else if span_origin == Some("auto.db.supabase") {
// The description only contains the table name, e.g. `"from(users)`.
// In the future, we might want to parse `data.query` as well.
// See https://github.com/supabase-community/sentry-integration-js/blob/master/index.js#L259
scrub_supabase(description)
} else {
let (scrubbed, mode) = sql::scrub_queries(db_system, description);
if let sql::Mode::Parsed(ast) = mode {
Expand Down Expand Up @@ -141,6 +147,13 @@ fn scrub_core_data(string: &str) -> Option<String> {
}
}

fn scrub_supabase(string: &str) -> Option<String> {
match DB_SUPABASE_REGEX.replace_all(string, "{%s}") {
Cow::Owned(scrubbed) => Some(scrubbed),
Cow::Borrowed(_) => None,
}
}

fn scrub_http(string: &str) -> Option<String> {
let (method, url) = string.split_once(' ')?;
if !HTTP_METHOD_EXTRACTOR_REGEX.is_match(method) {
Expand Down
46 changes: 46 additions & 0 deletions relay-event-normalization/src/normalize/span/tag_extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ pub fn extract_tags(
} else {
None
}
} else if span.origin.as_str() == Some("auto.db.supabase") {
scrubbed_description.as_deref().map(|s| {
s.trim_start_matches("from(")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we enforce that this description always starts with from(?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this actually surfaced a bug, see follow-up here: #3156

.trim_end_matches(')')
.to_owned()
})
} else if span_op.starts_with("db") {
span.description
.value()
Expand Down Expand Up @@ -1428,4 +1434,44 @@ LIMIT 1
Some(&"Chrome".to_string())
);
}

#[test]
fn supabase() {
let json = r#"{
"description": "from(my_table00)",
"op": "db.select",
"origin": "auto.db.supabase",
"data": {
"query": [
"select(*,other(*))",
"in(something, (value1,value2))"
]
}
}"#;

let span = Annotated::<Span>::from_json(json)
.unwrap()
.into_value()
.unwrap();

let tags = extract_tags(
&span,
&Config {
max_tag_value_size: 200,
},
None,
None,
false,
None,
);

assert_eq!(
tags.get(&SpanTagKey::Description).map(String::as_str),
Some("from(my_table{%s})")
);
assert_eq!(
tags.get(&SpanTagKey::Domain).map(String::as_str),
Some("my_table{%s}")
);
}
}
14 changes: 14 additions & 0 deletions relay-event-normalization/src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,17 @@ pub static RESOURCE_NORMALIZER_REGEX: Lazy<Regex> = Lazy::new(|| {

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

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