Пример #1
0
        private static void SimpleTest()
        {
            var startTime = DateTime.Now;

            Console.WriteLine("Start: " + startTime);
            Console.WriteLine($"Top ThreadId: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
            ITransaction newOrderTransaction = null;

            try
            {
                newOrderTransaction = Cat.NewTransaction("SimpleTestAsync-3-" + DateTime.Now.Ticks, "NewTrainOrder");

                newOrderTransaction.AddData("I am a detailed message");
                newOrderTransaction.AddData("another message");

                newOrderTransaction.Status = CatConstants.SUCCESS;
            }
            catch (Exception ex)
            {
                newOrderTransaction.SetStatus(ex);
            }
            finally
            {
                Console.WriteLine(newOrderTransaction.DurationInMillis);
                newOrderTransaction.Complete();
                Console.WriteLine("End: " + DateTime.Now);
                Console.WriteLine($"Duration: {(DateTime.Now - startTime).TotalMilliseconds}");
            }
        }
Пример #2
0
        private static async Task SimpleTest()
        {
            var startTime = DateTime.Now;

            Console.WriteLine("Start: " + startTime);
            Console.WriteLine($"Top ThreadId: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
            ITransaction newOrderTransaction = null;

            try
            {
                newOrderTransaction = Cat.NewTransaction("SimpleTestAsync-3-" + DateTime.Now.Ticks, "NewTrainOrder");
                asyncLocal.Value    = new Context {
                    Value = -8
                };

                newOrderTransaction.AddData("I am a detailed message");
                newOrderTransaction.AddData("another message");

                Cat.LogError("MyException", new Exception("My Exception"));

                var tasks = Enumerable.Range(1, 2).Select(async(i) =>
                {
                    await InvokePaymentWrap(i).ConfigureAwait(false);
                });

                await Task.WhenAll(tasks);

                Console.WriteLine("//////////////////////////////");

                for (int i = 100; i < 103; i++)
                {
                    await InvokePayment(i);
                }


                newOrderTransaction.Status = CatConstants.SUCCESS;
            }
            catch (Exception ex)
            {
                newOrderTransaction.SetStatus(ex);
            }
            finally
            {
                Console.WriteLine(newOrderTransaction.DurationInMillis);
                newOrderTransaction.Complete();
                Console.WriteLine("End: " + DateTime.Now);
                Console.WriteLine($"Duration: {(DateTime.Now - startTime).TotalMilliseconds}");
            }
        }
Пример #3
0
        private static void SimpleTest()
        {
            Console.WriteLine("Start: " + DateTime.Now);
            ITransaction newOrderTransaction = null;
            ITransaction paymentTransaction  = null;

            try
            {
                newOrderTransaction = Cat.NewTransaction("SimpleTest", "NewTrainOrder");
                newOrderTransaction.AddData("I am a detailed message");
                newOrderTransaction.AddData("another message");
                Cat.LogEvent("TrainNo", "123456");
                Cat.LogError("MyException", new Exception("My Exception"));

                try
                {
                    paymentTransaction        = Cat.NewTransaction("NewPayment", "PaymentDetail");
                    paymentTransaction.Status = CatConstants.SUCCESS;
                }
                catch (Exception ex)
                {
                    paymentTransaction.SetStatus(ex);
                }
                finally
                {
                    paymentTransaction.Complete();
                }

                newOrderTransaction.Status = CatConstants.SUCCESS;
            }
            catch (Exception ex)
            {
                newOrderTransaction.SetStatus(ex);
            }
            finally
            {
                newOrderTransaction.Complete();
                Console.WriteLine("End: " + DateTime.Now);
            }
        }
Пример #4
0
        private static void CheckExactSizeTest()
        {
            ITransaction root = Cat.NewTransaction("a", "a");

            root.AddData("aaa");
            //Cat.LogEvent("a", "a",CatConstants.SUCCESS, "aaaa");
            //IDictionary<string, string> indexedTags = new Dictionary<string, string>();
            //indexedTags["aaaa"] = "bbbbbbb";
            //Cat.LogTags("a", indexedTags, null);

            root.Status = CatConstants.SUCCESS;
            var tree = Cat.GetThreadLocalMessageTree();

            Console.WriteLine("Estimated tree byte size: " + tree.EstimatedByteSize);
            root.Complete();

            Thread.Sleep(100);
            Console.WriteLine("Estimated tree byte size: " + tree.EstimatedByteSize);
            Thread.Sleep(100);
        }
Пример #5
0
        public void Run(object o)
        {
            try
            {
                // Get DLL versions
                collectComponentVersions();
                Random random       = new Random();
                int    initialSleep = random.Next(CatConstants.HEARTBEAT_MIN_INITIAL_SLEEP_MILLISECONDS, CatConstants.HEARTBEAT_MAX_INITIAL_SLEEP_MILLISECONDS);
                Console.WriteLine("Heartbeat initial sleep: " + initialSleep + " ms");
                Thread.Sleep(initialSleep);
            }
            catch (Exception ex) { Cat.lastException = ex; return; }

            // In Java, try to avoid send heartbeat at 59-01 second, which is missing here in .NET

            // In Java, try to build class paths, which is a list of jar file names.
            try
            {
                ITransaction reboot = Cat.NewTransaction("System", "Reboot");
                reboot.Status = CatConstants.SUCCESS;
                Cat.LogEvent("Reboot", NetworkInterfaceManager.HostIP, CatConstants.SUCCESS);
                reboot.Complete();

                DateTime lastSendVersionsTimestamp = default(DateTime);
                while (true)
                {
                    ITransaction t = Cat.NewTransaction("System", "Status");
                    t.AddData(CatConstants.DUMP_LOCKED, false);
                    IHeartbeat h = Cat.NewHeartbeat("Heartbeat", NetworkInterfaceManager.HostIP);
                    try
                    {
                        var  now = DateTime.Now;
                        bool isLongIntevalHeartbeat = false;
                        if (default(DateTime) == lastSendVersionsTimestamp ||
                            now.Hour != lastSendVersionsTimestamp.Hour ||
                            (now - lastSendVersionsTimestamp >= TimeSpan.FromMinutes(LONG_INTERVAL_HEARBEAT_MINUTES)))
                        {
                            isLongIntevalHeartbeat    = true;
                            lastSendVersionsTimestamp = now;
                        }
                        h.AddData(BuildStatusData(isLongIntevalHeartbeat));
                        h.Status = CatConstants.SUCCESS;

                        if (isLongIntevalHeartbeat)
                        {
                            string configHeartBeatMessage = config.GetConfigHeartbeatMessage();
                            if (!String.IsNullOrWhiteSpace(configHeartBeatMessage))
                            {
                                Cat.LogEvent("Cat.Client.InconsistentAppId", configHeartBeatMessage);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Cat.lastException = ex;
                        h.SetStatus(ex);
                        Cat.LogError(ex);
                    }
                    finally
                    {
                        h.Complete();
                    }

                    if (!String.IsNullOrEmpty(fileVersion))
                    {
                        Cat.LogEvent("Cat.Client.Version", fileVersion);
                    }

                    t.Status = CatConstants.SUCCESS;
                    t.Complete();

                    // Append Cat.lastException if not null
                    if (null != Cat.lastException)
                    {
                        Exception ex = Cat.lastException;
                        Cat.lastException = null;
                        Cat.LogEvent("Cat.Client.LastException", ex.GetType().Name, CatConstants.SUCCESS, ex.ToString());
                    }

                    // Append Cat.lastMessage if not null
                    if (!String.IsNullOrWhiteSpace(Cat.lastMessage))
                    {
                        Cat.LogEvent("Cat.Client.LastMessage", "msg", CatConstants.SUCCESS, "message=" + Cat.lastMessage);
                        Cat.lastMessage = null;
                    }

                    // Sleep to the 30th second of the next minute, not to the 30th second of the current minute.
                    var sleepInSeconds = 90 - DateTime.Now.Second;
                    Thread.Sleep(sleepInSeconds * 1000);
                }
            }
            catch (Exception ex) { Cat.lastException = ex; }
        }
Пример #6
0
        protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent,
                                               Stack<ITransaction> stack, IMessageTree tree)
        {
            BufferHelper helper = _mBufferHelper;
            byte identifier = buf.ReadByte();
            String timestamp = helper.Read(buf, TAB);
            String type = helper.Read(buf, TAB);
            String name = helper.Read(buf, TAB);

            if (identifier == 'E')
            {
                IMessage evt = new DefaultEvent(type, name);
                String status = helper.Read(buf, TAB);
                String data = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                evt.Timestamp = _mDateHelper.Parse(timestamp);
                evt.Status = status;
                evt.AddData(data);

                if (parent != null)
                {
                    parent.AddChild(evt);
                    tree.EstimatedByteSize += evt.EstimateByteSize();
                    return parent;
                }
                return evt;
            }
            if (identifier == 'M')
            {
                DefaultMetric metric = new DefaultMetric(type, name);
                String status = helper.Read(buf, TAB);
                String data = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                metric.Timestamp = _mDateHelper.Parse(timestamp);
                metric.Status = status;
                metric.AddData(data);

                if (parent != null)
                {
                    parent.AddChild(metric);
                    tree.EstimatedByteSize += metric.EstimateByteSize();
                    return parent;
                }
                return metric;
            }
            if (identifier == 'H')
            {
                IMessage heartbeat = new DefaultHeartbeat(type, name);
                String status0 = helper.Read(buf, TAB);
                String data1 = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                heartbeat.Timestamp = _mDateHelper.Parse(timestamp);
                heartbeat.Status = status0;
                heartbeat.AddData(data1);

                if (parent != null)
                {
                    parent.AddChild(heartbeat);
                    tree.EstimatedByteSize += heartbeat.EstimateByteSize();
                    return parent;
                }
                return heartbeat;
            }
            if (identifier == 't')
            {
                IMessage transaction = new DefaultTransaction(type, name,
                                                              null);

                helper.Read(buf, LF); // get rid of line feed
                transaction.Timestamp = _mDateHelper.Parse(timestamp);

                if (parent != null)
                {
                    parent.AddChild(transaction);
                }

                stack.Push(parent);
                return transaction;
            }
            if (identifier == 'A')
            {
                ITransaction transaction2 = new DefaultTransaction(type, name, null);
                String status3 = helper.Read(buf, TAB);
                String duration = helper.Read(buf, TAB);
                String data4 = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                transaction2.Timestamp = _mDateHelper.Parse(timestamp);
                transaction2.Status = status3;
                transaction2.AddData(data4);

                long d = Int64.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer);
                transaction2.DurationInMicros = d;

                if (parent != null)
                {
                    parent.AddChild(transaction2);
                    tree.EstimatedByteSize += transaction2.EstimateByteSize();
                    return parent;
                }
                return transaction2;
            }
            if (identifier == 'T')
            {
                String status5 = helper.Read(buf, TAB);
                String duration6 = helper.Read(buf, TAB);
                String data7 = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                parent.Status = status5;
                parent.AddData(data7);

                long d8 = Int64.Parse(
                    duration6.Substring(0, duration6.Length - 2),
                    NumberStyles.Integer);
                parent.DurationInMicros = d8;
                tree.EstimatedByteSize += parent.EstimateByteSize();
                return stack.Pop();
            }
            Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf);

            // unknown message, ignore it
            return parent;
        }
Пример #7
0
        protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent,
                                               Stack<ITransaction> stack, IMessageTree tree)
        {
            BufferHelper helper = _mBufferHelper;
            char identifier = (char)buf.ReadByte();
            string timestamp = helper.Read(buf, TAB);
            string type = helper.Read(buf, TAB);
            string name = helper.Read(buf, TAB);
            switch (identifier)
            {
                case 't':
                    IMessage transaction = new DefaultTransaction(type, name, null);

                    helper.Read(buf, LF); // get rid of line feed
                    transaction.Timestamp = _mDateHelper.Parse(timestamp);

                    if (parent != null)
                    {
                        parent.AddChild(transaction);
                    }

                    stack.Push(parent);
                    return transaction;
                case 'A':
                    DefaultTransaction tran = new DefaultTransaction(type, name, null);
                    string status = helper.Read(buf, TAB);
                    string duration = helper.Read(buf, TAB);
                    string data = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    tran.Timestamp = _mDateHelper.Parse(timestamp);
                    tran.Status = status;
                    tran.AddData(data);

                    long d = long.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer);

                    tran.DurationInMicros = d;

                    if (parent != null)
                    {
                        parent.AddChild(tran);
                        return parent;
                    }
                    return tran;
                case 'T':
                    string transactionStatus = helper.Read(buf, TAB);
                    string transactionDuration = helper.Read(buf, TAB);
                    string transactionData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    parent.Status = transactionStatus;
                    parent.AddData(transactionData);

                    long transactionD = long.Parse(transactionDuration.Substring(0, transactionDuration.Length - 2), NumberStyles.Integer);

                    parent.DurationInMicros = transactionD;

                    return stack.Pop();
                case 'E':
                    DefaultEvent evt = new DefaultEvent(type, name);
                    string eventStatus = helper.Read(buf, TAB);
                    string eventData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    evt.Timestamp = _mDateHelper.Parse(timestamp);
                    evt.Status = eventStatus;
                    evt.AddData(eventData);

                    if (parent != null)
                    {
                        parent.AddChild(evt);
                        return parent;
                    }
                    return evt;
                case 'M':
                    DefaultMetric metric = new DefaultMetric(type, name);
                    string metricStatus = helper.Read(buf, TAB);
                    string metricData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF);
                    metric.Timestamp = _mDateHelper.Parse(timestamp);
                    metric.Status = metricStatus;
                    metric.AddData(metricData);

                    if (parent != null)
                    {
                        parent.AddChild(metric);
                        return parent;
                    }
                    return metric;
                case 'L':
                    DefaultTrace trace = new DefaultTrace(type, name);
                    string traceStatus = helper.Read(buf, TAB);
                    string traceData = helper.Read(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    trace.Timestamp = _mDateHelper.Parse(timestamp);
                    trace.Status = traceStatus;
                    trace.AddData(traceData);

                    if (parent != null)
                    {
                        parent.AddChild(trace);
                        return parent;
                    }
                    return trace;
                case 'H':
                    DefaultHeartbeat heartbeat = new DefaultHeartbeat(type, name);
                    string heartbeatStatus = helper.Read(buf, TAB);
                    string heartbeatData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    heartbeat.Timestamp = _mDateHelper.Parse(timestamp);
                    heartbeat.Status = heartbeatStatus;
                    heartbeat.AddData(heartbeatData);

                    if (parent != null)
                    {
                        parent.AddChild(heartbeat);
                        return parent;
                    }
                    return heartbeat;
            }

            Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf);
            //throw new Exception("Unknown identifier int name"); //java版的抛出异常

            // unknown message, ignore it
            return parent;
        }
