SQL Server Traces

I have been using SQL Server Profiler for years to run traces against SQL Server, it is a indispensable resource for when you need to find out what is being ran against your database, or when you really need to dive into what is going on. If you are not familar with SQL Server Profiler then check out this article on TechRepublic and then come back here.

What I have not used is server side tracing. SQL Server Profiler is just a GUI built on top of the tracing functionality that is built into the SQL Server engine. It turns out that using tracing functionality in SQL Server can be very powerful, and SQL Server Profiler is actually a great script generator for building your own traces. Let me give a example to clear things up.  I would run this example on a test database.  When running a trace on a busy production server I would apply more filters, and only trace the events that you need in order to avoid causing any performance issues.

Launch SQL Server Profiler and start a trace against your database, you can just use the Standard template that is built into SQL Server Profiler. Start the trace and then stop it (you have to have started the trace in order to be able to generate a script). Then go to the menu and select File -> Export -> Script Trace Definition -> For SQL Server 2005 ..2008. This will save a sql file. Open this file up in SQL Server Management Studio and you will see something like this:
/****************************************************/
/* Created by: SQL Server 2008 R2 Profiler */
/* Date: 06/17/2010 08:39:09 PM */
/****************************************************/

-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 5

-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share

exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL
if (@rc != 0) goto error

-- Client side File and Table cannot be scripted

-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 14, 1, @on
exec sp_trace_setevent @TraceID, 14, 9, @on
exec sp_trace_setevent @TraceID, 14, 6, @on
exec sp_trace_setevent @TraceID, 14, 10, @on
exec sp_trace_setevent @TraceID, 14, 14, @on
exec sp_trace_setevent @TraceID, 14, 11, @on
exec sp_trace_setevent @TraceID, 14, 12, @on
exec sp_trace_setevent @TraceID, 15, 15, @on
exec sp_trace_setevent @TraceID, 15, 16, @on
exec sp_trace_setevent @TraceID, 15, 9, @on
exec sp_trace_setevent @TraceID, 15, 17, @on
exec sp_trace_setevent @TraceID, 15, 6, @on
exec sp_trace_setevent @TraceID, 15, 10, @on
exec sp_trace_setevent @TraceID, 15, 14, @on
exec sp_trace_setevent @TraceID, 15, 18, @on
exec sp_trace_setevent @TraceID, 15, 11, @on
exec sp_trace_setevent @TraceID, 15, 12, @on
exec sp_trace_setevent @TraceID, 15, 13, @on
exec sp_trace_setevent @TraceID, 17, 1, @on
exec sp_trace_setevent @TraceID, 17, 9, @on
exec sp_trace_setevent @TraceID, 17, 6, @on
exec sp_trace_setevent @TraceID, 17, 10, @on
exec sp_trace_setevent @TraceID, 17, 14, @on
exec sp_trace_setevent @TraceID, 17, 11, @on
exec sp_trace_setevent @TraceID, 17, 12, @on
exec sp_trace_setevent @TraceID, 10, 15, @on
exec sp_trace_setevent @TraceID, 10, 16, @on
exec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 2, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 12, 15, @on
exec sp_trace_setevent @TraceID, 12, 16, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 9, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 18, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 13, 1, @on
exec sp_trace_setevent @TraceID, 13, 9, @on
exec sp_trace_setevent @TraceID, 13, 6, @on
exec sp_trace_setevent @TraceID, 13, 10, @on
exec sp_trace_setevent @TraceID, 13, 14, @on
exec sp_trace_setevent @TraceID, 13, 11, @on
exec sp_trace_setevent @TraceID, 13, 12, @on

-- Set the Filters
declare @intfilter int
declare @bigintfilter bigint

exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 73dd2758-d97c-405b-ae7e-3f67bee289ba'
-- Set the trace status to start
exec sp_trace_setstatus @TraceID, 1

-- display trace id for future references
select TraceID=@TraceID
goto finish

error:
select ErrorCode=@rc

finish:
go

The first section of this code is setting up @TraceId and @maxfilesize variables. The @TraceId will be used to get the id of the trace back from the stored procedure that creates the trace. The @maxfilesize variable configures how big the file is allowed to get, once the file reaches this size the trace stops.

The next section of code creates the trace by calling the sp_trace_create stored procedure. This creates the trace but does not start it. The first parameter passed to this stored procedure is the @TraceId, this is an output parameter and this parameter will be populated with the Id of the trace that is created. The second parameter specifies whether to use rollover files, when this parameter is set to 0 then rollover files will not be used, and when the file reaches the max file size the trace stops. If this parameter is set to 1 then when the file size reaches the max size, then it rolls over to a new file. The third parameter specifies what file to output the trace to, when SQL Server Profiler generates the script it just puts a generic value in here, you need to replace it with the path and file name that you want the trace data saved to. The forth parameter specifies the max file size.

