DefaultTransaction.java
3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.dianping.cat.message.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageManager;
public class DefaultTransaction extends AbstractMessage implements Transaction {
private long m_durationInMicro = -1; // must be less than 0
private List<Message> m_children;
private MessageManager m_manager;
private boolean m_standalone;
private long m_durationStart;
public DefaultTransaction(String type, String name, MessageManager manager) {
super(type, name);
m_manager = manager;
m_standalone = true;
m_durationStart = System.nanoTime();
}
@Override
public DefaultTransaction addChild(Message message) {
if (m_children == null) {
m_children = new ArrayList<Message>();
}
if (message != null) {
m_children.add(message);
} else {
Cat.logError(new Exception("null child message"));
}
return this;
}
@Override
public void complete() {
try {
if (isCompleted()) {
// complete() was called more than once
DefaultEvent event = new DefaultEvent("cat", "BadInstrument");
event.setStatus("TransactionAlreadyCompleted");
event.complete();
addChild(event);
} else {
m_durationInMicro = (System.nanoTime() - m_durationStart) / 1000L;
setCompleted(true);
if (m_manager != null) {
m_manager.end(this);
}
}
} catch (Exception e) {
// ignore
}
}
@Override
public List<Message> getChildren() {
if (m_children == null) {
return Collections.emptyList();
}
return m_children;
}
@Override
public long getDurationInMicros() {
if (m_durationInMicro >= 0) {
return m_durationInMicro;
} else { // if it's not completed explicitly
long duration = 0;
int len = m_children == null ? 0 : m_children.size();
if (len > 0) {
Message lastChild = m_children.get(len - 1);
if (lastChild instanceof Transaction) {
DefaultTransaction trx = (DefaultTransaction) lastChild;
duration = (trx.getTimestamp() - getTimestamp()) * 1000L;
} else {
duration = (lastChild.getTimestamp() - getTimestamp()) * 1000L;
}
}
return duration;
}
}
@Override
public long getDurationInMillis() {
return getDurationInMicros() / 1000L;
}
protected MessageManager getManager() {
return m_manager;
}
@Override
public boolean hasChildren() {
return m_children != null && m_children.size() > 0;
}
@Override
public boolean isStandalone() {
return m_standalone;
}
public void setDurationInMicros(long duration) {
m_durationInMicro = duration;
}
public void setDurationInMillis(long duration) {
m_durationInMicro = duration * 1000L;
}
public void setStandalone(boolean standalone) {
m_standalone = standalone;
}
public void setDurationStart(long durationStart) {
m_durationStart = durationStart;
}
}