Skip to content

Commit 65560d1

Browse files
committed
feat: add event type and pattern variables to centralize checks
1 parent 0cc8819 commit 65560d1

1 file changed

Lines changed: 64 additions & 41 deletions

File tree

sp_HumanEvents/sp_HumanEvents.sql

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,19 @@ DECLARE
515515
@drop_holder nvarchar(max) = N'',
516516
@cleanup_views nvarchar(max) = N'',
517517
@nc10 nvarchar(2) = NCHAR(10),
518-
@inputbuf_bom nvarchar(1) = CONVERT(nvarchar(1), 0x0a00, 0);
518+
@inputbuf_bom nvarchar(1) = CONVERT(nvarchar(1), 0x0a00, 0),
519+
@wait_event_type_pattern nvarchar(6) = N'%wait%',
520+
@lock_event_type_pattern nvarchar(6) = N'%lock%',
521+
@query_event_type_pattern nvarchar(6) = N'%quer%',
522+
@compile_event_type_pattern nvarchar(6) = N'%comp%',
523+
@recompile_exclude_event_type_pattern nvarchar(4) = N'%re%',
524+
@recompile_event_type_pattern nvarchar(8) = N'%recomp%',
525+
@is_wait_event_type bit = 0,
526+
@is_lock_event_type bit = 0,
527+
@is_query_event_type bit = 0,
528+
@is_compile_event_type bit = 0,
529+
@is_compile_any_event_type bit = 0,
530+
@is_recompile_event_type bit = 0;
519531

520532
/*check to make sure we're on a usable version*/
521533
IF
@@ -854,6 +866,18 @@ What on earth is %s?', 11, 1, @event_type) WITH NOWAIT;
854866
RETURN;
855867
END;
856868

869+
SELECT
870+
@is_lock_event_type = CASE WHEN @event_type LIKE @lock_event_type_pattern THEN 1 ELSE 0 END,
871+
@is_query_event_type = CASE WHEN @event_type LIKE @query_event_type_pattern THEN 1 ELSE 0 END,
872+
@is_recompile_event_type = CASE WHEN @event_type LIKE @recompile_event_type_pattern THEN 1 ELSE 0 END,
873+
@is_compile_any_event_type = CASE WHEN @event_type LIKE @compile_event_type_pattern THEN 1 ELSE 0 END,
874+
@is_wait_event_type = CASE WHEN @event_type LIKE @wait_event_type_pattern THEN 1 ELSE 0 END,
875+
@is_compile_event_type = CASE
876+
WHEN @event_type LIKE @compile_event_type_pattern
877+
AND @event_type NOT LIKE @recompile_exclude_event_type_pattern THEN 1
878+
ELSE 0
879+
END;
880+
857881

