parent
0935d78f10
commit
a5901261b4
|
|
@ -1,22 +0,0 @@
|
|||
package com.ai.cloud.skywalking.buffer;
|
||||
|
||||
import com.ai.cloud.skywalking.context.Span;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static com.ai.cloud.skywalking.conf.Config.Buffer.POOL_MAX_LENGTH;
|
||||
|
||||
class BufferPool {
|
||||
private static BufferGroup[] bufferGroups = new BufferGroup[POOL_MAX_LENGTH];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < POOL_MAX_LENGTH; i++) {
|
||||
bufferGroups[i] = new BufferGroup("buffer_group-" + i);
|
||||
}
|
||||
}
|
||||
|
||||
public void save(Span span) {
|
||||
bufferGroups[ThreadLocalRandom.current().nextInt(0, POOL_MAX_LENGTH)].save(span);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,56 +1,38 @@
|
|||
package com.ai.cloud.skywalking.buffer;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.Config;
|
||||
import com.ai.cloud.skywalking.conf.ConfigInitializer;
|
||||
import com.ai.cloud.skywalking.context.Span;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static com.ai.cloud.skywalking.conf.Config.Buffer.POOL_MAX_LENGTH;
|
||||
|
||||
public class ContextBuffer {
|
||||
private static Logger logger = Logger.getLogger(ContextBuffer.class.getName());
|
||||
private static boolean isAuth = true;
|
||||
|
||||
static {
|
||||
InputStream inputStream = ContextBuffer.class.getResourceAsStream("/sky-walking.auth");
|
||||
if (inputStream == null) {
|
||||
isAuth = false;
|
||||
logger.log(Level.ALL, "No provider sky-walking certification documents, buried point won't work");
|
||||
}
|
||||
if (isAuth) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.load(inputStream);
|
||||
ConfigInitializer.initialize(properties, Config.class);
|
||||
} catch (IllegalAccessException e) {
|
||||
isAuth = false;
|
||||
logger.log(Level.ALL, "Parsing certification file failed, buried won't work");
|
||||
} catch (IOException e) {
|
||||
isAuth = false;
|
||||
logger.log(Level.ALL, "Failed to read the certification file, buried won't work");
|
||||
}
|
||||
|
||||
ContextBuffer.init();
|
||||
}
|
||||
}
|
||||
|
||||
private static BufferPool pool;
|
||||
private static BufferPool pool = new BufferPool();
|
||||
|
||||
private ContextBuffer() {
|
||||
//non
|
||||
}
|
||||
|
||||
private static void init() {
|
||||
if (pool == null)
|
||||
pool = new BufferPool();
|
||||
}
|
||||
|
||||
public static void save(Span span) {
|
||||
if (!isAuth)
|
||||
return;
|
||||
pool.save(span);
|
||||
}
|
||||
|
||||
|
||||
static class BufferPool {
|
||||
private static BufferGroup[] bufferGroups = new BufferGroup[POOL_MAX_LENGTH];
|
||||
static {
|
||||
for (int i = 0; i < POOL_MAX_LENGTH; i++) {
|
||||
bufferGroups[i] = new BufferGroup("buffer_group-" + i);
|
||||
}
|
||||
}
|
||||
|
||||
public void save(Span span) {
|
||||
bufferGroups[ThreadLocalRandom.current().nextInt(0, POOL_MAX_LENGTH)].save(span);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ 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.AuthDesc;
|
||||
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;
|
||||
import com.ai.cloud.skywalking.model.EmptyContextData;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.util.ContextGenerator;
|
||||
import com.ai.cloud.skywalking.util.ExceptionHandleUtil;
|
||||
|
|
@ -18,6 +20,9 @@ public class LocalBuriedPointSender implements IBuriedPointSender {
|
|||
private static Logger logger = Logger.getLogger(LocalBuriedPointSender.class.getName());
|
||||
|
||||
public ContextData beforeSend(Identification id) {
|
||||
if (!AuthDesc.isAuth())
|
||||
return new EmptyContextData();
|
||||
|
||||
Span spanData = ContextGenerator.generateSpanFromThreadLocal(id);
|
||||
// 将新创建的Context存放到ThreadLocal栈中。
|
||||
Context.append(spanData);
|
||||
|
|
@ -26,6 +31,9 @@ public class LocalBuriedPointSender implements IBuriedPointSender {
|
|||
}
|
||||
|
||||
public void afterSend() {
|
||||
if (!AuthDesc.isAuth())
|
||||
return;
|
||||
|
||||
// 弹出上下文的栈顶中的元素
|
||||
Span spanData = Context.removeLastSpan();
|
||||
if (spanData == null) {
|
||||
|
|
|
|||
|
|
@ -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.AuthDesc;
|
||||
import com.ai.cloud.skywalking.conf.Config;
|
||||
import com.ai.cloud.skywalking.context.Context;
|
||||
import com.ai.cloud.skywalking.context.Span;
|
||||
|
|
@ -18,6 +19,9 @@ public class RPCBuriedPointReceiver implements IBuriedPointReceiver {
|
|||
private static Logger logger = Logger.getLogger(LocalBuriedPointSender.class.getName());
|
||||
|
||||
public void afterReceived() {
|
||||
if (!AuthDesc.isAuth())
|
||||
return;
|
||||
|
||||
// 获取上下文的栈顶中的元素
|
||||
Span spanData = Context.removeLastSpan();
|
||||
// 填上必要信息
|
||||
|
|
@ -27,6 +31,9 @@ public class RPCBuriedPointReceiver implements IBuriedPointReceiver {
|
|||
}
|
||||
|
||||
public void beforeReceived(ContextData context, Identification id) {
|
||||
if (!AuthDesc.isAuth())
|
||||
return;
|
||||
|
||||
Span spanData = ContextGenerator.generateSpanFromContextData(context, id);
|
||||
//设置是否为接收端
|
||||
spanData.setReceiver(true);
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ 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.AuthDesc;
|
||||
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;
|
||||
import com.ai.cloud.skywalking.model.EmptyContextData;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.util.ContextGenerator;
|
||||
import com.ai.cloud.skywalking.util.ExceptionHandleUtil;
|
||||
|
|
@ -18,6 +20,9 @@ public class RPCBuriedPointSender implements IBuriedPointSender {
|
|||
private static Logger logger = Logger.getLogger(RPCBuriedPointReceiver.class.getName());
|
||||
|
||||
public ContextData beforeSend(Identification id) {
|
||||
if (!AuthDesc.isAuth())
|
||||
return new EmptyContextData();
|
||||
|
||||
Span spanData = ContextGenerator.generateSpanFromThreadLocal(id);
|
||||
// 将新创建的Context存放到ThreadLocal栈中。
|
||||
Context.append(spanData);
|
||||
|
|
@ -26,6 +31,10 @@ public class RPCBuriedPointSender implements IBuriedPointSender {
|
|||
}
|
||||
|
||||
public void afterSend() {
|
||||
|
||||
if (!AuthDesc.isAuth())
|
||||
return;
|
||||
|
||||
// 获取上下文的栈顶中的元素
|
||||
Span spanData = Context.removeLastSpan();
|
||||
if (spanData == null) {
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ 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.AuthDesc;
|
||||
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;
|
||||
import com.ai.cloud.skywalking.model.EmptyContextData;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.util.BuriedPointMachineUtil;
|
||||
import com.ai.cloud.skywalking.util.ExceptionHandleUtil;
|
||||
|
|
@ -21,6 +23,8 @@ public class ThreadBuriedPointSender implements IBuriedPointSender {
|
|||
private Span span;
|
||||
|
||||
public ThreadBuriedPointSender(int threadSeqId) {
|
||||
if (!AuthDesc.isAuth())
|
||||
return ;
|
||||
Span spanData;
|
||||
// 从ThreadLocal中取出上下文
|
||||
final Span parentSpanData = Context.getLastSpan();
|
||||
|
|
@ -36,6 +40,9 @@ public class ThreadBuriedPointSender implements IBuriedPointSender {
|
|||
}
|
||||
|
||||
public ContextData beforeSend(Identification id) {
|
||||
if (!AuthDesc.isAuth())
|
||||
return new EmptyContextData();
|
||||
|
||||
if (this.span == null) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ 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.AuthDesc;
|
||||
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;
|
||||
import com.ai.cloud.skywalking.model.EmptyContextData;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.util.ContextGenerator;
|
||||
import com.ai.cloud.skywalking.util.ExceptionHandleUtil;
|
||||
|
|
@ -18,6 +20,9 @@ public class ThreadFactoryBuriedPointSender implements IBuriedPointSender {
|
|||
private static Logger logger = Logger.getLogger(ThreadBuriedPointSender.class.getName());
|
||||
|
||||
public ContextData beforeSend(Identification id) {
|
||||
if (!AuthDesc.isAuth())
|
||||
return new EmptyContextData();
|
||||
|
||||
Span spanData = ContextGenerator.generateSpanFromThreadLocal(id);
|
||||
// 将新创建的Context存放到ThreadLocal栈中。
|
||||
Context.append(spanData);
|
||||
|
|
@ -26,6 +31,9 @@ public class ThreadFactoryBuriedPointSender implements IBuriedPointSender {
|
|||
}
|
||||
|
||||
public void afterSend() {
|
||||
if (!AuthDesc.isAuth())
|
||||
return;
|
||||
|
||||
// 获取上下文的栈顶中的元素
|
||||
Span spanData = Context.removeLastSpan();
|
||||
if (spanData == null) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ai.cloud.skywalking.conf;
|
||||
|
||||
public class AuthDesc {
|
||||
static boolean isAuth = false;
|
||||
|
||||
static {
|
||||
ConfigInitializer.initialize();
|
||||
}
|
||||
|
||||
public static boolean isAuth() {
|
||||
return isAuth;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,10 @@ package com.ai.cloud.skywalking.conf;
|
|||
|
||||
public class Config {
|
||||
|
||||
public static class SkyWalking {
|
||||
public static String USER_ID;
|
||||
}
|
||||
|
||||
public static class BuriedPoint {
|
||||
//是否打印埋点信息
|
||||
public static boolean PRINTF = false;
|
||||
|
|
|
|||
|
|
@ -1,15 +1,33 @@
|
|||
package com.ai.cloud.skywalking.conf;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ConfigInitializer {
|
||||
private static Logger logger = Logger.getLogger(ConfigInitializer.class.getName());
|
||||
|
||||
|
||||
public static void initialize(Properties properties, Class<?> rootConfigType) throws IllegalAccessException {
|
||||
initNextLevel(properties, rootConfigType, new ConfigDesc());
|
||||
public static void initialize() {
|
||||
InputStream inputStream = ConfigInitializer.class.getResourceAsStream("/sky-walking.auth");
|
||||
if (inputStream == null) {
|
||||
logger.log(Level.ALL, "No provider sky-walking certification documents, buried point won't work");
|
||||
} else {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.load(inputStream);
|
||||
initNextLevel(properties, Config.class, new ConfigDesc());
|
||||
AuthDesc.isAuth = true;
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.log(Level.ALL, "Parsing certification file failed, buried won't work");
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.ALL, "Failed to read the certification file, buried won't work");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void initNextLevel(Properties properties, Class<?> recentConfigType, ConfigDesc parentDesc) throws NumberFormatException, IllegalArgumentException, IllegalAccessException {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,10 @@ public class ContextData {
|
|||
private long levelId;
|
||||
private char spanType;
|
||||
|
||||
ContextData() {
|
||||
|
||||
}
|
||||
|
||||
public ContextData(Span span) {
|
||||
this.traceId = span.getTraceId();
|
||||
this.parentLevel = span.getParentLevel();
|
||||
|
|
@ -27,8 +31,8 @@ public class ContextData {
|
|||
return levelId;
|
||||
}
|
||||
|
||||
public char getSpanType() {
|
||||
return spanType;
|
||||
}
|
||||
public char getSpanType() {
|
||||
return spanType;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package com.ai.cloud.skywalking.model;
|
||||
|
||||
public class EmptyContextData extends ContextData {
|
||||
public EmptyContextData() {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.ai.cloud.skywalking.util;
|
||||
|
||||
import com.ai.cloud.skywalking.context.Context;
|
||||
import com.ai.cloud.skywalking.context.Span;
|
||||
|
||||
public final class BusinessKeyTracing {
|
||||
|
||||
private static final char spiltChar='^';
|
||||
|
||||
|
||||
private BusinessKeyTracing() {
|
||||
// Non
|
||||
}
|
||||
|
||||
public static void trace(String businessKey) {
|
||||
//
|
||||
Span spanData = Context.getLastSpan();
|
||||
spanData.setBusinessKey(businessKey.replace('-',spiltChar));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
package com.ai.cloud.skywalking.util;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.Config;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public final class TraceIdGenerator {
|
||||
|
|
@ -8,6 +10,6 @@ public final class TraceIdGenerator {
|
|||
}
|
||||
|
||||
public static String generate() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
return UUID.randomUUID().toString().replaceAll("-", "") + Config.SkyWalking.USER_ID;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue