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; }
private IMessageTree MergeTree(BlockingThreadSafeQueue<IMessageTree> trees) { int max = MAX_ATOMIC_MESSAGES - 1; DefaultTransaction tran = new DefaultTransaction("_CatMergeTree", "_CatMergeTree", null); tran.Status = CatConstants.SUCCESS; tran.SetCompleted(true); IMessageTree first; trees.TryPeek(out first, false); // Usually this should not happen, because it is in the same thread with ShouldMerge() if (first == null) { return null; } // Set merge tree start time to the first massage's timestamp tran.Timestamp = first.Message.Timestamp; long lastTimestamp = 0; long lastDuration = 0; int estimatedByteSize = 0; while (max >= 0) { IMessageTree tree; trees.TryDequeue(out tree, false); if (tree != null) { tran.AddChild(tree.Message); estimatedByteSize += tree.EstimatedByteSize; if (first!=tree) { _messageIdFactory.Reuse(tree.MessageId); } lastTimestamp = tree.Message.Timestamp; if (tree.Message is DefaultTransaction) { lastDuration = ((DefaultTransaction)tree.Message).DurationInMillis; } else { lastDuration = 0; } } if (tree == null || max == 0) { // Set merge tree end time to the last massage's end time tran.DurationInMillis = (lastTimestamp - tran.Timestamp + lastDuration); break; } max--; } ((DefaultMessageTree) first).Message = tran; estimatedByteSize += tran.EstimateByteSize(); first.EstimatedByteSize = estimatedByteSize; return first; }
public void TruncateAndFlush(Context ctx, long timestamp) { IMessageTree tree = ctx.Tree; Stack<ITransaction> stack = ctx.Stack; IMessage message = tree.Message; if (message is DefaultTransaction) { string id = tree.MessageId; string rootId = tree.RootMessageId; string childId = _mManager.NextMessageId(); DefaultTransaction source = (DefaultTransaction)message; DefaultTransaction target = new DefaultTransaction(source.Type, source.Name, _mManager); target.Timestamp = source.Timestamp; target.DurationInMicros = source.DurationInMicros; target.AddData(source.Data); target.Status = CatConstants.SUCCESS; MigrateMessage(stack, source, target, 1); int reducedByteSize = 0; foreach (ITransaction transaction in stack) { DefaultTransaction tran = (DefaultTransaction)transaction; tran.Timestamp = timestamp; reducedByteSize += transaction.EstimateByteSize(); } DefaultEvent next = new DefaultEvent("RemoteCall", "Next"); next.AddData(childId); next.Status = CatConstants.SUCCESS; target.AddChild(next); IMessageTree t = tree.Copy(); t.Message = target; ctx.Tree.MessageId = childId; ctx.Tree.ParentMessageId = id; ctx.Tree.RootMessageId = (rootId != null ? rootId : id); ctx._mLength = stack.Count; // Update estimated byte size of the truncated tree to be the total size of all on-stack transactions. ctx.Tree.EstimatedByteSize = reducedByteSize; ctx._mTotalDurationInMicros = ctx._mTotalDurationInMicros + target.DurationInMicros; _mManager.Flush(t); } }
private void MigrateMessage(Stack<ITransaction> stack, ITransaction source, ITransaction target, int level) { // Note that stack.ToArray() gives an array reversed, which is the opposite of Java. ITransaction[] onStackTransactions = stack.ToArray(); ITransaction current = (level < stack.Count ? onStackTransactions[stack.Count - 1 - level] : null); bool shouldKeep = false; foreach (IMessage child in source.Children) { if (child != current) { target.AddChild(child); } else { DefaultTransaction cloned = new DefaultTransaction(current.Type, current.Name, _mManager); cloned.Timestamp = current.Timestamp; cloned.DurationInMicros = current.DurationInMicros; cloned.AddData(current.Data); cloned.Status = CatConstants.SUCCESS; target.AddChild(cloned); MigrateMessage(stack, current, cloned, level + 1); shouldKeep = true; } } source.Children.Clear(); if (shouldKeep) { source.AddChild(current); } }
public void markAsNotCompleted(DefaultTransaction transaction) { DefaultEvent evt = new DefaultEvent("cat", "BadInstrument"); evt.Status = "TransactionNotCompleted"; evt.SetCompleted(true); transaction.AddChild(evt); transaction.SetCompleted(true); }
public virtual ITransaction NewTransaction(String type, String name) { // this enable CAT client logging cat message without explicit setup if (!_mManager.HasContext()) { _mManager.Setup(); } if (_mManager.CatEnabled) { ITransaction transaction = new DefaultTransaction(type, name, _mManager); _mManager.Start(transaction, false); return transaction; } return new NullTransaction(); }