858882
IF @debug = 1 BEGIN RAISERROR(N'Checking query sort order', 0, 1) WITH NOWAIT; END;
859883
IF @query_sort_order NOT IN
@@ -984,7 +1008,7 @@ IF
9841008
(
9851009
@wait_type <> N''
9861010
AND @wait_type <> N'ALL'
987-
AND LOWER(@event_type) NOT LIKE N'%wait%'
1011+
AND @is_wait_event_type = 0
9881012
)
9891013
BEGIN
9901014
RAISERROR(N'You can''t filter on wait stats unless you use the wait stats event.', 11, 1) WITH NOWAIT;
@@ -998,7 +1022,7 @@ IF @debug = 1 BEGIN RAISERROR(N'Are we trying to filter for a blocking session?'
9981022
/* blocking events need a database name to resolve objects */
9991023
IF
10001024
(
1001-
LOWER(@event_type) LIKE N'%lock%'
1025+
@is_lock_event_type = 1
10021026
AND DB_ID(@database_name) IS NULL
10031027
AND @object_name <> N''
10041028
)
@@ -1010,7 +1034,7 @@ END;
10101034
/* but could we resolve the object name? */
10111035
IF
10121036
(
1013-
LOWER(@event_type) LIKE N'%lock%'
1037+
@is_lock_event_type = 1
10141038
AND @object_name <> N''
10151039
AND OBJECT_ID(@fully_formed_babby) IS NULL
10161040
)
@@ -1021,7 +1045,7 @@ END;
10211045

10221046
/* no blocked process report, no love */
10231047
IF @debug = 1 BEGIN RAISERROR(N'Validating if the Blocked Process Report is on, if the session is for blocking', 0, 1) WITH NOWAIT; END;
1024-
IF @event_type LIKE N'%lock%'
1048+
IF @is_lock_event_type = 1
10251049
AND EXISTS
10261050
(
10271051
SELECT
@@ -1234,7 +1258,7 @@ END;
12341258
IF @debug = 1 BEGIN RAISERROR(N'Setting up individual filters', 0, 1) WITH NOWAIT; END;
12351259
IF @query_duration_ms > 0
12361260
BEGIN
1237-
IF LOWER(@event_type) NOT LIKE N'%comp%' /* compile and recompile durations are tiny */
1261+
IF @is_compile_any_event_type <> 0 /* compile and recompile durations are tiny */
12381262
BEGIN
12391263
SET @query_duration_filter += N' AND duration >= ' + CONVERT(nvarchar(20), (@query_duration_ms * 1000)) + @nc10;
12401264
END;
@@ -1262,11 +1286,11 @@ END;
12621286

12631287
IF @database_name <> N''
12641288
BEGIN
1265-
IF LOWER(@event_type) NOT LIKE N'%lock%'
1289+
IF @is_lock_event_type = 0
12661290
BEGIN
12671291
SET @database_name_filter += N' AND sqlserver.database_name = N' + QUOTENAME(@database_name, N'''') + @nc10;
12681292
END;
1269-
IF LOWER(@event_type) LIKE N'%lock%'
1293+
IF @is_lock_event_type = 1
12701294
BEGIN
12711295
SET @database_name_filter += N' AND database_name = N' + QUOTENAME(@database_name, N'''') + @nc10;
12721296
END;
@@ -1291,12 +1315,12 @@ END;
12911315

12921316
IF @object_name <> N''
12931317
BEGIN
1294-
IF @event_type LIKE N'%lock%'
1318+
IF @is_lock_event_type = 1
12951319
BEGIN
12961320
SET @object_id = OBJECT_ID(@fully_formed_babby);
12971321
SET @object_name_filter += N' AND object_id = ' + @object_id + @nc10;
12981322
END;
1299-
IF @event_type NOT LIKE N'%lock%'
1323+
IF @is_lock_event_type = 0
13001324
BEGIN
13011325
SET @object_name_filter += N' AND object_name = N' + QUOTENAME(@object_name, N'''') + @nc10;
13021326
END;
@@ -1311,7 +1335,7 @@ END;
13111335

13121336
/* At this point we'll either put my list of interesting waits in a temp table,
13131337
or a list of user defined waits */
1314-
IF LOWER(@event_type) LIKE N'%wait%'
1338+
IF @is_wait_event_type = 1
13151339
BEGIN
13161340
INSERT
13171341
#wait WITH(TABLOCK)
@@ -1571,11 +1595,11 @@ SET @session_filter_parameterization +=
15711595
IF @debug = 1 BEGIN RAISERROR(N'Setting up the event session', 0, 1) WITH NOWAIT; END;
15721596
SET @session_sql +=
15731597
CASE
1574-
WHEN LOWER(@event_type) LIKE N'%lock%'
1598+
WHEN @is_lock_event_type = 1
15751599
THEN N'
15761600
ADD EVENT sqlserver.blocked_process_report
15771601
(WHERE ( ' + @session_filter_blocking + N' ))'
1578-
WHEN LOWER(@event_type) LIKE N'%quer%'
1602+
WHEN @is_query_event_type = 1
15791603
THEN N'
15801604
ADD EVENT sqlserver.module_end
15811605
(SET collect_statement = 1
@@ -1602,21 +1626,21 @@ SET @session_sql +=
16021626
WHERE ( ' + @session_filter_query_plans + N' ))'
16031627
ELSE N''
16041628
END
1605-
WHEN LOWER(@event_type) LIKE N'%wait%'
1629+
WHEN @is_wait_event_type = 1
16061630
AND @v > 11
16071631
THEN N'
16081632
ADD EVENT sqlos.wait_completed
16091633
(SET collect_wait_resource = 1
16101634
ACTION (sqlserver.database_name, sqlserver.plan_handle, sqlserver.query_hash_signed, sqlserver.query_plan_hash_signed)
16111635
WHERE ( ' + @session_filter_waits + N' ))'
1612-
WHEN LOWER(@event_type) LIKE N'%wait%'
1636+
WHEN @is_wait_event_type = 1
16131637
AND @v = 11
16141638
THEN N'
16151639
ADD EVENT sqlos.wait_info
16161640
(
16171641
ACTION (sqlserver.database_name, sqlserver.plan_handle, sqlserver.query_hash_signed, sqlserver.query_plan_hash_signed)
16181642
WHERE ( ' + @session_filter_waits + N' ))'
1619-
WHEN LOWER(@event_type) LIKE N'%recomp%'
1643+
WHEN @is_recompile_event_type = 1
16201644
THEN CASE
16211645
WHEN @compile_events = 1
16221646
THEN N'
@@ -1630,8 +1654,7 @@ SET @session_sql +=
16301654
ACTION(sqlserver.database_name)
16311655
WHERE ( ' + @session_filter_recompile + N' ))'
16321656
END
1633-
WHEN (LOWER(@event_type) LIKE N'%comp%'
1634-
AND LOWER(@event_type) NOT LIKE N'%re%')
1657+
WHEN @is_compile_event_type = 1
16351658
THEN CASE
16361659
WHEN @compile_events = 1
16371660
THEN N'
@@ -1784,7 +1807,7 @@ END;
17841807
/*
17851808
This is where magic will happen
17861809
*/
1787-
IF LOWER(@event_type) LIKE N'%quer%'
1810+
IF @is_query_event_type = 1
17881811
BEGIN
17891812
WITH
17901813
queries AS
@@ -2137,7 +2160,7 @@ BEGIN
21372160
END;
21382161

21392162

2140-
IF LOWER(@event_type) LIKE N'%comp%' AND LOWER(@event_type) NOT LIKE N'%re%'
2163+
IF @is_compile_event_type = 1
21412164
BEGIN
21422165
IF @compile_events = 1
21432166
BEGIN
@@ -2389,7 +2412,7 @@ BEGIN
23892412
END;
23902413
END;
23912414

2392-
IF LOWER(@event_type) LIKE N'%recomp%'
2415+
IF @is_recompile_event_type = 1
23932416
BEGIN
23942417
IF @compile_events = 1
23952418
BEGIN
@@ -2548,7 +2571,7 @@ IF @compile_events = 1
25482571
END;
25492572

25502573

2551-
IF LOWER(@event_type) LIKE N'%wait%'
2574+
IF @is_wait_event_type = 1
25522575
BEGIN
25532576
WITH
25542577
waits AS
@@ -2700,7 +2723,7 @@ BEGIN
27002723
END;
27012724

27022725

2703-
IF LOWER(@event_type) LIKE N'%lock%'
2726+
IF @is_lock_event_type = 1
27042727
BEGIN
27052728
SELECT
27062729
event_time =
@@ -3626,16 +3649,16 @@ BEGIN
36263649
SET
36273650
hew.event_type_short =
36283651
CASE
3629-
WHEN hew.event_type LIKE N'%block%'
3652+
WHEN hew.event_type LIKE @lock_event_type_pattern
36303653
THEN N'[_]Blocking'
3631-
WHEN ( hew.event_type LIKE N'%comp%'
3632-
AND hew.event_type NOT LIKE N'%re%' )
3654+
WHEN ( hew.event_type LIKE @compile_event_type_pattern
3655+
AND hew.event_type NOT LIKE @recompile_exclude_event_type_pattern )
36333656
THEN N'[_]Compiles'
3634-
WHEN hew.event_type LIKE N'%quer%'
3657+
WHEN hew.event_type LIKE @query_event_type_pattern
36353658
THEN N'[_]Queries'
3636-
WHEN hew.event_type LIKE N'%recomp%'
3659+
WHEN hew.event_type LIKE @recompile_event_type_pattern
36373660
THEN N'[_]Recompiles'
3638-
WHEN hew.event_type LIKE N'%wait%'
3661+
WHEN hew.event_type LIKE @wait_event_type_pattern
36393662
THEN N'[_]Waits'
36403663
ELSE N'?'
36413664
END
@@ -3687,20 +3710,20 @@ BEGIN
36873710
SELECT
36883711
@table_sql =
36893712
CASE
3690-
WHEN @event_type_check LIKE N'%wait%'
3713+
WHEN @event_type_check LIKE @wait_event_type_pattern
36913714
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
36923715
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
36933716
N' database_name sysname NULL, wait_type nvarchar(60) NULL, duration_ms bigint NULL, signal_duration_ms bigint NULL, ' + @nc10 +
36943717
N' wait_resource sysname NULL, query_plan_hash_signed binary(8) NULL, query_hash_signed binary(8) NULL, plan_handle varbinary(64) NULL );'
3695-
WHEN @event_type_check LIKE N'%lock%'
3718+
WHEN @event_type_check LIKE @lock_event_type_pattern
36963719
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
36973720
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, ' + @nc10 +
36983721
N' activity nvarchar(20) NULL, database_name sysname NULL, database_id integer NULL, object_id bigint NULL, contentious_object AS OBJECT_NAME(object_id, database_id), ' + @nc10 +
36993722
N' transaction_id bigint NULL, resource_owner_type sysname NULL, monitor_loop integer NULL, spid integer NULL, ecid integer NULL, query_text nvarchar(max) NULL, ' +
37003723
N' wait_time bigint NULL, transaction_name sysname NULL, last_transaction_started nvarchar(30) NULL, wait_resource nvarchar(100) NULL, ' + @nc10 +
37013724
N' lock_mode nvarchar(10) NULL, status nvarchar(10) NULL, priority integer NULL, transaction_count integer NULL, ' + @nc10 +
37023725
N' client_app sysname NULL, host_name sysname NULL, login_name sysname NULL, isolation_level nvarchar(30) NULL, sql_handle varbinary(64) NULL, blocked_process_report XML NULL );'
3703-
WHEN @event_type_check LIKE N'%quer%'
3726+
WHEN @event_type_check LIKE @query_event_type_pattern
37043727
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
37053728
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
37063729
N' database_name sysname NULL, object_name nvarchar(512) NULL, sql_text nvarchar(max) NULL, statement nvarchar(max) NULL, ' + @nc10 +
@@ -3709,12 +3732,12 @@ BEGIN
37093732
N' spills_mb decimal(18,2) NULL, row_count decimal(18,2) NULL, estimated_rows decimal(18,2) NULL, dop integer NULL, ' + @nc10 +
37103733
N' serial_ideal_memory_mb decimal(18,2) NULL, requested_memory_mb decimal(18,2) NULL, used_memory_mb decimal(18,2) NULL, ideal_memory_mb decimal(18,2) NULL, ' + @nc10 +
37113734
N' granted_memory_mb decimal(18,2) NULL, query_plan_hash_signed binary(8) NULL, query_hash_signed binary(8) NULL, plan_handle varbinary(64) NULL );'
3712-
WHEN @event_type_check LIKE N'%recomp%'
3735+
WHEN @event_type_check LIKE @recompile_event_type_pattern
37133736
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
37143737
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
37153738
N' database_name sysname NULL, object_name nvarchar(512) NULL, recompile_cause sysname NULL, statement_text nvarchar(max) NULL, statement_text_checksum AS CHECKSUM(database_name + statement_text) PERSISTED '
37163739
+ CASE WHEN @compile_events = 1 THEN N', compile_cpu_ms bigint NULL, compile_duration_ms bigint NULL );' ELSE N' );' END
3717-
WHEN @event_type_check LIKE N'%comp%' AND @event_type_check NOT LIKE N'%re%'
3740+
WHEN @event_type_check LIKE @compile_event_type_pattern AND @event_type_check NOT LIKE @recompile_exclude_event_type_pattern
37183741
THEN N'CREATE TABLE ' + @object_name_check + @nc10 +
37193742
N'( id bigint PRIMARY KEY IDENTITY, server_name sysname NULL, event_time datetime2 NULL, event_type sysname NULL, ' + @nc10 +
37203743
N' database_name sysname NULL, object_name nvarchar(512) NULL, statement_text nvarchar(max) NULL, statement_text_checksum AS CHECKSUM(database_name + statement_text) PERSISTED '
@@ -4052,7 +4075,7 @@ END;
40524075
(
40534076
nvarchar(max),
40544077
CASE
4055-
WHEN @event_type_check LIKE N'%wait%' /*Wait stats!*/
4078+
WHEN @event_type_check LIKE @wait_event_type_pattern /*Wait stats!*/
40564079
THEN CONVERT
40574080
(
40584081
nvarchar(max),
@@ -4104,9 +4127,9 @@ OUTER APPLY xet.human_events_xml.nodes(''//event'') AS oa(c)
41044127
WHERE c.exist(''(data[@name="duration"]/value/text()[. > 0])'') = 1
41054128
AND c.exist(''@timestamp[. > sql:variable("@date_filter")]'') = 1;')
41064129
)
4107-
WHEN @event_type_check LIKE N'%lock%' /*Blocking!*/
4108-
/*To cut down on nonsense, I'm only inserting new blocking scenarios*/
4109-
/*Any existing blocking scenarios will update the blocking duration*/
4130+
WHEN @event_type_check LIKE @lock_event_type_pattern /*Blocking!*/
4131+
/*To cut down on nonsense, I'm only inserting new blocking scenarios*/
4132+
/*Any existing blocking scenarios will update the blocking duration*/
41104133
THEN CONVERT
41114134
(
41124135
nvarchar(max),
@@ -4283,7 +4306,7 @@ JOIN
42834306
AND x.hostname = x2.host_name
42844307
AND x.loginname = x2.login_name;
42854308
' ))
4286-
WHEN @event_type_check LIKE N'%quer%' /*Queries!*/
4309+
WHEN @event_type_check LIKE @query_event_type_pattern /*Queries!*/
42874310
THEN
42884311
CONVERT
42894312
(
@@ -4346,7 +4369,7 @@ OUTER APPLY xet.human_events_xml.nodes(''//event'') AS oa(c)
43464369
WHERE oa.c.exist(''@timestamp[. > sql:variable("@date_filter")]'') = 1
43474370
AND oa.c.exist(''(action[@name="query_hash_signed"]/value[. != 0])'') = 1; '
43484371
))
4349-
WHEN @event_type_check LIKE N'%recomp%' /*Recompiles!*/
4372+
WHEN @event_type_check LIKE @recompile_event_type_pattern /*Recompiles!*/
43504373
THEN
43514374
CONVERT
43524375
(
@@ -4393,7 +4416,7 @@ AND oa.c.exist(''@timestamp[. > sql:variable("@date_filter")]'') = 1
43934416
ORDER BY
43944417
event_time;'
43954418
)))
4396-
WHEN @event_type_check LIKE N'%comp%' AND @event_type_check NOT LIKE N'%re%' /*Compiles!*/
4419+
WHEN @event_type_check LIKE @compile_event_type_pattern AND @event_type_check NOT LIKE @recompile_exclude_event_type_pattern /*Compiles!*/
43974420
THEN
43984421
CONVERT
43994422
(

0 commit comments

Comments
 (0)