Пример #8
0
        protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent,
                                               Stack <ITransaction> stack, IMessageTree tree)
        {
            BufferHelper helper     = _mBufferHelper;
            byte         identifier = buf.ReadByte();
            String       timestamp  = helper.Read(buf, TAB);
            String       type       = helper.Read(buf, TAB);
            String       name       = helper.Read(buf, TAB);

            if (identifier == 'E')
            {
                IMessage evt    = new DefaultEvent(type, name);
                String   status = helper.Read(buf, TAB);
                String   data   = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                evt.Timestamp = _mDateHelper.Parse(timestamp);
                evt.Status    = status;
                evt.AddData(data);

                if (parent != null)
                {
                    parent.AddChild(evt);
                    tree.EstimatedByteSize += evt.EstimateByteSize();
                    return(parent);
                }
                return(evt);
            }
            if (identifier == 'M')
            {
                DefaultMetric metric = new DefaultMetric(type, name);
                String        status = helper.Read(buf, TAB);
                String        data   = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                metric.Timestamp = _mDateHelper.Parse(timestamp);
                metric.Status    = status;
                metric.AddData(data);

                if (parent != null)
                {
                    parent.AddChild(metric);
                    tree.EstimatedByteSize += metric.EstimateByteSize();
                    return(parent);
                }
                return(metric);
            }
            if (identifier == 'H')
            {
                IMessage heartbeat = new DefaultHeartbeat(type, name);
                String   status0   = helper.Read(buf, TAB);
                String   data1     = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                heartbeat.Timestamp = _mDateHelper.Parse(timestamp);
                heartbeat.Status    = status0;
                heartbeat.AddData(data1);

                if (parent != null)
                {
                    parent.AddChild(heartbeat);
                    tree.EstimatedByteSize += heartbeat.EstimateByteSize();
                    return(parent);
                }
                return(heartbeat);
            }
            if (identifier == 't')
            {
                IMessage transaction = new DefaultTransaction(type, name,
                                                              null);

                helper.Read(buf, LF); // get rid of line feed
                transaction.Timestamp = _mDateHelper.Parse(timestamp);

                if (parent != null)
                {
                    parent.AddChild(transaction);
                    tree.EstimatedByteSize += transaction.EstimateByteSize();
                }

                stack.Push(parent);
                return(transaction);
            }
            if (identifier == 'A')
            {
                ITransaction transaction2 = new DefaultTransaction(type, name, null);
                String       status3      = helper.Read(buf, TAB);
                String       duration     = helper.Read(buf, TAB);
                String       data4        = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                transaction2.Timestamp = _mDateHelper.Parse(timestamp);
                transaction2.Status    = status3;
                transaction2.AddData(data4);

                long d = Int64.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer);
                transaction2.DurationInMicros = d;

                if (parent != null)
                {
                    parent.AddChild(transaction2);
                    tree.EstimatedByteSize += transaction2.EstimateByteSize();
                    return(parent);
                }
                return(transaction2);
            }
            if (identifier == 'T')
            {
                String status5   = helper.Read(buf, TAB);
                String duration6 = helper.Read(buf, TAB);
                String data7     = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                parent.Status = status5;
                parent.AddData(data7);

                long d8 = Int64.Parse(
                    duration6.Substring(0, duration6.Length - 2),
                    NumberStyles.Integer);
                parent.DurationInMicros = d8;

                return(stack.Pop());
            }
            Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf);

            // unknown message, ignore it
            return(parent);
        }
