Add application id and application instance id check to avoid illegal data.

This commit is contained in:
wusheng 2017-07-05 23:17:45 +08:00
parent e36810d936
commit ff501c4d9e
5 changed files with 60 additions and 67 deletions

View File

@ -10,6 +10,8 @@ import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
public class RemoteDownstreamConfig {
public static class Agent {
public volatile static int APPLICATION_ID = DictionaryUtil.nullValue();
public volatile static int APPLICATION_INSTANCE_ID = DictionaryUtil.nullValue();
}
public static class Collector {

View File

@ -36,12 +36,9 @@ public class ContextManager implements TracingContextListener, BootService, Igno
}
context = new IgnoredTracerContext();
} else {
if (RemoteDownstreamConfig.Agent.APPLICATION_ID == DictionaryUtil.nullValue()) {
/**
* Can't register to collector, no need to trace anything.
*/
context = new IgnoredTracerContext();
} else {
if (RemoteDownstreamConfig.Agent.APPLICATION_ID != DictionaryUtil.nullValue()
|| RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID != DictionaryUtil.nullValue()
) {
int suffixIdx = operationName.lastIndexOf(".");
if (suffixIdx > -1 && Config.Agent.IGNORE_SUFFIX.contains(operationName.substring(suffixIdx))) {
context = new IgnoredTracerContext();
@ -53,6 +50,11 @@ public class ContextManager implements TracingContextListener, BootService, Igno
context = new IgnoredTracerContext();
}
}
} else {
/**
* Can't register to collector, no need to trace anything.
*/
context = new IgnoredTracerContext();
}
}
CONTEXT.set(context);

View File

@ -51,14 +51,6 @@ public class TraceSegment {
*/
private List<AbstractTracingSpan> spans;
/**
* The <code>applicationId</code> represents a name of current application/JVM and indicates which is business
* role in the cluster.
* <p>
* e.g. account_app, billing_app
*/
private int applicationId;
/**
* The <code>relatedGlobalTraces</code> represent a set of all related trace. Most time it contains only one
* element, because only one parent {@link TraceSegment} exists, but, in batch scenario, the num becomes greater
@ -82,20 +74,6 @@ public class TraceSegment {
* and generate a new segment id.
*/
public TraceSegment() {
this.applicationId = (Integer)DictionaryManager.findApplicationCodeSection()
.find(Config.Agent.APPLICATION_CODE)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int applicationId) {
return applicationId;
}
},
new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
throw new IllegalStateException("Application id must not NULL.");
}
}
);
this.traceSegmentId = GlobalIdGenerator.generate(ID_TYPE);
this.spans = new LinkedList<AbstractTracingSpan>();
this.relatedGlobalTraces = new DistributedTraceIds();
@ -154,7 +132,7 @@ public class TraceSegment {
}
public int getApplicationId() {
return applicationId;
return RemoteDownstreamConfig.Agent.APPLICATION_ID;
}
public boolean hasRef() {
@ -206,8 +184,8 @@ public class TraceSegment {
for (AbstractTracingSpan span : this.spans) {
traceSegmentBuilder.addSpans(span.transform());
}
traceSegmentBuilder.setApplicationId(this.applicationId);
traceSegmentBuilder.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_ID);
traceSegmentBuilder.setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID);
traceSegmentBuilder.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID);
upstreamBuilder.setSegment(traceSegmentBuilder.build().toByteString());
return upstreamBuilder.build();
@ -219,7 +197,6 @@ public class TraceSegment {
"traceSegmentId='" + traceSegmentId + '\'' +
", refs=" + refs +
", spans=" + spans +
", applicationId='" + applicationId + '\'' +
", relatedGlobalTraces=" + relatedGlobalTraces +
'}';
}

View File

@ -28,6 +28,7 @@ public class ConsumerPool<T> {
prototype.init();
for (int i = 0; i < num; i++) {
consumerThreads[i] = new ConsumerThread("DataCarrier.Consumser." + i + ".Thread", prototype);
consumerThreads[i].setDaemon(true);
}
}

