Skip to content

Commit

Permalink
feat(spans): Capture SQL type casts (#2317)
Browse files Browse the repository at this point in the history
Replace type casts in normalized SQL span descriptions, for example:

```sql
INSERT INTO a (b, c, d) VALUES ('foo'::date, 123::bigint[], %s::bigint[]);

-- becomes

INSERT INTO a (b, c, d) VALUES (%s)
```
  • Loading branch information
jjbayer committed Jul 17, 2023
1 parent b3c5d54 commit 693bcb8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions relay-general/src/store/normalize/span/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ static SQL_NORMALIZER_REGEX: Lazy<Regex> = Lazy::new(|| {
# Capture `SAVEPOINT` savepoints.
((?-x)SAVEPOINT (?P<savepoint>(?:(?:"[^"]+")|(?:'[^']+')|(?:`[^`]+`)|(?:[a-z]\w+)))) |
# Capture single-quoted strings, including the remaining substring if `\'` is found.
((?-x)(?P<single_quoted_strs>'(?:\\'|[^'])*(?:'|$))) |
((?-x)(?P<single_quoted_strs>'(?:\\'|[^'])*(?:'|$)(::\w+(\[\]?)?)?)) |
# Capture placeholders.
((?-x)(?P<placeholder>(?:\?+|\$\d+))) |
( (?P<placeholder> (?:\?+|\$\d+|%s) (::\w+(\[\]?)?)? ) ) |
# Capture numbers.
((?-x)(?P<number>(-?\b(?:[0-9]+\.)?[0-9]+(?:[eE][+-]?[0-9]+)?\b))) |
((?-x)(?P<number>(-?\b(?:[0-9]+\.)?[0-9]+(?:[eE][+-]?[0-9]+)?\b)(::\w+(\[\]?)?)?)) |
# Capture booleans (as full tokens, not as substrings of other tokens).
((?-x)(?P<bool>(\b(?:true|false)\b)))
"#,
Expand Down Expand Up @@ -590,6 +590,13 @@ mod tests {
"INSERT INTO a (b, c, d, e) VALuES (%s) ON CONFLICT DO NOTHING"
);

span_description_test!(
span_description_scrub_type_casts,
"INSERT INTO a (b, c, d) VALUES ('foo'::date, 123::bigint[], %s::bigint[])",
"db.sql.query",
"INSERT INTO a (b, c, d) VALUES (%s)"
);

span_description_test!(
span_description_scrub_clickhouse,
"SELECT (toStartOfHour(finish_ts, 'Universal') AS _snuba_time), (uniqIf((nullIf(user, '') AS _snuba_user), greater(multiIf(equals(tupleElement(('duration', 300), 1), 'lcp'), (if(has(measurements.key, 'lcp'), arrayElement(measurements.value, indexOf(measurements.key, 'lcp')), NULL) AS `_snuba_measurements[lcp]`), (duration AS _snuba_duration)), multiply(tupleElement(('duration', 300), 2), 4))) AS _snuba_count_miserable_user), (ifNull(divide(plus(_snuba_count_miserable_user, 4.56), plus(nullIf(uniqIf(_snuba_user, greater(multiIf(equals(tupleElement(('duration', 300), 1), 'lcp'), `_snuba_measurements[lcp]`, _snuba_duration), 0)), 0), 113.45)), 0) AS _snuba_user_misery), _snuba_count_miserable_user, (divide(countIf(notEquals(transaction_status, 0) AND notEquals(transaction_status, 1) AND notEquals(transaction_status, 2)), count()) AS _snuba_failure_rate), (divide(count(), divide(3600.0, 60)) AS _snuba_tpm_3600) FROM transactions_dist WHERE equals(('transaction' AS _snuba_type), 'transaction') AND greaterOrEquals((finish_ts AS _snuba_finish_ts), toDateTime('2023-06-13T09:08:51', 'Universal')) AND less(_snuba_finish_ts, toDateTime('2023-07-11T09:08:51', 'Universal')) AND in((project_id AS _snuba_project_id), [123, 456, 789]) AND equals((environment AS _snuba_environment), 'production') GROUP BY _snuba_time ORDER BY _snuba_time ASC LIMIT 10000 OFFSET 0",
Expand Down

0 comments on commit 693bcb8

Please sign in to comment.