Skip to content

Commit 1cb9cfa

Browse files
Merge pull request #618 from ReeceGoding/dev
Made sp_HumanEventsBlockViewer not error out when system_health is used
2 parents 9dea67d + f268a59 commit 1cb9cfa

2 files changed

Lines changed: 79 additions & 37 deletions

File tree

sp_HumanEvents/README.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ EXECUTE dbo.sp_HumanEvents
122122

123123
This was originally a companion script to analyze the blocked process report Extended Event created by sp_HumanEvents, but has since turned into its own monster.
124124

125-
It will work on any Extended Event that captures the blocked process report. If you need to set that up, run these two pieces of code.
125+
It will work on any Extended Event that captures the blocked process report. If you need to set that up, run the next two pieces of code.
126+
127+
The system_health Extended Event works, but its blocked process report is much less comprehensive than the real thing. I do not allow logging to a table from this, because the set of columns and available data is too incomplete, and I don't want to juggle multiple table definitions.
126128

127129
## Setup
128130

@@ -172,28 +174,28 @@ ON SERVER
172174

173175
## Parameters
174176

175-
| parameter_name | data_type | description | valid_inputs | defaults |
176-
|-----------------------|-----------|-------------------------------------------------|------------------------------------------------------------------------|------------------------------------|
177-
| @session_name | sysname | name of the extended event session to pull from | extended event session name capturing sqlserver.blocked_process_report | keeper_HumanEvents_blocking |
178-
| @target_type | sysname | target of the extended event session | event_file or ring_buffer | NULL |
179-
| @start_date | datetime2 | filter by date | a reasonable date | NULL; will shortcut to last 7 days |
180-
| @end_date | datetime2 | filter by date | a reasonable date | NULL |
181-
| @database_name | sysname | filter by database name | a database that exists on this server | NULL |
182-
| @object_name | sysname | filter by table name | a schema-prefixed table name | NULL |
183-
| @target_database | sysname | database containing the table with BPR data | a valid database name | NULL |
184-
| @target_schema | sysname | schema of the table | a valid schema name | NULL |
185-
| @target_table | sysname | table name | a valid table name | NULL |
186-
| @target_column | sysname | column containing XML data | a valid column name | NULL |
187-
| @timestamp_column | sysname | column containing timestamp (optional) | a valid column name | NULL |
188-
| @log_to_table | bit | enable logging to permanent tables | 0 or 1 | 0 |
189-
| @log_database_name | sysname | database to store logging tables | a valid database name | NULL |
190-
| @log_schema_name | sysname | schema to store logging tables | a valid schema name | NULL |
191-
| @log_table_name_prefix| sysname | prefix for all logging tables | a valid table name prefix | 'HumanEventsBlockViewer' |
192-
| @log_retention_days | integer | Number of days to keep logs, 0 = keep indefinitely | a valid integer | 30 |
193-
| @help | bit | how you got here | 0 or 1 | 0 |
194-
| @debug | bit | dumps raw temp table contents | 0 or 1 | 0 |
195-
| @version | varchar | OUTPUT; for support | none; OUTPUT | none; OUTPUT |
196-
| @version_date | datetime | OUTPUT; for support | none; OUTPUT | none; OUTPUT |
177+
| parameter_name | data_type | description | valid_inputs | defaults |
178+
|-----------------------|-----------|----------------------------------------------------|--------------------------------------------------------------------------------------------------|------------------------------------|
179+
| @session_name | sysname | name of the extended event session to pull from | extended event session name capturing sqlserver.blocked_process_report, system_health also works | keeper_HumanEvents_blocking |
180+
| @target_type | sysname | target of the extended event session | event_file or ring_buffer or table | NULL |
181+
| @start_date | datetime2 | filter by date | a reasonable date | NULL; will shortcut to last 7 days |
182+
| @end_date | datetime2 | filter by date | a reasonable date | NULL |
183+
| @database_name | sysname | filter by database name | a database that exists on this server | NULL |
184+
| @object_name | sysname | filter by table name | a schema-prefixed table name | NULL |
185+
| @target_database | sysname | database containing the table with BPR data | a valid database name | NULL |
186+
| @target_schema | sysname | schema of the table | a valid schema name | NULL |
187+
| @target_table | sysname | table name | a valid table name | NULL |
188+
| @target_column | sysname | column containing XML data | a valid column name | NULL |
189+
| @timestamp_column | sysname | column containing timestamp (optional) | a valid column name | NULL |
190+
| @log_to_table | bit | enable logging to permanent tables | 0 or 1 | 0 |
191+
| @log_database_name | sysname | database to store logging tables | a valid database name | NULL |
192+
| @log_schema_name | sysname | schema to store logging tables | a valid schema name | NULL |
193+
| @log_table_name_prefix| sysname | prefix for all logging tables | a valid table name prefix | 'HumanEventsBlockViewer' |
194+
| @log_retention_days | integer | Number of days to keep logs, 0 = keep indefinitely | a valid integer | 30 |
195+
| @help | bit | how you got here | 0 or 1 | 0 |
196+
| @debug | bit | dumps raw temp table contents | 0 or 1 | 0 |
197+
| @version | varchar | OUTPUT; for support | none; OUTPUT | none; OUTPUT |
198+
| @version_date | datetime | OUTPUT; for support | none; OUTPUT | none; OUTPUT |
197199

