提交 0f55fc14 编写于 作者: Z zhangxin10

1.增加打印埋点信息和是否发送的配置

2.修改节点层次的实现
上级 71063f08
......@@ -10,5 +10,9 @@ buffer.pool_max_length=5
sender.max_buffer_data_size=1000
#最大发送者的连接数阀比例
sender.send_connection_threshold=1
#是否打印数据
buriedpoint.printf=true
#是否开启发送消息
sender.off=true
#发送服务端配置
sender.sender_servers=127.0.0.1:34000
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.buriedpoint;
import com.ai.cloud.skywalking.api.IBuriedPointSender;
import com.ai.cloud.skywalking.buffer.ContextBuffer;
import com.ai.cloud.skywalking.conf.Config;
import com.ai.cloud.skywalking.context.Context;
import com.ai.cloud.skywalking.context.Span;
import com.ai.cloud.skywalking.model.ContextData;
......@@ -21,14 +22,20 @@ public class LocalBuriedPointSender implements IBuriedPointSender {
public void afterSend() {
// 弹出上下文的栈顶中的元素
Span spanData = Context.getOrCreate().removeLastSpan();
Span spanData = Context.getOrCreate().removeLastSpan();
if (spanData == null) {
return;
}
// 填上必要信息
spanData.setCost(System.currentTimeMillis() - spanData.getStartDate());
if (Config.BuriedPoint.PRINTF) {
System.out.println("viewpointId:" + spanData.getViewPointId() + "\tParentLevelId:" + spanData.
getParentLevel() + "\tLevelId:" + spanData.getLevelId());
}
// 存放到本地发送进程中
ContextBuffer.save(spanData);
if (!Config.Sender.OFF) {
ContextBuffer.save(spanData);
}
}
public void handleException(Throwable e) {
......
......@@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.buriedpoint;
import com.ai.cloud.skywalking.api.IBuriedPointReceiver;
import com.ai.cloud.skywalking.buffer.ContextBuffer;
import com.ai.cloud.skywalking.conf.Config;
import com.ai.cloud.skywalking.context.Context;
import com.ai.cloud.skywalking.context.Span;
import com.ai.cloud.skywalking.model.ContextData;
......@@ -22,6 +23,10 @@ public class RPCBuriedPointReceiver implements IBuriedPointReceiver {
public void beforeReceived(ContextData context) {
Span spanData = ContextGenerator.generateContextFromContextData(context);
// 存放到上下文
if (Config.BuriedPoint.PRINTF) {
System.out.println("viewpointId:" + spanData.getViewPointId() + "\tParentLevelId:" + spanData.
getParentLevel() + "\tLevelId:" + spanData.getLevelId());
}
Context.getOrCreate().append(spanData);
}
......
......@@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.buriedpoint;
import com.ai.cloud.skywalking.api.IBuriedPointSender;
import com.ai.cloud.skywalking.buffer.ContextBuffer;
import com.ai.cloud.skywalking.conf.Config;
import com.ai.cloud.skywalking.context.Context;
import com.ai.cloud.skywalking.context.Span;
import com.ai.cloud.skywalking.model.ContextData;
......@@ -16,8 +17,8 @@ public class RPCBuriedPointSender implements IBuriedPointSender {
// 3.将新创建的Context存放到ThreadLocal栈中。
Context.getOrCreate().append(spanData);
// 4 并将当前的Context返回回去
return new ContextData(new Span(spanData.getTraceId(), spanData.getParentLevel() +
"." + spanData.getLevelId()));
return new ContextData(new Span(spanData.getTraceId(), spanData.getParentLevel()/* +
"." + spanData.getLevelId()*/));
}
public void afterSend() {
......@@ -28,8 +29,14 @@ public class RPCBuriedPointSender implements IBuriedPointSender {
}
// 填上必要信息
spanData.setCost(System.currentTimeMillis() - spanData.getStartDate());
if (Config.BuriedPoint.PRINTF) {
System.out.println("viewpointId:" + spanData.getViewPointId() + "\tParentLevelId:" + spanData.
getParentLevel() + "\tLevelId:" + spanData.getLevelId());
}
// 存放到本地发送进程中
ContextBuffer.save(spanData);
if (!Config.Sender.OFF) {
ContextBuffer.save(spanData);
}
}
public void handleException(Throwable e) {
......
......@@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.buriedpoint;
import com.ai.cloud.skywalking.api.IBuriedPointSender;
import com.ai.cloud.skywalking.buffer.ContextBuffer;
import com.ai.cloud.skywalking.conf.Config;
import com.ai.cloud.skywalking.context.Context;
import com.ai.cloud.skywalking.context.Span;
import com.ai.cloud.skywalking.model.ContextData;
......@@ -47,8 +48,14 @@ public class ThreadBuriedPointSender implements IBuriedPointSender {
}
// 填上必要信息
span.setCost(System.currentTimeMillis() - span.getStartDate());
if (Config.BuriedPoint.PRINTF) {
System.out.println("viewpointId:" + span.getViewPointId() + "\tParentLevelId:" + span.
getParentLevel() + "\tLevelId:" + span.getLevelId());
}
// 存放到本地发送进程中
ContextBuffer.save(span);
if (!Config.Sender.OFF) {
ContextBuffer.save(span);
}
}
public void handleException(Throwable th) {
......
......@@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.buriedpoint;
import com.ai.cloud.skywalking.api.IBuriedPointSender;
import com.ai.cloud.skywalking.buffer.ContextBuffer;
import com.ai.cloud.skywalking.conf.Config;
import com.ai.cloud.skywalking.context.Context;
import com.ai.cloud.skywalking.context.Span;
import com.ai.cloud.skywalking.model.ContextData;
......@@ -27,8 +28,14 @@ public class ThreadFactoryBuriedPointSender implements IBuriedPointSender {
}
// 填上必要信息
spanData.setCost(System.currentTimeMillis() - spanData.getStartDate());
if (Config.BuriedPoint.PRINTF) {
System.out.println("viewpointId:" + spanData.getViewPointId() + "\tParentLevelId:" + spanData.
getParentLevel() + "\tLevelId:" + spanData.getLevelId());
}
// 存放到本地发送进程中
ContextBuffer.save(spanData);
if (!Config.Sender.OFF) {
ContextBuffer.save(spanData);
}
}
public void handleException(Throwable th) {
......
package com.ai.cloud.skywalking.conf;
public class Config {
public static class BuriedPoint{
public static boolean PRINTF = true;
}
public static class Consumer {
//最大消费线程数
public static int MAX_CONSUMER = 2;
......@@ -25,5 +30,8 @@ public class Config {
// 发送服务端配置
public static String SENDER_SERVERS;
// 是否开启发送
public static boolean OFF = false;
}
}
\ No newline at end of file
......@@ -25,6 +25,8 @@ public class ConfigInitializer {
field.set(null, value);
if (field.getType().equals(long.class))
field.set(null, Long.valueOf(value));
if (field.getType().equals(boolean.class))
field.set(null, Boolean.valueOf(value));
}
}
}
......
package com.ai.cloud.skywalking.context;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicInteger;
/**
*/
public class Context {
private static ThreadLocal<SpanStack> spans = new ThreadLocal<SpanStack>();
//private static ThreadLocal<SpanStack> nodes = new ThreadLocal<SpanStack>();
private static ThreadLocal<SpanNodeStack> nodes = new ThreadLocal<SpanNodeStack>();
private static Context context;
private Context() {
spans.set(new SpanStack());
nodes.set(new SpanNodeStack());
}
public void append(Span span) {
spans.get().push(span);
nodes.get().push(span);
}
public Span getLastSpan() {
return spans.get().peek();
return nodes.get().peek();
}
public Span removeLastSpan() {
return spans.get().pop();
return nodes.get().pop();
}
public static Context getOrCreate() {
......@@ -34,12 +36,14 @@ public class Context {
static class SpanStack {
private Stack<Span> spans = new Stack<Span>();
private AtomicInteger levelId = new AtomicInteger();
public Span pop() {
return spans.pop();
}
public void push(Span span) {
span.setLevelId(levelId.getAndDecrement());
spans.push(span);
}
......@@ -59,4 +63,67 @@ public class Context {
}
}
static class SpanNodeStack {
private Stack<SpanNode> spans = new Stack<SpanNode>();
public Span pop() {
Span span = spans.pop().getData();
if (!isEmpty()) {
spans.peek().getLevelId().incrementAndGet();
}
return span;
}
public void push(Span span) {
if (!isEmpty()) {
spans.push(new SpanNode(span, spans.peek().getLevelId().get()));
} else {
spans.push(new SpanNode(span));
}
}
public Span peek() {
if (spans.isEmpty()) {
return null;
}
return spans.peek().getData();
}
public boolean isEmpty() {
return spans.isEmpty();
}
}
static class SpanNode {
private Span data;
private AtomicInteger levelId = new AtomicInteger();
public SpanNode(Span data) {
this.data = data;
this.data.setLevelId(levelId.get());
}
public SpanNode(Span data, int levelId) {
this.data = data;
this.data.setLevelId(levelId);
}
public Span getData() {
return data;
}
public void setData(Span data) {
this.data = data;
}
public AtomicInteger getLevelId() {
return levelId;
}
public void setLevelId(AtomicInteger levelId) {
this.levelId = levelId;
}
}
}
......@@ -5,7 +5,7 @@ import com.ai.cloud.skywalking.util.StringUtil;
public class Span {
private String traceId;
private String parentLevel;
private long levelId;
private int levelId;
private String viewPointId;
private long startDate;
private long cost;
......@@ -60,18 +60,21 @@ public class Span {
}
public String getParentLevel() {
return parentLevel;
if (!StringUtil.isEmpty(parentLevel)) {
return parentLevel;
}
return "";
}
public void setParentLevel(String parentLevel) {
this.parentLevel = parentLevel;
}
public long getLevelId() {
public int getLevelId() {
return levelId;
}
public void setLevelId(long levelId) {
public void setLevelId(int levelId) {
this.levelId = levelId;
}
......
......@@ -35,7 +35,7 @@ public final class ContextGenerator {
if (context == null || StringUtil.isEmpty(context.getTraceId())) {
// 不存在,新创建一个Context
spanData = new Span(TraceIdGenerator.generate());
spanData.setLevelId(0L);
// spanData.setLevelId(0L);
} else {
// 如果不为空,则将当前的Context存放到上下文
spanData = new Span(context.getTraceId());
......@@ -56,12 +56,10 @@ public final class ContextGenerator {
if (parentSpan == null) {
// 不存在,新创建一个Context
span = new Span(TraceIdGenerator.generate());
span.setLevelId(0L);
} else {
// 根据ParentContextData的TraceId和RPCID
span = new Span(parentSpan.getTraceId());
span.setParentLevel(parentSpan.getParentLevel());
span.setLevelId(parentSpan.getLevelId() + 1);
span.setParentLevel(parentSpan.getParentLevel() + "." + parentSpan.getLevelId());
}
return span;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册