Skip to content

Commit

Permalink
support span amount control mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
ascrutae committed Nov 12, 2017
1 parent e8a43a1 commit 0eb0907
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.skywalking.apm.agent.core.context.trace.EntrySpan;
import org.skywalking.apm.agent.core.context.trace.ExitSpan;
import org.skywalking.apm.agent.core.context.trace.LocalSpan;
import org.skywalking.apm.agent.core.context.trace.NoopExitSpan;
import org.skywalking.apm.agent.core.context.trace.NoopSpan;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.skywalking.apm.agent.core.context.trace.TraceSegmentRef;
Expand Down Expand Up @@ -95,17 +96,28 @@ public void inject(ContextCarrier carrier) {
if (!span.isExit()) {
throw new IllegalStateException("Inject can be done only in Exit Span");
}
ExitSpan exitSpan = (ExitSpan)span;

String peer;
int peerId;
if (span instanceof NoopExitSpan) {
NoopExitSpan exitSpan = (NoopExitSpan)span;
peerId = exitSpan.getPeerId();
peer = exitSpan.getPeer();
} else {
ExitSpan exitSpan = (ExitSpan)span;
peerId = exitSpan.getPeerId();
peer = exitSpan.getPeer();
}

carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
carrier.setSpanId(span.getSpanId());

carrier.setParentApplicationInstanceId(segment.getApplicationInstanceId());

if (DictionaryUtil.isNull(exitSpan.getPeerId())) {
carrier.setPeerHost(exitSpan.getPeer());
if (DictionaryUtil.isNull(peerId)) {
carrier.setPeerHost(peer);
} else {
carrier.setPeerId(exitSpan.getPeerId());
carrier.setPeerId(peerId);
}
List<TraceSegmentRef> refs = this.segment.getRefs();
int operationId;
Expand Down Expand Up @@ -304,21 +316,21 @@ public Object doProcess() {
*/
@Override
public AbstractSpan createExitSpan(final String operationName, final String remotePeer) {
if (isLimitMechanismWorking()) {
NoopSpan span = new NoopSpan();
return push(span);
}
AbstractSpan exitSpan;
AbstractSpan parentSpan = peek();
if (parentSpan != null && parentSpan.isExit()) {
exitSpan = parentSpan;
} else {
final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId();
exitSpan = (AbstractTracingSpan)DictionaryManager.findApplicationCodeSection()
exitSpan = (AbstractSpan)DictionaryManager.findApplicationCodeSection()
.find(remotePeer).doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(final int peerId) {
if (isLimitMechanismWorking()) {
return new NoopExitSpan(peerId);
}

return DictionaryManager.findOperationNameCodeSection()
.findOnly(segment.getApplicationId(), operationName)
.doInCondition(
Expand All @@ -338,6 +350,10 @@ public Object doProcess() {
new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
if (isLimitMechanismWorking()) {
return new NoopExitSpan(remotePeer);
}

return DictionaryManager.findOperationNameCodeSection()
.findOnly(segment.getApplicationId(), operationName)
.doInCondition(
Expand Down Expand Up @@ -489,6 +505,6 @@ private AbstractSpan first() {
}

private boolean isLimitMechanismWorking() {
return spanIdGenerator > Config.Agent.SPAN_LIMIT_PER_SEGMENT;
return spanIdGenerator >= Config.Agent.SPAN_LIMIT_PER_SEGMENT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/

package org.skywalking.apm.agent.core.context.trace;

/**
* The <code>AbstractNoopSpan</code> represents a span implementation without any actual operation.
*
* @author zhangxin
*/
public interface AbstractNoopSpan extends AbstractSpan {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/

package org.skywalking.apm.agent.core.context.trace;

import java.util.Map;
import org.skywalking.apm.network.trace.component.Component;

public class NoopExitSpan implements AbstractNoopSpan {

private String peer;
private int peerId;

public NoopExitSpan(int peerId) {
this.peerId = peerId;
}

public NoopExitSpan(String peer) {
this.peer = peer;
}

@Override public AbstractSpan setComponent(Component component) {
return this;
}

@Override public AbstractSpan setComponent(String componentName) {
return this;
}

@Override public AbstractSpan setLayer(SpanLayer layer) {
return this;
}

@Override public AbstractSpan tag(String key, String value) {
return this;
}

@Override public AbstractSpan log(Throwable t) {
return this;
}

@Override public AbstractSpan errorOccurred() {
return null;
}

@Override public boolean isEntry() {
return false;
}

@Override public boolean isExit() {
return true;
}

@Override public AbstractSpan log(long timestamp, Map<String, ?> event) {
return this;
}

@Override public AbstractSpan setOperationName(String operationName) {
return this;
}

@Override public AbstractSpan start() {
return this;
}

@Override public int getSpanId() {
return 0;
}

@Override public int getOperationId() {
return 0;
}

@Override public String getOperationName() {
return "";
}

@Override public AbstractSpan setOperationId(int operationId) {
return this;
}

public int getPeerId() {
return peerId;
}

public String getPeer() {
return peer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author wusheng
*/
public class NoopSpan implements AbstractSpan {
public class NoopSpan implements AbstractNoopSpan {
public NoopSpan() {
}

Expand Down

0 comments on commit 0eb0907

Please sign in to comment.