Skip to content

Commit 68d832c

Browse files
Merge pull request #653 from erikdarlingdata/dev
Dev
2 parents f929cb5 + 4d234e1 commit 68d832c

1 file changed

Lines changed: 169 additions & 61 deletions

File tree

sp_HumanEvents/sp_HumanEvents.sql

Lines changed: 169 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ CREATE TABLE
375375
human_events_xml xml
376376
);
377377

378+
CREATE TABLE
379+
#human_events_xml
380+
(
381+
human_events_xml xml
382+
);
383+
378384
CREATE TABLE
379385
#wait
380386
(
@@ -1755,8 +1761,55 @@ WAITFOR DELAY @waitfor;
17551761

17561762

17571763
/* Dump whatever we got into a temp table */
1758-
IF @azure = 0
1764+
IF LOWER(@target_output) = N'ring_buffer'
17591765
BEGIN
1766+
IF @azure = 0
1767+
BEGIN
1768+
INSERT
1769+
#x WITH(TABLOCK)
1770+
(
1771+
x
1772+
)
1773+
SELECT
1774+
x =
1775+
CONVERT
1776+
(
1777+
xml,
1778+
t.target_data
1779+
)
1780+
FROM sys.dm_xe_session_targets AS t
1781+
JOIN sys.dm_xe_sessions AS s
1782+
ON s.address = t.event_session_address
1783+
WHERE s.name = @session_name
1784+
AND t.target_name = N'ring_buffer';
1785+
END;
1786+
ELSE
1787+
BEGIN
1788+
INSERT
1789+
#x WITH(TABLOCK)
1790+
(
1791+
x
1792+
)
1793+
SELECT
1794+
x =
1795+
CONVERT
1796+
(
1797+
xml,
1798+
t.target_data
1799+
)
1800+
FROM sys.dm_xe_database_session_targets AS t
1801+
JOIN sys.dm_xe_database_sessions AS s
1802+
ON s.address = t.event_session_address
1803+
WHERE s.name = @session_name
1804+
AND t.target_name = N'ring_buffer';
1805+
END;
1806+
END;
1807+
ELSE IF LOWER(@target_output) = N'event_file'
1808+
BEGIN
1809+
/*
1810+
Read from event file target
1811+
Azure SQL Database and Managed Instance are not supported for event_file
1812+
*/
17601813
INSERT
17611814
#x WITH(TABLOCK)
17621815
(
@@ -1767,41 +1820,46 @@ BEGIN
17671820
CONVERT
17681821
(
17691822
xml,
1770-
t.target_data
1823+
f.event_data
17711824
)
1772-
FROM sys.dm_xe_session_targets AS t
1773-
JOIN sys.dm_xe_sessions AS s
1774-
ON s.address = t.event_session_address
1775-
WHERE s.name = @session_name
1776-
AND t.target_name = N'ring_buffer';
1825+
FROM sys.fn_xe_file_target_read_file
1826+
(
1827+
@session_name + N'*.xel',
1828+
NULL,
1829+
NULL,
1830+
NULL
1831+
) AS f;
17771832
END;
1778-
ELSE
1833+
1834+
1835+
/*
1836+
Parse XML events based on target output type
1837+
ring_buffer wraps events in RingBufferTarget node, event_file does not
1838+
*/
1839+
IF LOWER(@target_output) = N'ring_buffer'
17791840
BEGIN
17801841
INSERT
1781-
#x WITH(TABLOCK)
1842+
#human_events_xml WITH(TABLOCK)
17821843
(
1783-
x
1844+
human_events_xml
17841845
)
17851846
SELECT
1786-
x =
1787-
CONVERT
1788-
(
1789-
xml,
1790-
t.target_data
1791-
)
1792-
FROM sys.dm_xe_database_session_targets AS t
1793-
JOIN sys.dm_xe_database_sessions AS s
1794-
ON s.address = t.event_session_address
1795-
WHERE s.name = @session_name
1796-
AND t.target_name = N'ring_buffer';
1847+
human_events_xml = e.x.query('.')
1848+
FROM #x AS x
1849+
CROSS APPLY x.x.nodes('/RingBufferTarget/event') AS e(x);
1850+
END;
1851+
ELSE IF LOWER(@target_output) = N'event_file'
1852+
BEGIN
1853+
INSERT
1854+
#human_events_xml WITH(TABLOCK)
1855+
(
1856+
human_events_xml
1857+
)
1858+
SELECT
1859+
human_events_xml = e.x.query('.')
1860+
FROM #x AS x
1861+
CROSS APPLY x.x.nodes('/event') AS e(x);
17971862
END;
1798-
1799-
1800-
SELECT
1801-
human_events_xml = e.x.query('.')
1802-
INTO #human_events_xml
1803-
FROM #x AS x
1804-
CROSS APPLY x.x.nodes('/RingBufferTarget/event') AS e(x);
18051863

18061864

18071865
IF @debug = 1
@@ -4524,8 +4582,55 @@ ORDER BY
45244582
);
45254583

45264584
/* this table is only used for the inserts, hence the "internal" in the name */
4527-
IF @azure = 0
4585+
IF LOWER(@target_output) = N'ring_buffer'
4586+
BEGIN
4587+
IF @azure = 0
4588+
BEGIN
4589+
INSERT
4590+
#x WITH(TABLOCK)
4591+
(
4592+
x
4593+
)
4594+
SELECT
4595+
x =
4596+
CONVERT
4597+
(
4598+
xml,
4599+
t.target_data
4600+
)
4601+
FROM sys.dm_xe_session_targets AS t
4602+
JOIN sys.dm_xe_sessions AS s
4603+
ON s.address = t.event_session_address
4604+
WHERE s.name = @event_type_check
4605+
AND t.target_name = N'ring_buffer';
4606+
END;
4607+
ELSE
4608+
BEGIN
4609+
INSERT
4610+
#x WITH(TABLOCK)
4611+
(
4612+
x
4613+
)
4614+
SELECT
4615+
x =
4616+
CONVERT
4617+
(
4618+
xml,
4619+
t.target_data
4620+
)
4621+
FROM sys.dm_xe_database_session_targets AS t
4622+
JOIN sys.dm_xe_database_sessions AS s
4623+
ON s.address = t.event_session_address
4624+
WHERE s.name = @event_type_check
4625+
AND t.target_name = N'ring_buffer';
4626+
END;
4627+
END;
4628+
ELSE IF LOWER(@target_output) = N'event_file'
45284629
BEGIN
4630+
/*
4631+
Read from event file target
4632+
Azure SQL Database and Managed Instance are not supported for event_file
4633+
*/
45294634
INSERT
45304635
#x WITH(TABLOCK)
45314636
(
@@ -4536,46 +4641,49 @@ ORDER BY
45364641
CONVERT
45374642
(
45384643
xml,
4539-
t.target_data
4644+
f.event_data
45404645
)
4541-
FROM sys.dm_xe_session_targets AS t
4542-
JOIN sys.dm_xe_sessions AS s
4543-
ON s.address = t.event_session_address
4544-
WHERE s.name = @event_type_check
4545-
AND t.target_name = N'ring_buffer';
4646+
FROM sys.fn_xe_file_target_read_file
4647+
(
4648+
@event_type_check + N'*.xel',
4649+
NULL,
4650+
NULL,
4651+
NULL
4652+
) AS f;
45464653
END;
4547-
ELSE
4654+
4655+
/*
4656+
Parse XML events based on target output type
4657+
ring_buffer wraps events in RingBufferTarget node, event_file does not
4658+
*/
4659+
IF LOWER(@target_output) = N'ring_buffer'
45484660
BEGIN
45494661
INSERT
4550-
#x WITH(TABLOCK)
4662+
#human_events_xml_internal WITH(TABLOCK)
45514663
(
4552-
x
4664+
human_events_xml
45534665
)
45544666
SELECT
4555-
x =
4556-
CONVERT
4557-
(
4558-
xml,
4559-
t.target_data
4560-
)
4561-
FROM sys.dm_xe_database_session_targets AS t
4562-
JOIN sys.dm_xe_database_sessions AS s
4563-
ON s.address = t.event_session_address
4564-
WHERE s.name = @event_type_check
4565-
AND t.target_name = N'ring_buffer';
4667+
human_events_xml =
4668+
e.x.query('.')
4669+
FROM #x AS x
4670+
CROSS APPLY x.x.nodes('/RingBufferTarget/event') AS e(x)
4671+
WHERE e.x.exist('@timestamp[. > sql:variable("@date_filter")]') = 1;
4672+
END;
4673+
ELSE IF LOWER(@target_output) = N'event_file'
4674+
BEGIN
4675+
INSERT
4676+
#human_events_xml_internal WITH(TABLOCK)
4677+
(
4678+
human_events_xml
4679+
)
4680+
SELECT
4681+
human_events_xml =
4682+
e.x.query('.')
4683+
FROM #x AS x
4684+
CROSS APPLY x.x.nodes('/event') AS e(x)
4685+
WHERE e.x.exist('@timestamp[. > sql:variable("@date_filter")]') = 1;
45664686
END;
4567-
4568-
INSERT
4569-
#human_events_xml_internal WITH(TABLOCK)
4570-
(
4571-
human_events_xml
4572-
)
4573-
SELECT
4574-
human_events_xml =
4575-
e.x.query('.')
4576-
FROM #x AS x
4577-
CROSS APPLY x.x.nodes('/RingBufferTarget/event') AS e(x)
4578-
WHERE e.x.exist('@timestamp[. > sql:variable("@date_filter")]') = 1;
45794687

45804688
IF @debug = 1
45814689
BEGIN

0 commit comments

Comments
 (0)