Skip to content

Commit

Permalink
Provide AbstractSpan interface for the context.
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-sheng committed Jun 14, 2017
1 parent cf3d2a4 commit 983dd8e
Show file tree
Hide file tree
Showing 32 changed files with 204 additions and 148 deletions.
@@ -1,6 +1,6 @@
package org.skywalking.apm.agent.core.context;

import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;

/**
* The <code>AbstractTracerContext</code> represents the tracer context manager.
Expand All @@ -14,15 +14,15 @@ public interface AbstractTracerContext {

String getGlobalTraceId();

Span createSpan(String operationName, boolean isLeaf);
AbstractSpan createSpan(String operationName, boolean isLeaf);

Span createSpan(String operationName, long startTime, boolean isLeaf);
AbstractSpan createSpan(String operationName, long startTime, boolean isLeaf);

Span activeSpan();
AbstractSpan activeSpan();

void stopSpan(Span span);
void stopSpan(AbstractSpan span);

void stopSpan(Span span, Long endTime);
void stopSpan(AbstractSpan span, Long endTime);

void dispose();
}
@@ -1,7 +1,7 @@
package org.skywalking.apm.agent.core.context;

import org.skywalking.apm.agent.core.boot.BootService;
import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;

/**
Expand All @@ -12,7 +12,7 @@
* https://github.com/opentracing/specification/blob/master/specification.md#references-between-spans}
* <p> Also, {@link
* ContextManager} delegates to all {@link TracerContext}'s major methods: {@link TracerContext#createSpan(String,
* boolean)}, {@link TracerContext#activeSpan()}, {@link TracerContext#stopSpan(Span)}
* boolean)}, {@link TracerContext#activeSpan()}, {@link AbstractTracerContext#stopSpan(org.skywalking.apm.agent.core.context.trace.AbstractSpan)}
* <p>
*
* @author wusheng
Expand Down Expand Up @@ -55,23 +55,23 @@ public static String getGlobalTraceId() {
}
}

public static Span createSpan(String operationName) {
public static AbstractSpan createSpan(String operationName) {
return get().createSpan(operationName, false);
}

public static Span createSpan(String operationName, long startTime) {
public static AbstractSpan createSpan(String operationName, long startTime) {
return get().createSpan(operationName, startTime, false);
}

public static Span createLeafSpan(String operationName) {
public static AbstractSpan createLeafSpan(String operationName) {
return get().createSpan(operationName, true);
}

public static Span createLeafSpan(String operationName, long startTime) {
public static AbstractSpan createLeafSpan(String operationName, long startTime) {
return get().createSpan(operationName, startTime, true);
}

public static Span activeSpan() {
public static AbstractSpan activeSpan() {
return get().activeSpan();
}

Expand Down
Expand Up @@ -2,7 +2,7 @@

import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;

/**
* The <code>IgnoreTracerContext</code> represent a context should be ignored.
Expand Down Expand Up @@ -36,31 +36,31 @@ public String getGlobalTraceId() {
}

@Override
public Span createSpan(String operationName, boolean isLeaf) {
public AbstractSpan createSpan(String operationName, boolean isLeaf) {
stackDepth++;
return null;
}

@Override
public Span createSpan(String operationName, long startTime, boolean isLeaf) {
public AbstractSpan createSpan(String operationName, long startTime, boolean isLeaf) {
return createSpan(operationName, isLeaf);
}

@Override
public Span activeSpan() {
public AbstractSpan activeSpan() {
return null;
}

@Override
public void stopSpan(Span span) {
public void stopSpan(AbstractSpan span) {
stackDepth--;
if (stackDepth == 0) {

}
}

@Override
public void stopSpan(Span span, Long endTime) {
public void stopSpan(AbstractSpan span, Long endTime) {
stopSpan(span);
}

Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.LeafSpan;
import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
Expand Down Expand Up @@ -42,7 +43,7 @@ public final class TracerContext implements AbstractTracerContext {
* @param operationName {@link Span#operationName}
* @return the new active span.
*/
public Span createSpan(String operationName, boolean isLeaf) {
public AbstractSpan createSpan(String operationName, boolean isLeaf) {
return this.createSpan(operationName, System.currentTimeMillis(), isLeaf);
}

Expand All @@ -54,7 +55,7 @@ public Span createSpan(String operationName, boolean isLeaf) {
* @param isLeaf is true, if the span is a leaf in trace tree.
* @return
*/
public Span createSpan(String operationName, long startTime, boolean isLeaf) {
public AbstractSpan createSpan(String operationName, long startTime, boolean isLeaf) {
Span parentSpan = peek();
Span span;
if (parentSpan == null) {
Expand Down Expand Up @@ -82,7 +83,7 @@ public Span createSpan(String operationName, long startTime, boolean isLeaf) {
/**
* @return the active span of current context.
*/
public Span activeSpan() {
public AbstractSpan activeSpan() {
Span span = peek();
if (span == null) {
throw new IllegalStateException("No active span.");
Expand All @@ -95,7 +96,7 @@ public Span activeSpan() {
*
* @param span to finish. It must the the top element of {@link #activeSpanStack}.
*/
public void stopSpan(Span span) {
public void stopSpan(AbstractSpan span) {
stopSpan(span, System.currentTimeMillis());
}

Expand All @@ -106,7 +107,7 @@ public String getGlobalTraceId() {
return segment.getRelatedGlobalTraces().get(0).get();
}

public void stopSpan(Span span, Long endTime) {
public void stopSpan(AbstractSpan span, Long endTime) {
Span lastSpan = peek();
if (lastSpan.isLeaf()) {
LeafSpan leafSpan = (LeafSpan)lastSpan;
Expand Down Expand Up @@ -147,7 +148,7 @@ private void finish() {
*/
public void inject(ContextCarrier carrier) {
carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
Span span = this.activeSpan();
Span span = (Span)this.activeSpan();
carrier.setSpanId(span.getSpanId());
carrier.setApplicationCode(Config.Agent.APPLICATION_CODE);
String host = span.getPeerHost();
Expand Down
@@ -1,5 +1,6 @@
package org.skywalking.apm.agent.core.context.tag;

import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.Span;

/**
Expand All @@ -22,7 +23,7 @@ public AbstractTag(String tagKey) {
this.key = tagKey;
}

protected abstract void set(Span span, T tagValue);
protected abstract void set(AbstractSpan span, T tagValue);

/**
* @return the key of this tag.
Expand Down
@@ -1,6 +1,6 @@
package org.skywalking.apm.agent.core.context.tag;

import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;

/**
* Do the same thing as {@link StringTag}, just with a {@link Boolean} value.
Expand All @@ -17,7 +17,7 @@ public BooleanTag(String key, boolean defaultValue) {
}

@Override
public void set(Span span, Boolean tagValue) {
public void set(AbstractSpan span, Boolean tagValue) {
span.setTag(key, tagValue);
}

Expand Down
@@ -1,6 +1,6 @@
package org.skywalking.apm.agent.core.context.tag;

import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;

/**
* Do the same thing as {@link StringTag}, just with a {@link Integer} value.
Expand All @@ -13,7 +13,7 @@ public IntTag(String key) {
}

@Override
public void set(Span span, Integer tagValue) {
public void set(AbstractSpan span, Integer tagValue) {
span.setTag(super.key, tagValue);
}

Expand Down
@@ -1,6 +1,6 @@
package org.skywalking.apm.agent.core.context.tag;

import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;

/**
* A subclass of {@link AbstractTag},
Expand All @@ -14,7 +14,7 @@ public StringTag(String tagKey) {
}

@Override
public void set(Span span, String tagValue) {
public void set(AbstractSpan span, String tagValue) {
span.setTag(key, tagValue);
}
}
@@ -1,6 +1,6 @@
package org.skywalking.apm.agent.core.context.tag;

import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;

/**
* The span tags are supported by sky-walking engine.
Expand Down Expand Up @@ -52,15 +52,15 @@ public static final class SPAN_LAYER {
private static final String RPC_FRAMEWORK_LAYER = "rpc";
private static final String HTTP_LAYER = "http";

public static void asDB(Span span) {
public static void asDB(AbstractSpan span) {
SPAN_LAYER_TAG.set(span, DB_LAYER);
}

public static void asRPCFramework(Span span) {
public static void asRPCFramework(AbstractSpan span) {
SPAN_LAYER_TAG.set(span, RPC_FRAMEWORK_LAYER);
}

public static void asHttp(Span span) {
public static void asHttp(AbstractSpan span) {
SPAN_LAYER_TAG.set(span, HTTP_LAYER);
}
}
Expand Down
@@ -0,0 +1,31 @@
package org.skywalking.apm.agent.core.context.trace;

import java.util.Map;

/**
* The <code>AbstractSpan</code> represents the span's skeleton,
* which contains all open methods.
*
* @author wusheng
*/
public interface AbstractSpan {
AbstractSpan setOperationName(String operationName);

void setPeerHost(String peerHost);

void setPort(int port);

void setPeers(String peers);

AbstractSpan setTag(String key, String value);

AbstractSpan setTag(String key, boolean value);

AbstractSpan setTag(String key, Integer value);

AbstractSpan log(Map<String, String> fields);

AbstractSpan log(Throwable t);

AbstractSpan log(String event);
}
Expand Up @@ -32,7 +32,7 @@
* Created by wusheng on 2017/2/17.
*/
@JsonAdapter(Span.Serializer.class)
public class Span {
public class Span implements AbstractSpan {
private static Gson SERIALIZATION_GSON = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

private int spanId;
Expand Down Expand Up @@ -111,7 +111,7 @@ protected Span(int spanId, int parentSpanId, String operationName, long startTim
this.spanId = spanId;
this.parentSpanId = parentSpanId;
this.startTime = startTime;
this.operationName = operationName;
this.setOperationName(operationName);
}

/**
Expand Down Expand Up @@ -192,10 +192,12 @@ public void finish(TraceSegment owner, long endTime) {

/**
* Sets the string name for the logical operation this span represents.
* These is one scenario, which trigger context switch.
* 1) the operations ends with the defined suffix, see {@link Config.Agent#IGNORE_SUFFIX}
*
* @return this Span instance, for chaining
*/
public Span setOperationName(String operationName) {
public AbstractSpan setOperationName(String operationName) {
this.operationName = operationName;
if (this.spanId == 0) {
if (operationName != null) {
Expand Down
Expand Up @@ -5,9 +5,9 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.context.trace.Span;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.skywalking.apm.agent.core.context.tag.Tags;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;

/**
* Created by wusheng on 2017/2/19.
Expand All @@ -20,7 +20,7 @@ public static void setup() {

@Test
public void testDelegateToTracerContext() {
Span span = ContextManager.createSpan("serviceA");
AbstractSpan span = ContextManager.createSpan("serviceA");
Tags.COMPONENT.set(span, "test");

Assert.assertEquals(span, ContextManager.activeSpan());
Expand Down

0 comments on commit 983dd8e

Please sign in to comment.