From ca11058f8efd4e0b1fc2a66579d17e43c3c1448d Mon Sep 17 00:00:00 2001 From: wu-sheng Date: Wed, 7 Feb 2018 15:58:05 +0800 Subject: [PATCH] Add a new field for operation register. --- .../apm-network/src/main/proto/Common.proto | 10 ++++++ .../src/main/proto/DiscoveryService.proto | 2 ++ .../src/main/proto/TraceSegmentService.proto | 7 +--- .../agent/core/context/TracingContext.java | 2 +- .../context/trace/StackBasedTracingSpan.java | 3 +- .../dictionary/OperationNameDictionary.java | 35 ++++++++++++++----- 6 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 apm-protocol/apm-network/src/main/proto/Common.proto diff --git a/apm-protocol/apm-network/src/main/proto/Common.proto b/apm-protocol/apm-network/src/main/proto/Common.proto new file mode 100644 index 000000000..9142490f3 --- /dev/null +++ b/apm-protocol/apm-network/src/main/proto/Common.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = "org.apache.skywalking.apm.network.proto"; + +enum SpanType { + Entry = 0; + Exit = 1; + Local = 2; +} \ No newline at end of file diff --git a/apm-protocol/apm-network/src/main/proto/DiscoveryService.proto b/apm-protocol/apm-network/src/main/proto/DiscoveryService.proto index f2068f027..c4c06ca5c 100644 --- a/apm-protocol/apm-network/src/main/proto/DiscoveryService.proto +++ b/apm-protocol/apm-network/src/main/proto/DiscoveryService.proto @@ -3,6 +3,7 @@ syntax = "proto3"; option java_multiple_files = true; option java_package = "org.apache.skywalking.apm.network.proto"; +import "Common.proto"; import "Downstream.proto"; service InstanceDiscoveryService { @@ -67,4 +68,5 @@ message ServiceNameMappingElement { message ServiceNameElement { string serviceName = 1; int32 applicationId = 2; + SpanType srcSpanType = 3; } diff --git a/apm-protocol/apm-network/src/main/proto/TraceSegmentService.proto b/apm-protocol/apm-network/src/main/proto/TraceSegmentService.proto index 5ac1aba91..f58fbbf3f 100644 --- a/apm-protocol/apm-network/src/main/proto/TraceSegmentService.proto +++ b/apm-protocol/apm-network/src/main/proto/TraceSegmentService.proto @@ -3,6 +3,7 @@ syntax = "proto3"; option java_multiple_files = true; option java_package = "org.apache.skywalking.apm.network.proto"; +import "Common.proto"; import "Downstream.proto"; import "KeyWithStringValue.proto"; @@ -66,12 +67,6 @@ enum RefType { CrossThread = 1; } -enum SpanType { - Entry = 0; - Exit = 1; - Local = 2; -} - enum SpanLayer { Unknown = 0; Database = 1; diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java index 03a1900a7..f848502b7 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java @@ -296,7 +296,7 @@ public class TracingContext implements AbstractTracerContext { AbstractSpan parentSpan = peek(); final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId(); AbstractTracingSpan span = (AbstractTracingSpan)DictionaryManager.findOperationNameCodeSection() - .findOrPrepare4Register(segment.getApplicationId(), operationName) + .findOrPrepare4Register(segment.getApplicationId(), operationName, false, false) .doInCondition(new PossibleFound.FoundAndObtain() { @Override public Object doProcess(int operationId) { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java index 22a21fd8b..4ee8957c8 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.agent.core.context.trace; import org.apache.skywalking.apm.agent.core.dictionary.DictionaryManager; @@ -48,7 +47,7 @@ public abstract class StackBasedTracingSpan extends AbstractTracingSpan { if (--stackDepth == 0) { if (this.operationId == DictionaryUtil.nullValue()) { this.operationId = (Integer)DictionaryManager.findOperationNameCodeSection() - .findOrPrepare4Register(owner.getApplicationId(), operationName) + .findOrPrepare4Register(owner.getApplicationId(), operationName, this.isEntry(), this.isExit()) .doInCondition( new PossibleFound.FoundAndObtain() { @Override public Object doProcess(int value) { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java index 18c8b35e6..7075f2860 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java @@ -16,7 +16,6 @@ * */ - package org.apache.skywalking.apm.agent.core.dictionary; import io.netty.util.internal.ConcurrentSet; @@ -28,6 +27,7 @@ import org.apache.skywalking.apm.network.proto.ServiceNameDiscoveryServiceGrpc; import org.apache.skywalking.apm.network.proto.ServiceNameElement; import org.apache.skywalking.apm.network.proto.ServiceNameMappingCollection; import org.apache.skywalking.apm.network.proto.ServiceNameMappingElement; +import org.apache.skywalking.apm.network.proto.SpanType; import static org.apache.skywalking.apm.agent.core.conf.Config.Dictionary.OPERATION_NAME_BUFFER_SIZE; @@ -39,19 +39,21 @@ public enum OperationNameDictionary { private Map operationNameDictionary = new ConcurrentHashMap(); private Set unRegisterOperationNames = new ConcurrentSet(); - public PossibleFound findOrPrepare4Register(int applicationId, String operationName) { - return find0(applicationId, operationName, true); + public PossibleFound findOrPrepare4Register(int applicationId, String operationName, + boolean isEntry, boolean isExit) { + return find0(applicationId, operationName, isEntry, isExit, true); } public PossibleFound findOnly(int applicationId, String operationName) { - return find0(applicationId, operationName, false); + return find0(applicationId, operationName, false, false, false); } - private PossibleFound find0(int applicationId, String operationName, boolean registerWhenNotFound) { + private PossibleFound find0(int applicationId, String operationName, + boolean isEntry, boolean isExit, boolean registerWhenNotFound) { if (operationName == null || operationName.length() == 0) { return new NotFound(); } - OperationNameKey key = new OperationNameKey(applicationId, operationName); + OperationNameKey key = new OperationNameKey(applicationId, operationName, isEntry, isExit); Integer operationId = operationNameDictionary.get(key); if (operationId != null) { return new Found(operationId); @@ -78,9 +80,12 @@ public enum OperationNameDictionary { ServiceNameMappingCollection serviceNameMappingCollection = serviceNameDiscoveryServiceBlockingStub.discovery(builder.build()); if (serviceNameMappingCollection.getElementsCount() > 0) { for (ServiceNameMappingElement serviceNameMappingElement : serviceNameMappingCollection.getElementsList()) { + ServiceNameElement element = serviceNameMappingElement.getElement(); OperationNameKey key = new OperationNameKey( - serviceNameMappingElement.getElement().getApplicationId(), - serviceNameMappingElement.getElement().getServiceName()); + element.getApplicationId(), + element.getServiceName(), + SpanType.Entry.equals(element.getSrcSpanType()), + SpanType.Exit.equals(element.getSrcSpanType())); unRegisterOperationNames.remove(key); operationNameDictionary.put(key, serviceNameMappingElement.getServiceId()); } @@ -91,10 +96,14 @@ public enum OperationNameDictionary { private class OperationNameKey { private int applicationId; private String operationName; + private boolean isEntry; + private boolean isExit; - public OperationNameKey(int applicationId, String operationName) { + public OperationNameKey(int applicationId, String operationName, boolean isEntry, boolean isExit) { this.applicationId = applicationId; this.operationName = operationName; + this.isEntry = isEntry; + this.isExit = isExit; } public int getApplicationId() { @@ -123,5 +132,13 @@ public enum OperationNameDictionary { result = 31 * result + operationName.hashCode(); return result; } + + boolean isEntry() { + return isEntry; + } + + boolean isExit() { + return isExit; + } } }