Next the script uses a bunch of sp_trace_setevent statements to configure what events should be traced, this is the part that is really nice to have generated for you.

Next the script uses the sp_trace_setfilter stored procedure to set the filters for the trace.

Finally it uses the sp_trace_setstatus stored procedure to set the status of the trace to 1, which starts the trace.

Once the trace has started collecting data you can used the fn_trace_gettable function to query the data in the file. This function takes two parameters, the first parameter is the file to load, and the second parameter is how many rollover files to load. If you are not using rollover files then you can just pass in a value of 1 for the second parameter. The beauty of this is that you can easily us a SELECT INTO statement to load the file into a table, and then run whatever queries you want against it. For example to load my trace file into a table I used the following query:
SELECT *
INTO MYTraceTable
FROM fn_trace_gettable(N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\testrace.trc', 1);

This loads the trace data from the file into the MyTraceTable table. You an not run whatever queries you want against this data.

Personally I have found that when I am debugging a application, and want to see what SQL code is being executed against a database it is easier to just use SQL Server Profiler. SQL Server Profiler makes it very easy to start and stop a trace, and it displays the SQL code in a format that is fairly easy to read. When I am run a trace that I want to being running unattended, or if I am looking at performance data, then I find server side traces to be more useful.

Mirth Connect Part 4 – Creating A Simple HL7 Interface

In this post I will go over how to create a simple HL7 interface that just accepts a HL7 message on a TCP port and then writes the message to a file. This will be a very simple interface, but it will get you familar with working with channels, sources, and destinations.

To begin, launch the Mirth Connect Administrator program, and go to the ‘Channels’ area.

Mirth Administrator Channels

A channel in Mirth is what I normally call a interface.  It contains the sources, destinations, and all of the transformations and filters that make up a interface.  So in order to create a new interface, we want to create a new Channel.  To create a new Channel, click on ‘New Channel’ under the ‘Channel Tasks’ section.  This will take you to the ‘Edit Channel’ window, and you will be on the summary tab.

Mirth Edit Channel

If you click on ‘Help on this topic’ under the ‘Other’ section you will be taken to the Mirth help page, which gives a good explanation of all the options on the summary tab.  I am going to name my channel ‘Simple Interface’, I am going to have it enabled, the incoming data is going to be HL7 2.x, I am going to synchronize the channel, and I am going to store the message data indefinitely.

Next click on the ‘Source’ tab to define the source for this interface.  The source is how the data goes into the interface.

Mirth Channel Source

Again, you can click on ‘Help on this topic’ under the ‘Other’ section to get a good description of all the options on this screen.  At the top of this window you select what type of connector you want to use.  You will see that Mirth offers a large number of options for contecting to data.  For this example we want to get HL7 information on a TCP socket.  Almost all HL7 interfaces that go over TCP use LLP as their protocal, so I will select the LLP Listener.

Since I am receiving the data, I want to configure the LLP Mode to Server, and I can just enter 127.0.0.1 to let it know that I want to listen on the local address.  You can pick whatever port you would like to listen to, for my example I will be listening on port 6661.  I am going to accept the default for the rest of the settings.  You can also specify filters and transformers on the source in order to filter out messages and make changes to the message, but I am not going to cover that in this post.

Next we need to create our destination.  The destination species where the data is sent to.  In Mirth you can have multiple destinations.  You can either always send the data to all destinations, or apply filters to determine what destination the data is sent to.  To create destinations you go to the ‘Destinations’ tab in the Edit Channel window.

Mirth File Destination

There will already be one destination created for you.  Just like the source, you select a Connector Type.  We want to write the HL7 message to a file, so select the ‘File Writer’ message type.  For the method select file, enter the directory that you want to write the file to.  For the file name, drag ‘Message ID’ over from the Destination Mappings area to the file name field to have it use the message id as the file name.  The template section determines what is written to the file, drag ‘Encoded Data’ from the Destination Mappings section to the Template filed to have it write the contents of the HL7 message to the file.

We now have the channel completed, click on ‘Save Changes’ under the Channel Tasks section in order to save the channel.  We now need to deploy the channel in order to make it active so we can use it.  Under the ‘Mirth Connect’ section click on ‘Channels’ to get back to the Channels window.  You will now see you channel listed.

Mirth Channel

To deploy your channel click on ‘Deploy All’ under the ‘Channel Tasks’ section.  Mirth will deploy your channel and switch to the Dashboard window.  We are now ready to test the interface.

There are multiple tools that are available that you can use to test the interface by sending a HL7 message to the interface.  I used HL7 Browser.  Open a HL7 message in HL7 Browser and send it to your local computer to port 6661.  You will see the dashboard in Mirth update to show that it sent the message, and you will see the HL7 message written out to a file.

Tags: ,