View File

@ -10,6 +10,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.skywalking.apm.agent.core.boot.BootService;
import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.skywalking.apm.agent.core.jvm.cpu.CPUProvider;
import org.skywalking.apm.agent.core.jvm.gc.GCProvider;
import org.skywalking.apm.agent.core.jvm.memory.MemoryProvider;
@ -59,32 +61,36 @@ public class JVMService implements BootService, Runnable {
@Override
public void run() {
long currentTimeMillis = System.currentTimeMillis();
Date day = new Date(currentTimeMillis);
String second = sdf.format(day);
int blockIndex = Integer.parseInt(second) / 15;
if (blockIndex != lastBlockIdx) {
lastBlockIdx = blockIndex;
try {
JVMMetric.Builder jvmBuilder = JVMMetric.newBuilder();
jvmBuilder.setTime(currentTimeMillis);
jvmBuilder.setCpu(CPUProvider.INSTANCE.getCpuMetric());
jvmBuilder.addAllMemory(MemoryProvider.INSTANCE.getMemoryMetricList());
jvmBuilder.addAllMemoryPool(MemoryPoolProvider.INSTANCE.getMemoryPoolMetricList());
jvmBuilder.addAllGc(GCProvider.INSTANCE.getGCList());
JVMMetric jvmMetric = jvmBuilder.build();
lock.lock();
if (RemoteDownstreamConfig.Agent.APPLICATION_ID != DictionaryUtil.nullValue()
&& RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID != DictionaryUtil.nullValue()
) {
long currentTimeMillis = System.currentTimeMillis();
Date day = new Date(currentTimeMillis);
String second = sdf.format(day);
int blockIndex = Integer.parseInt(second) / 15;
if (blockIndex != lastBlockIdx) {
lastBlockIdx = blockIndex;
try {
buffer.add(jvmMetric);
while (buffer.size() > 4) {
buffer.removeFirst();
JVMMetric.Builder jvmBuilder = JVMMetric.newBuilder();
jvmBuilder.setTime(currentTimeMillis);
jvmBuilder.setCpu(CPUProvider.INSTANCE.getCpuMetric());
jvmBuilder.addAllMemory(MemoryProvider.INSTANCE.getMemoryMetricList());
jvmBuilder.addAllMemoryPool(MemoryPoolProvider.INSTANCE.getMemoryPoolMetricList());
jvmBuilder.addAllGc(GCProvider.INSTANCE.getGCList());
JVMMetric jvmMetric = jvmBuilder.build();
lock.lock();
try {
buffer.add(jvmMetric);
while (buffer.size() > 4) {
buffer.removeFirst();
}
} finally {
lock.unlock();
}
} finally {
lock.unlock();
} catch (Exception e) {
logger.error(e, "Collect JVM info fail.");
}
} catch (Exception e) {
logger.error(e, "Collect JVM info fail.");
}
}
}
@ -95,20 +101,25 @@ public class JVMService implements BootService, Runnable {
@Override
public void run() {
if (status == GRPCChannelStatus.CONNECTED) {
try {
JVMMetrics.Builder builder = JVMMetrics.newBuilder();
lock.lock();
if (RemoteDownstreamConfig.Agent.APPLICATION_ID != DictionaryUtil.nullValue()
&& RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID != DictionaryUtil.nullValue()
) {
if (status == GRPCChannelStatus.CONNECTED) {
try {
builder.addAllMetrics(buffer);
buffer.clear();
} finally {
lock.unlock();
}
JVMMetrics.Builder builder = JVMMetrics.newBuilder();
lock.lock();
try {
builder.addAllMetrics(buffer);
buffer.clear();
} finally {
lock.unlock();
}
stub.collect(builder.build());
} catch (Throwable t) {
logger.error(t, "send JVM metrics to Collector fail.");
builder.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID);
stub.collect(builder.build());
} catch (Throwable t) {
logger.error(t, "send JVM metrics to Collector fail.");
}
}
}
}