1. 生产消息newTransaction,检查上下文ThreadLocal<Context>是否已经初始化,没有则通过m_manager.setup()初始化。初始化消息DefaultTransaction,转交给默认消息管理开始消息处理,m_manager.start(transaction, false);如果是该线程的第一个消息则保存在MessageTree
public void start(Transaction transaction, boolean forked) {
if (!m_stack.isEmpty()) {
// Do NOT make strong reference from parent transaction to forked transaction.
// Instead, we create a "soft" reference to forked transaction later, via linkAsRunAway()
// By doing so, there is no need for synchronization between parent and child threads.
// Both threads can complete() anytime despite the other thread.
Transaction parent = m_stack.peek();
addTransactionChild(transaction, parent);
} else {
m_tree.setMessage(transaction);
}
if (!forked) {
m_stack.push(transaction);
}
}
否则就和Stack中的栈顶消息比较生成时间,超过本时段结束时间10s即到达下一时段或者子消息的数量超过配置的最大值,这时深度复制最初的消息以及它的子消息,添加事件消息消息DefaultEvent,最后设置ctx.m_totalDurationInMicros为对应的生成时间,也就表示该值大于0。
public void truncateAndFlush(Context ctx, long timestamp) {
MessageTree tree = ctx.m_tree;
Stack<Transaction> stack = ctx.m_stack;
Message message = tree.getMessage();
if (message instanceof DefaultTransaction) {
String id = tree.getMessageId();
if (id == null) {
id = nextMessageId();
tree.setMessageId(id);
}
Stri