DefaultMessageProducer.java
7.61 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.dianping.cat.message.internal;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.ForkedTransaction;
import com.dianping.cat.message.Heartbeat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.MessageProducer;
import com.dianping.cat.message.Metric;
import com.dianping.cat.message.TaggedTransaction;
import com.dianping.cat.message.Trace;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageManager;
import com.dianping.cat.message.spi.MessageTree;
public class DefaultMessageProducer implements MessageProducer {
@Inject
private MessageManager m_manager;
@Inject
private MessageIdFactory m_factory;
@Override
public String createMessageId() {
return m_factory.getNextId();
}
@Override
public boolean isEnabled() {
return m_manager.isMessageEnabled();
}
@Override
public void logError(String message, Throwable cause) {
if (Cat.getManager().isCatEnabled()) {
if (shouldLog(cause)) {
m_manager.getThreadLocalMessageTree().setSample(false);
StringWriter writer = new StringWriter(2048);
if (message != null) {
writer.write(message);
writer.write(' ');
}
cause.printStackTrace(new PrintWriter(writer));
String detailMessage = writer.toString();
if (cause instanceof Error) {
logEvent("Error", cause.getClass().getName(), "ERROR", detailMessage);
} else if (cause instanceof RuntimeException) {
logEvent("RuntimeException", cause.getClass().getName(), "ERROR", detailMessage);
} else {
logEvent("Exception", cause.getClass().getName(), "ERROR", detailMessage);
}
}
} else {
cause.printStackTrace();
}
}
@Override
public void logError(Throwable cause) {
logError(null, cause);
}
@Override
public void logEvent(String type, String name) {
logEvent(type, name, Message.SUCCESS, null);
}
@Override
public void logEvent(String type, String name, String status, String nameValuePairs) {
Event event = newEvent(type, name);
if (nameValuePairs != null && nameValuePairs.length() > 0) {
event.addData(nameValuePairs);
}
event.setStatus(status);
event.complete();
}
@Override
public void logHeartbeat(String type, String name, String status, String nameValuePairs) {
Heartbeat heartbeat = newHeartbeat(type, name);
heartbeat.addData(nameValuePairs);
heartbeat.setStatus(status);
heartbeat.complete();
}
@Override
public void logMetric(String name, String status, String nameValuePairs) {
String type = "";
Metric metric = newMetric(type, name);
if (nameValuePairs != null && nameValuePairs.length() > 0) {
metric.addData(nameValuePairs);
}
metric.setStatus(status);
metric.complete();
}
@Override
public void logTrace(String type, String name) {
logTrace(type, name, Message.SUCCESS, null);
}
@Override
public void logTrace(String type, String name, String status, String nameValuePairs) {
if (m_manager.isTraceMode()) {
Trace trace = newTrace(type, name);
if (nameValuePairs != null && nameValuePairs.length() > 0) {
trace.addData(nameValuePairs);
}
trace.setStatus(status);
trace.complete();
}
}
@Override
public Event newEvent(String type, String name) {
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
DefaultEvent event = new DefaultEvent(type, name, m_manager);
return event;
} else {
return NullMessage.EVENT;
}
}
public Event newEvent(Transaction parent, String type, String name) {
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled() && parent != null) {
DefaultEvent event = new DefaultEvent(type, name);
parent.addChild(event);
return event;
} else {
return NullMessage.EVENT;
}
}
@Override
public ForkedTransaction newForkedTransaction(String type, String name) {
// this enable CAT client logging cat message without explicit setup
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
MessageTree tree = m_manager.getThreadLocalMessageTree();
if (tree.getMessageId() == null) {
tree.setMessageId(createMessageId());
}
DefaultForkedTransaction transaction = new DefaultForkedTransaction(type, name, m_manager);
if (m_manager instanceof DefaultMessageManager) {
((DefaultMessageManager) m_manager).linkAsRunAway(transaction);
}
m_manager.start(transaction, true);
return transaction;
} else {
return NullMessage.TRANSACTION;
}
}
@Override
public Heartbeat newHeartbeat(String type, String name) {
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
DefaultHeartbeat heartbeat = new DefaultHeartbeat(type, name, m_manager);
m_manager.getThreadLocalMessageTree().setSample(false);
return heartbeat;
} else {
return NullMessage.HEARTBEAT;
}
}
@Override
public Metric newMetric(String type, String name) {
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
DefaultMetric metric = new DefaultMetric(type == null ? "" : type, name, m_manager);
m_manager.getThreadLocalMessageTree().setSample(false);
return metric;
} else {
return NullMessage.METRIC;
}
}
@Override
public TaggedTransaction newTaggedTransaction(String type, String name, String tag) {
// this enable CAT client logging cat message without explicit setup
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
MessageTree tree = m_manager.getThreadLocalMessageTree();
if (tree.getMessageId() == null) {
tree.setMessageId(createMessageId());
}
DefaultTaggedTransaction transaction = new DefaultTaggedTransaction(type, name, tag, m_manager);
m_manager.start(transaction, true);
return transaction;
} else {
return NullMessage.TRANSACTION;
}
}
@Override
public Trace newTrace(String type, String name) {
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
DefaultTrace trace = new DefaultTrace(type, name, m_manager);
return trace;
} else {
return NullMessage.TRACE;
}
}
@Override
public Transaction newTransaction(String type, String name) {
// this enable CAT client logging cat message without explicit setup
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled()) {
DefaultTransaction transaction = new DefaultTransaction(type, name, m_manager);
m_manager.start(transaction, false);
return transaction;
} else {
return NullMessage.TRANSACTION;
}
}
public Transaction newTransaction(Transaction parent, String type, String name) {
// this enable CAT client logging cat message without explicit setup
if (!m_manager.hasContext()) {
m_manager.setup();
}
if (m_manager.isMessageEnabled() && parent != null) {
DefaultTransaction transaction = new DefaultTransaction(type, name, m_manager);
parent.addChild(transaction);
transaction.setStandalone(false);
return transaction;
} else {
return NullMessage.TRANSACTION;
}
}
private boolean shouldLog(Throwable e) {
if (m_manager instanceof DefaultMessageManager) {
return ((DefaultMessageManager) m_manager).shouldLog(e);
} else {
return true;
}
}
}