Пример #9
0
        protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent,
                                               Stack <ITransaction> stack, IMessageTree tree)
        {
            BufferHelper helper     = _mBufferHelper;
            char         identifier = (char)buf.ReadByte();
            string       timestamp  = helper.Read(buf, TAB);
            string       type       = helper.Read(buf, TAB);
            string       name       = helper.Read(buf, TAB);

            switch (identifier)
            {
            case 't':
                IMessage transaction = new DefaultTransaction(type, name, null);

                helper.Read(buf, LF);     // get rid of line feed
                transaction.Timestamp = _mDateHelper.Parse(timestamp);

                if (parent != null)
                {
                    parent.AddChild(transaction);
                }

                stack.Push(parent);
                return(transaction);

            case 'A':
                DefaultTransaction tran     = new DefaultTransaction(type, name, null);
                string             status   = helper.Read(buf, TAB);
                string             duration = helper.Read(buf, TAB);
                string             data     = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF);     // get rid of line feed
                tran.Timestamp = _mDateHelper.Parse(timestamp);
                tran.Status    = status;
                tran.AddData(data);

                long d = long.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer);

                tran.DurationInMicros = d;

                if (parent != null)
                {
                    parent.AddChild(tran);
                    return(parent);
                }
                return(tran);

            case 'T':
                string transactionStatus   = helper.Read(buf, TAB);
                string transactionDuration = helper.Read(buf, TAB);
                string transactionData     = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF);     // get rid of line feed
                parent.Status = transactionStatus;
                parent.AddData(transactionData);

                long transactionD = long.Parse(transactionDuration.Substring(0, transactionDuration.Length - 2), NumberStyles.Integer);

                parent.DurationInMicros = transactionD;

                return(stack.Pop());

            case 'E':
                DefaultEvent evt         = new DefaultEvent(type, name);
                string       eventStatus = helper.Read(buf, TAB);
                string       eventData   = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF);     // get rid of line feed
                evt.Timestamp = _mDateHelper.Parse(timestamp);
                evt.Status    = eventStatus;
                evt.AddData(eventData);

                if (parent != null)
                {
                    parent.AddChild(evt);
                    return(parent);
                }
                return(evt);

            case 'M':
                DefaultMetric metric       = new DefaultMetric(type, name);
                string        metricStatus = helper.Read(buf, TAB);
                string        metricData   = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF);
                metric.Timestamp = _mDateHelper.Parse(timestamp);
                metric.Status    = metricStatus;
                metric.AddData(metricData);

                if (parent != null)
                {
                    parent.AddChild(metric);
                    return(parent);
                }
                return(metric);

            case 'L':
                DefaultTrace trace       = new DefaultTrace(type, name);
                string       traceStatus = helper.Read(buf, TAB);
                string       traceData   = helper.Read(buf, TAB);

                helper.Read(buf, LF);     // get rid of line feed
                trace.Timestamp = _mDateHelper.Parse(timestamp);
                trace.Status    = traceStatus;
                trace.AddData(traceData);

                if (parent != null)
                {
                    parent.AddChild(trace);
                    return(parent);
                }
                return(trace);

            case 'H':
                DefaultHeartbeat heartbeat       = new DefaultHeartbeat(type, name);
                string           heartbeatStatus = helper.Read(buf, TAB);
                string           heartbeatData   = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF);     // get rid of line feed
                heartbeat.Timestamp = _mDateHelper.Parse(timestamp);
                heartbeat.Status    = heartbeatStatus;
                heartbeat.AddData(heartbeatData);

                if (parent != null)
                {
                    parent.AddChild(heartbeat);
                    return(parent);
                }
                return(heartbeat);
            }

            Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf);
            //throw new Exception("Unknown identifier int name"); //java版的抛出异常

            // unknown message, ignore it
            return(parent);
        }