Skip to content

Commit a06125f

Browse files
Merge pull request #383 from erikdarlingdata/fix/blocked-process-utc-timezone
Fix UTC/local timezone mismatch in blocked process XML processor
2 parents dacaf61 + 699f248 commit a06125f

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

install/23_process_blocked_process_xml.sql

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ BEGIN
5252
@rows_marked bigint = 0,
5353
@rows_parsed bigint = 0,
5454
@start_time datetime2(7) = SYSDATETIME(),
55+
@utc_offset_minutes integer = DATEDIFF(MINUTE, GETUTCDATE(), SYSDATETIME()),
56+
@start_date_local datetime2(7) = NULL,
57+
@end_date_local datetime2(7) = NULL,
5558
@error_message nvarchar(4000),
5659
@error_number integer,
5760
@blockviewer_database sysname = NULL,
@@ -136,23 +139,44 @@ BEGIN
136139
AND bx.event_time IS NOT NULL
137140
OPTION(RECOMPILE);
138141

142+
/*
143+
Convert UTC event_time to local time for sp_HumanEventsBlockViewer
144+
The proc expects local time inputs and converts to UTC internally
145+
Raw table event_time is UTC (from XE @timestamp attribute)
146+
*/
147+
SELECT
148+
@start_date_local = DATEADD(MINUTE, @utc_offset_minutes, @start_date),
149+
@end_date_local = DATEADD(MINUTE, @utc_offset_minutes, @end_date);
150+
139151
IF @debug = 1
140152
BEGIN
141-
SET @debug_msg = N'Derived date range from unprocessed rows: ' + ISNULL(CONVERT(nvarchar(30), @start_date, 121), N'NULL') + N' to ' + ISNULL(CONVERT(nvarchar(30), @end_date, 121), N'NULL');
153+
SET @debug_msg = N'Derived date range (UTC): ' + ISNULL(CONVERT(nvarchar(30), @start_date, 121), N'NULL') + N' to ' + ISNULL(CONVERT(nvarchar(30), @end_date, 121), N'NULL');
154+
RAISERROR(@debug_msg, 0, 1) WITH NOWAIT;
155+
SET @debug_msg = N'Converted to local: ' + ISNULL(CONVERT(nvarchar(30), @start_date_local, 121), N'NULL') + N' to ' + ISNULL(CONVERT(nvarchar(30), @end_date_local, 121), N'NULL');
142156
RAISERROR(@debug_msg, 0, 1) WITH NOWAIT;
143157
END;
144158
END;
159+
ELSE
160+
BEGIN
161+
/*
162+
User provided explicit dates (assumed local time)
163+
No conversion needed — pass through directly
164+
*/
165+
SELECT
166+
@start_date_local = @start_date,
167+
@end_date_local = @end_date;
168+
END;
145169

146170
/*
147171
Delete existing parsed blocking events for the time range to prevent duplicates
148172
sp_HumanEventsBlockViewer will re-insert fresh parsed data
149173
*/
150-
IF @start_date IS NOT NULL AND @end_date IS NOT NULL
174+
IF @start_date_local IS NOT NULL AND @end_date_local IS NOT NULL
151175
BEGIN
152176
DELETE b
153177
FROM collect.blocking_BlockedProcessReport AS b
154-
WHERE b.event_time >= @start_date
155-
AND b.event_time <= @end_date;
178+
WHERE b.event_time >= @start_date_local
179+
AND b.event_time <= @end_date_local;
156180

157181
SELECT
158182
@rows_deleted = ROWCOUNT_BIG();
@@ -192,19 +216,20 @@ BEGIN
192216
N'@log_retention_days integer, @max_events_to_process integer, @start_date datetime2(7), @end_date datetime2(7), @debug bit',
193217
@log_retention_days = @log_retention_days,
194218
@max_events_to_process = @max_events_to_process,
195-
@start_date = @start_date,
196-
@end_date = @end_date,
219+
@start_date = @start_date_local,
220+
@end_date = @end_date_local,
197221
@debug = @debug;
198222

199223
/*
200224
Verify sp_HumanEventsBlockViewer produced parsed results before marking rows as processed
201225
If no results were inserted, leave rows unprocessed so they are retried next run
226+
Parsed results use local time (sp_HumanEventsBlockViewer converts UTC to local)
202227
*/
203228
SELECT
204229
@rows_parsed = COUNT_BIG(*)
205230
FROM collect.blocking_BlockedProcessReport AS b
206-
WHERE b.event_time >= @start_date
207-
AND b.event_time <= @end_date
231+
WHERE b.event_time >= @start_date_local
232+
AND b.event_time <= @end_date_local
208233
OPTION(RECOMPILE);
209234

210235
IF @rows_parsed > 0

0 commit comments

Comments
 (0)