198200
## Usage Examples
199201

@@ -219,4 +221,4 @@ EXECUTE dbo.sp_HumanEventsBlockViewer
219221
@log_to_table = 1,
220222
@log_database_name = 'DBA',
221223
@log_schema_name = 'dbo';
222-
```
224+
```

sp_HumanEvents/sp_HumanEventsBlockViewer.sql

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ BEGIN
105105
SELECT 'it will also work with any other extended event session that captures blocking' UNION ALL
106106
SELECT 'just use the @session_name parameter to point me there' UNION ALL
107107
SELECT 'EXECUTE dbo.sp_HumanEventsBlockViewer @session_name = N''blocked_process_report'';' UNION ALL
108+
SELECT 'the system_health session also works, if you are okay with its lousy blocked process report'
108109
SELECT 'all scripts and documentation are available here: https://code.erikdarling.com' UNION ALL
109110
SELECT 'from your loving sql server consultant, erik darling: https://erikdarling.com';
110111

@@ -137,8 +138,8 @@ BEGIN
137138
END,
138139
valid_inputs =
139140
CASE ap.name
140-
WHEN N'@session_name' THEN 'extended event session name capturing sqlserver.blocked_process_report'
141-
WHEN N'@target_type' THEN 'event_file or ring_buffer'
141+
WHEN N'@session_name' THEN 'extended event session name capturing sqlserver.blocked_process_report, system_health also works'
142+
WHEN N'@target_type' THEN 'event_file or ring_buffer or table'
142143
WHEN N'@start_date' THEN 'a reasonable date'
143144
WHEN N'@end_date' THEN 'a reasonable date'
144145
WHEN N'@database_name' THEN 'a database that exists on this server'
@@ -195,7 +196,7 @@ BEGIN
195196
N'check the messages tab for setup commands';
196197

