Commit 0eb0907d authored by ascrutae's avatar ascrutae
Browse files

support span amount control mechanism

parent e8a43a19
Loading
Loading
Loading
Loading
+26 −10
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
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;
@@ -95,17 +96,28 @@ public class TracingContext implements AbstractTracerContext {
        if (!span.isExit()) {
            throw new IllegalStateException("Inject can be done only in Exit 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;
@@ -304,21 +316,21 @@ public class TracingContext implements AbstractTracerContext {
     */
    @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(
@@ -338,6 +350,10 @@ public class TracingContext implements AbstractTracerContext {
                    new PossibleFound.NotFoundAndObtain() {
                        @Override
                        public Object doProcess() {
                            if (isLimitMechanismWorking()) {
                                return new NoopExitSpan(remotePeer);
                            }

                            return DictionaryManager.findOperationNameCodeSection()
                                .findOnly(segment.getApplicationId(), operationName)
                                .doInCondition(
@@ -489,6 +505,6 @@ public class TracingContext implements AbstractTracerContext {
    }

    private boolean isLimitMechanismWorking() {
        return spanIdGenerator > Config.Agent.SPAN_LIMIT_PER_SEGMENT;
        return spanIdGenerator >= Config.Agent.SPAN_LIMIT_PER_SEGMENT;
    }
}
+27 −0
Original line number Diff line number Diff line
/*
 * 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 {
}
+104 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import org.skywalking.apm.network.trace.component.Component;
 *
 * @author wusheng
 */
public class NoopSpan implements AbstractSpan {
public class NoopSpan implements AbstractNoopSpan {
    public NoopSpan() {
    }