Ejemplo n.º 1
0
        public override void ReadEvents()
        {
            EventCount = 0;
            SqlTransformer transformer = new SqlTransformer();

            using (QueryableXEventData eventstream = new QueryableXEventData(
                       ConnectionString,
                       SessionName,
                       EventStreamSourceOptions.EventStream,
                       EventStreamCacheOptions.DoNotCache))
            {
                var eventsEnumerator = eventstream.GetEnumerator();

                while (!stopped && eventsEnumerator.MoveNext())
                {
                    PublishedEvent         evt  = eventsEnumerator.Current;
                    ExecutionWorkloadEvent evnt = new ExecutionWorkloadEvent();

                    string commandText = String.Empty;
                    if (evt.Name == "rpc_completed")
                    {
                        commandText = (string)TryGetValue(evt, FieldType.Field, "statement");
                        evnt.Type   = WorkloadEvent.EventType.RPCCompleted;
                    }
                    else if (evt.Name == "sql_batch_completed")
                    {
                        commandText = (string)TryGetValue(evt, FieldType.Field, "batch_text");
                        evnt.Type   = WorkloadEvent.EventType.BatchCompleted;
                    }
                    else if (evt.Name == "attention")
                    {
                        object value = TryGetValue(evt, FieldType.Action, "sql_text");
                        try
                        {
                            if (value is string)
                            {
                                commandText = (string)value;
                            }
                            else if (value is byte[])
                            {
                                commandText = Encoding.Unicode.GetString((byte[])value);
                            }
                            else
                            {
                                throw new ArgumentException("Argument is of the wrong type");
                            }
                        }
                        catch (Exception e)
                        {
                            logger.Error(e, $"Unable to extract sql_text from attention event. Value is of type ${value.GetType().FullName}");
                        }
                        evnt.Type = WorkloadEvent.EventType.Timeout;
                    }
                    else if (evt.Name == "user_event")
                    {
                        int num = (int)TryGetValue(evt, FieldType.Field, "event_id");
                        if (num == 83)
                        {
                            commandText = (string)TryGetValue(evt, FieldType.Field, "user_data");
                            evnt.Type   = WorkloadEvent.EventType.Error;
                        }
                    }
                    else
                    {
                        evnt.Type = WorkloadEvent.EventType.Unknown;
                        continue;
                    }

                    try
                    {
                        evnt.ApplicationName = (string)TryGetValue(evt, FieldType.Action, "client_app_name");
                        evnt.DatabaseName    = (string)TryGetValue(evt, FieldType.Action, "database_name");
                        evnt.HostName        = (string)TryGetValue(evt, FieldType.Action, "client_hostname");
                        evnt.LoginName       = (string)TryGetValue(evt, FieldType.Action, "server_principal_name");
                        object oSession = TryGetValue(evt, FieldType.Action, "session_id");
                        if (oSession != null)
                        {
                            evnt.SPID = Convert.ToInt32(oSession);
                        }
                        if (commandText != null)
                        {
                            evnt.Text = commandText;
                        }


                        evnt.StartTime = evt.Timestamp.LocalDateTime;

                        if (evnt.Type == WorkloadEvent.EventType.Error)
                        {
                            // do nothing
                        }
                        else if (evnt.Type == WorkloadEvent.EventType.Timeout)
                        {
                            evnt.Duration = Convert.ToInt64(evt.Fields["duration"].Value);
                            evnt.CPU      = Convert.ToInt64(evnt.Duration);
                        }
                        else
                        {
                            evnt.Reads    = Convert.ToInt64(evt.Fields["logical_reads"].Value);
                            evnt.Writes   = Convert.ToInt64(evt.Fields["writes"].Value);
                            evnt.CPU      = Convert.ToInt64(evt.Fields["cpu_time"].Value);
                            evnt.Duration = Convert.ToInt64(evt.Fields["duration"].Value);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e, "Error converting XE data from the stream.");
                        throw;
                    }

                    if (evnt.Type <= WorkloadEvent.EventType.BatchCompleted)
                    {
                        if (transformer.Skip(evnt.Text))
                        {
                            continue;
                        }

                        evnt.Text = transformer.Transform(evnt.Text);
                    }

                    Events.Enqueue(evnt);

                    EventCount++;
                }
            }
        }
        public void ReadEvents()
        {
            EventCount = 0;
            SqlTransformer transformer = new SqlTransformer();

            using (QueryableXEventData eventstream = new QueryableXEventData(Path))
            {
                var eventsEnumerator = eventstream.GetEnumerator();

                while (!stopped && eventsEnumerator.MoveNext())
                {
                    PublishedEvent         evt  = eventsEnumerator.Current;
                    ExecutionWorkloadEvent evnt = new ExecutionWorkloadEvent();

                    string commandText = String.Empty;
                    if (evt.Name == "rpc_completed")
                    {
                        commandText = (string)TryGetValue(evt, FieldType.Field, "statement");
                        evnt.Type   = WorkloadEvent.EventType.RPCCompleted;
                    }
                    else if (evt.Name == "sql_batch_completed")
                    {
                        commandText = (string)TryGetValue(evt, FieldType.Field, "batch_text");
                        evnt.Type   = WorkloadEvent.EventType.BatchCompleted;
                    }
                    else if (evt.Name == "attention")
                    {
                        commandText = (string)TryGetValue(evt, FieldType.Action, "sql_text");
                        evnt.Type   = WorkloadEvent.EventType.Timeout;
                    }
                    else
                    {
                        evnt.Type = WorkloadEvent.EventType.Unknown;
                        continue;
                    }

                    try
                    {
                        evnt.ApplicationName = (string)TryGetValue(evt, FieldType.Action, "client_app_name");
                        evnt.DatabaseName    = (string)TryGetValue(evt, FieldType.Action, "database_name");
                        evnt.HostName        = (string)TryGetValue(evt, FieldType.Action, "client_hostname");
                        evnt.LoginName       = (string)TryGetValue(evt, FieldType.Action, "server_principal_name");
                        object oSession = TryGetValue(evt, FieldType.Action, "session_id");
                        if (oSession != null)
                        {
                            evnt.SPID = Convert.ToInt32(oSession);
                        }
                        if (commandText != null)
                        {
                            evnt.Text = commandText;
                        }


                        evnt.StartTime = evt.Timestamp.LocalDateTime;

                        if (evnt.Type == WorkloadEvent.EventType.Timeout)
                        {
                            evnt.Duration = Convert.ToInt64(evt.Fields["duration"].Value);
                            evnt.CPU      = Convert.ToInt64(evnt.Duration);
                        }
                        else
                        {
                            evnt.Reads    = Convert.ToInt64(evt.Fields["logical_reads"].Value);
                            evnt.Writes   = Convert.ToInt64(evt.Fields["writes"].Value);
                            evnt.CPU      = Convert.ToInt64(evt.Fields["cpu_time"].Value);
                            evnt.Duration = Convert.ToInt64(evt.Fields["duration"].Value);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e, "Error converting XE data from the stream.");
                        throw;
                    }

                    if (transformer.Skip(evnt.Text))
                    {
                        continue;
                    }

                    evnt.Text = transformer.Transform(evnt.Text);

                    Events.Enqueue(evnt);

                    EventCount++;
                }

                finished = true;
            }
        }
        public override void ReadEvents()
        {
            EventCount = 0;
            SqlTransformer transformer = new SqlTransformer();

            using (QueryableXEventData eventstream = new QueryableXEventData(
                       ConnectionString,
                       SessionName,
                       EventStreamSourceOptions.EventStream,
                       EventStreamCacheOptions.CacheToDisk))
            {
                var eventsEnumerator = eventstream.GetEnumerator();

                while (!stopped && eventsEnumerator.MoveNext())
                {
                    PublishedEvent         evt           = eventsEnumerator.Current;
                    ExecutionWorkloadEvent workloadEvent = new ExecutionWorkloadEvent();
                    try
                    {
                        workloadEvent.EventSequence = Convert.ToInt64(TryGetValue(evt, FieldType.Action, "event_sequence"));
                        string commandText = String.Empty;
                        if (evt.Name == "rpc_completed")
                        {
                            commandText        = (string)TryGetValue(evt, FieldType.Field, "statement");
                            workloadEvent.Type = WorkloadEvent.EventType.RPCCompleted;
                        }
                        else if (evt.Name == "sql_batch_completed")
                        {
                            commandText        = (string)TryGetValue(evt, FieldType.Field, "batch_text");
                            workloadEvent.Type = WorkloadEvent.EventType.BatchCompleted;
                        }
                        else if (evt.Name == "attention")
                        {
                            workloadEvent = new ErrorWorkloadEvent();
                            object value = TryGetValue(evt, FieldType.Action, "sql_text");

                            if (value == null)
                            {
                                continue;
                            }

                            try
                            {
                                if (value is string)
                                {
                                    commandText = (string)value;
                                }
                                else if (value is byte[])
                                {
                                    commandText = Encoding.Unicode.GetString((byte[])value);
                                }
                                else
                                {
                                    throw new ArgumentException("Argument is of the wrong type");
                                }
                            }
                            catch (Exception e)
                            {
                                logger.Error(e, $"Unable to extract sql_text from attention event. Value is of type ${value.GetType().FullName}");
                            }
                            workloadEvent.Text = commandText;
                            workloadEvent.Type = WorkloadEvent.EventType.Timeout;
                        }
                        else if (evt.Name == "user_event")
                        {
                            workloadEvent = new ErrorWorkloadEvent();
                            int num = (int)TryGetValue(evt, FieldType.Field, "event_id");
                            if (num == 83 || num == 82)
                            {
                                if (TryGetString(evt, FieldType.Field, "user_info").StartsWith("WorkloadTools."))
                                {
                                    commandText        = TryGetString(evt, FieldType.Field, "user_data");
                                    workloadEvent.Text = commandText;

                                    if (num == 83)
                                    {
                                        workloadEvent.Type = WorkloadEvent.EventType.Error;
                                    }
                                    else
                                    {
                                        workloadEvent.Type = WorkloadEvent.EventType.Timeout;
                                    }
                                }
                                else
                                {
                                    workloadEvent.Type = WorkloadEvent.EventType.Unknown;
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            workloadEvent.Type = WorkloadEvent.EventType.Unknown;
                            continue;
                        }

                        try
                        {
                            workloadEvent.ApplicationName = TryGetString(evt, FieldType.Action, "client_app_name");
                            workloadEvent.DatabaseName    = TryGetString(evt, FieldType.Action, "database_name");
                            workloadEvent.HostName        = TryGetString(evt, FieldType.Action, "client_hostname");
                            workloadEvent.LoginName       = TryGetString(evt, FieldType.Action, "server_principal_name");
                            workloadEvent.SPID            = TryGetInt32(evt, FieldType.Action, "session_id");
                            if (commandText != null)
                            {
                                workloadEvent.Text = commandText;
                            }


                            workloadEvent.StartTime = evt.Timestamp.LocalDateTime;

                            if (workloadEvent.Type == WorkloadEvent.EventType.Error)
                            {
                                workloadEvent.Duration = 0;
                                workloadEvent.CPU      = 0;
                            }
                            else if (workloadEvent.Type == WorkloadEvent.EventType.Timeout)
                            {
                                workloadEvent.Duration = TryGetInt64(evt, FieldType.Field, "duration");
                                workloadEvent.CPU      = Convert.ToInt64(workloadEvent.Duration);
                            }
                            else
                            {
                                workloadEvent.Reads    = TryGetInt64(evt, FieldType.Field, "logical_reads");
                                workloadEvent.Writes   = TryGetInt64(evt, FieldType.Field, "writes");
                                workloadEvent.CPU      = TryGetInt64(evt, FieldType.Field, "cpu_time");
                                workloadEvent.Duration = TryGetInt64(evt, FieldType.Field, "duration");
                            }
                        }
                        catch (Exception e)
                        {
                            logger.Error(e, "Error converting XE data from the stream.");
                            throw;
                        }

                        if (workloadEvent.Type <= WorkloadEvent.EventType.BatchCompleted)
                        {
                            if (transformer.Skip(workloadEvent.Text))
                            {
                                continue;
                            }

                            workloadEvent.Text = transformer.Transform(workloadEvent.Text);
                        }

                        Events.Enqueue(workloadEvent);

                        EventCount++;
                    }
                    catch (Exception ex)
                    {
                        logger.Error($"Error converting XE data from the stream: {ex.Message}");
                        try
                        {
                            logger.Error($"    event type            : {workloadEvent.Type}");
                            logger.Error($"    client_app_name       : {TryGetString(evt, FieldType.Action, "client_app_name")}");
                            logger.Error($"    database_name         : {TryGetString(evt, FieldType.Action, "database_name")}");
                            logger.Error($"    client_hostname       : {TryGetString(evt, FieldType.Action, "client_hostname")}");
                            logger.Error($"    server_principal_name : {TryGetString(evt, FieldType.Action, "server_principal_name")}");
                            logger.Error($"    session_id            : {TryGetString(evt, FieldType.Action, "session_id")}");
                            logger.Error($"    duration              : {TryGetString(evt, FieldType.Field, "duration")}");
                            logger.Error($"    logical_reads         : {TryGetString(evt, FieldType.Field, "logical_reads")}");
                            logger.Error($"    writes                : {TryGetString(evt, FieldType.Field, "writes")}");
                            logger.Error($"    cpu_time              : {TryGetString(evt, FieldType.Field, "cpu_time")}");
                        }
                        catch (Exception)
                        {
                            //ignore, it is only logging
                        }
                        throw;
                    }
                }
            }
        }