197198
RAISERROR('
198-
The blocked process report needs to be enabled:
199+
Unless you want to use the lousy version in system_health, the blocked process report needs to be enabled:
199200
EXECUTE sys.sp_configure ''show advanced options'', 1;
200201
EXECUTE sys.sp_configure ''blocked process threshold'', 5; /* Seconds of blocking before a report is generated */
201202
RECONFIGURE;', 0, 1) WITH NOWAIT;
@@ -273,8 +274,9 @@ IF EXISTS
273274
WHERE c.name = N'blocked process threshold (s)'
274275
AND CONVERT(int, c.value_in_use) = 0
275276
)
277+
AND @session_name NOT LIKE N'system%health'
276278
BEGIN
277-
RAISERROR(N'The blocked process report needs to be enabled:
279+
RAISERROR(N'Unless you want to use the lousy version in system_health, the blocked process report needs to be enabled:
278280
EXECUTE sys.sp_configure ''show advanced options'', 1;
279281
EXECUTE sys.sp_configure ''blocked process threshold'', 5; /* Seconds of blocking before a report is generated */
280282
RECONFIGURE;',
@@ -291,6 +293,7 @@ IF EXISTS
291293
WHERE c.name = N'blocked process threshold (s)'
292294
AND CONVERT(int, c.value_in_use) <> 5
293295
)
296+
AND @session_name NOT LIKE N'system%health'
294297
BEGIN
295298
RAISERROR(N'For best results, set up the blocked process report like this:
296299
EXECUTE sys.sp_configure ''show advanced options'', 1;
@@ -439,6 +442,47 @@ BEGIN
439442
FROM {table_check};
440443
END;';
441444

445+
IF @debug = 1
446+
AND @is_system_health = 0
447+
BEGIN
448+
RAISERROR('We are not using system_health', 0, 1) WITH NOWAIT;
449+
END;
450+
451+
IF @is_system_health = 1
452+
BEGIN
453+
RAISERROR('For best results, consider not using system_health as your target. Re-run with @help = 1 for guidance.', 0, 1) WITH NOWAIT;
454+
END
455+
456+
/*
457+
Note: I do not allow logging to a table from system_health, because the set of columns
458+
and available data is too incomplete, and I don't want to juggle multiple
459+
table definitions.
460+
461+
Logging to a table is only allowed from a blocked_process_report Extended Event,
462+
but it can either be ring buffer or file target. I don't care about that.
463+
*/
464+
IF @is_system_health = 1
465+
AND
466+
(
467+
LOWER(@target_type) = N'table'
468+
OR @log_to_table = 1
469+
)
470+
BEGIN
471+
RAISERROR('Logging system_health to a table is not supported.
472+
Either pick a different session or change both
473+
@target_type to be ''event_file'' or ''ring_buffer''
474+
and @log_to_table to be 0.', 11, 0) WITH NOWAIT;
475+
RETURN;
476+
END
477+
478+
IF @is_system_health = 1
479+
AND @target_type IS NULL
480+
BEGIN
481+
RAISERROR('No @target_type specified, using the ''event_file'' for system_health.', 0, 1) WITH NOWAIT;
482+
SELECT
483+
@target_type = 'event_file';
484+
END
485+
442486
SELECT
443487
@azure_msg =
444488
CONVERT(nchar(1), @azure),
@@ -450,6 +494,7 @@ IF ISNULL(@target_database, DB_NAME()) IS NOT NULL
450494
AND ISNULL(@target_schema, N'dbo') IS NOT NULL
451495
AND @target_table IS NOT NULL
452496
AND @target_column IS NOT NULL
497+
AND @is_system_health = 0
453498
BEGIN
454499
SET @target_type = N'table';
455500
END;
@@ -1125,18 +1170,13 @@ END;
11251170

11261171
/*
11271172
This section is special for the well-hidden and much less comprehensive blocked
1128-
process report stored in the system health extended event session
1129-
1130-
Note: I do not allow logging to a table from this, because the set of columns
1131-
and available data is too incomplete, and I don't want to juggle multiple
1132-
table definitions.
1173+
process report stored in the system health extended event session.
11331174
1134-
Logging to a table is only allowed from the a blocked_process_report Extended Event,
1135-
but it can either be ring buffer or file target. I don't care about that.
1175+
We disallow many features here.
1176+
See where @is_system_health was declared for details.
1177+
That is also where we error out if somebody tries to use an unsupported feature.
11361178
*/
11371179
IF @is_system_health = 1
1138-
AND LOWER(@target_type) <> N'table'
1139-
AND @log_to_table = 0
11401180
BEGIN
11411181
IF @debug = 1
11421182
BEGIN

0 commit comments

Comments
 (0)