Skip to content

Commit

Permalink
feat(spans): Add SAVEPOINT and SET sql db actions (#2385)
Browse files Browse the repository at this point in the history
Add support for parsing `SET`, `SAVEPOINT`, `RELEASE SAVEPOINT`, and
`ROLLBACK TO SAVEPOINT` db sql actions.

Adds table test to validate this.

Co-authored-by: Iker Barriocanal <32816711+iker-barriocanal@users.noreply.github.com>
  • Loading branch information
AbhiPrasad and iker-barriocanal committed Aug 8, 2023
1 parent 55327b4 commit 95cb257
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
37 changes: 34 additions & 3 deletions relay-general/src/store/normalize/span/tag_extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,10 @@ fn truncate_string(mut string: String, max_bytes: usize) -> String {

/// Regex with a capture group to extract the database action from a query.
///
/// Currently, we're only interested in `SELECT`, `INSERT`, `DELETE` and `UPDATE` statements.
static SQL_ACTION_EXTRACTOR_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(?i)(?P<action>(SELECT|INSERT|DELETE|UPDATE))"#).unwrap());
/// Currently we have an explicit allow-list of database actions considered important.
static SQL_ACTION_EXTRACTOR_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"(?i)(?P<action>(SELECT|INSERT|DELETE|UPDATE|SET|SAVEPOINT|RELEASE SAVEPOINT|ROLLBACK TO SAVEPOINT))"#).unwrap()
});

fn sql_action_from_query(query: &str) -> Option<&str> {
extract_captured_substring(query, &SQL_ACTION_EXTRACTOR_REGEX)
Expand Down Expand Up @@ -560,4 +561,34 @@ mod tests {
let query = r#"UPDATE "a" SET "x" = %s, "y" = %s WHERE "z" = %s"#;
assert_eq!(sql_table_from_query(query).unwrap(), "a");
}

#[test]
fn extract_sql_action() {
let test_cases = vec![
(
r#"SELECT "sentry_organization"."id" FROM "sentry_organization" WHERE "sentry_organization"."id" = %s"#,
"SELECT",
),
(
r#"INSERT INTO "sentry_groupseen" ("project_id", "group_id", "user_id", "last_seen") VALUES (%s, %s, %s, %s) RETURNING "sentry_groupseen"."id"#,
"INSERT",
),
(
r#"UPDATE sentry_release SET date_released = %s WHERE id = %s"#,
"UPDATE",
),
(
r#"DELETE FROM "sentry_groupinbox" WHERE "sentry_groupinbox"."id" IN (%s)"#,
"DELETE",
),
(r#"SET search_path TO my_schema, public"#, "SET"),
(r#"SAVEPOINT %s"#, "SAVEPOINT"),
(r#"RELEASE SAVEPOINT %s"#, "RELEASE SAVEPOINT"),
(r#"ROLLBACK TO SAVEPOINT %s"#, "ROLLBACK TO SAVEPOINT"),
];

for (query, expected) in test_cases {
assert_eq!(sql_action_from_query(query).unwrap(), expected)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ expression: metrics
tags: {
"environment": "fake_environment",
"http.status_code": "500",
"span.action": "SAVEPOINT",
"span.category": "db",
"span.description": "SAVEPOINT %s",
"span.group": "3f955cbde39e04b9",
Expand All @@ -793,6 +794,7 @@ expression: metrics
tags: {
"environment": "fake_environment",
"http.status_code": "500",
"span.action": "SAVEPOINT",
"span.category": "db",
"span.description": "SAVEPOINT %s",
"span.group": "3f955cbde39e04b9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,9 @@ expression: event.value().unwrap().spans
"release": String(
"1.2.3",
),
"span.action": String(
"SAVEPOINT",
),
"span.category": String(
"db",
),
Expand Down

0 comments on commit 95cb257

Please sign in to comment.