Commit dcafdaa3 authored by 吴晟's avatar 吴晟
Browse files

Fix compile issue. Add new rest-service Object: SegmentsMessage.java. It uses...

Fix compile issue. Add new rest-service Object: SegmentsMessage.java. It uses Gson(JsonAdapter) as serialize/deserialize tool.
parent f48f82fc
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -91,12 +91,14 @@
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${compiler.version}</source>
                    <target>${compiler.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
                <version>3.6.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
+74 −0
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace;

import com.a.eye.skywalking.trace.TraceId.PropagatedTraceId;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * The <code>SegmentsMessage</code> is a set of {@link TraceSegment},
 * this set provides a container, when several {@link TraceSegment}s are going to uplink to server.
 *
 *
 * @author wusheng
 */
@JsonAdapter(SegmentsMessage.Serializer.class)
public class SegmentsMessage {
    private List<TraceSegment> segments;

    public SegmentsMessage(){
        segments = new LinkedList<TraceSegment>();
    }

    public void append(TraceSegment segment){
        this.segments.add(segment);
    }

    public List<TraceSegment> getSegments() {
        return Collections.unmodifiableList(segments);
    }

    public static class Serializer extends TypeAdapter<SegmentsMessage>{

        @Override
        public void write(JsonWriter out, SegmentsMessage value) throws IOException {
            Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();

            out.beginArray();
            try {
                for (TraceSegment segment : value.segments) {
                    out.jsonValue(gson.toJson(segment));
                }
            }finally {
                out.endArray();
            }
        }

        @Override
        public SegmentsMessage read(JsonReader in) throws IOException {
            SegmentsMessage message = new SegmentsMessage();
            in.beginArray();
            Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
            try {
                while (in.hasNext()) {
                    TraceSegment traceSegment = gson.fromJson(in, TraceSegment.class);
                    message.append(traceSegment);
                }
            } finally {
                in.endArray();
            }
            return message;
        }
    }
}
+0 −25
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace.TraceId;

import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;

/**
 * The <code>DistributedTraceId</code> presents a distributed call chain.
 *
@@ -19,7 +13,6 @@ import java.io.IOException;
 *
 * @author wusheng
 */
@JsonAdapter(DistributedTraceId.Serializer.class)
public abstract class DistributedTraceId {
    private String id;

@@ -47,22 +40,4 @@ public abstract class DistributedTraceId {
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    public static class Serializer extends TypeAdapter<DistributedTraceId> {

        @Override
        public void write(JsonWriter out, DistributedTraceId value) throws IOException {
            out.beginArray();
            out.value(value.get());
            out.endArray();
        }

        @Override
        public DistributedTraceId read(JsonReader in) throws IOException {
            in.beginArray();
            PropagatedTraceId traceId = new PropagatedTraceId(in.nextString());
            in.endArray();
            return traceId;
        }
    }
}
+66 −0
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace.TraceId;

import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wusheng
 */
@JsonAdapter(DistributedTraceIds.Serializer.class)
public class DistributedTraceIds {
    private LinkedList<DistributedTraceId> relatedGlobalTraces;

    public DistributedTraceIds() {
        relatedGlobalTraces = new LinkedList<DistributedTraceId>();
    }

    public List<DistributedTraceId> getRelatedGlobalTraces() {
        return Collections.unmodifiableList(relatedGlobalTraces);
    }

    public void append(DistributedTraceId distributedTraceId) {
        if (relatedGlobalTraces.size() > 0 && relatedGlobalTraces.getFirst() instanceof NewDistributedTraceId) {
            relatedGlobalTraces.removeFirst();
        }
        if (!relatedGlobalTraces.contains(distributedTraceId)) {
            relatedGlobalTraces.add(distributedTraceId);
        }
    }

    public static class Serializer extends TypeAdapter<DistributedTraceIds> {

        @Override
        public void write(JsonWriter out, DistributedTraceIds value) throws IOException {
            List<DistributedTraceId> globalTraces = value.getRelatedGlobalTraces();

            if (globalTraces.size() > 0) {
                out.beginArray();
                for (DistributedTraceId trace : globalTraces) {
                    out.value(trace.get());
                }
                out.endArray();
            }
        }

        @Override
        public DistributedTraceIds read(JsonReader in) throws IOException {
            DistributedTraceIds distributedTraceIds = new DistributedTraceIds();
            in.beginArray();
            try {
                while (in.hasNext()) {
                    PropagatedTraceId traceId = new PropagatedTraceId(in.nextString());
                    distributedTraceIds.append(traceId);
                }
            } finally {
                in.endArray();
            }
            return distributedTraceIds;
        }
    }
}
+10 −13
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace;

import com.a.eye.skywalking.trace.TraceId.DistributedTraceId;
import com.a.eye.skywalking.trace.TraceId.DistributedTraceIds;
import com.a.eye.skywalking.trace.TraceId.NewDistributedTraceId;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@@ -86,7 +87,7 @@ public class TraceSegment {
     */
    @Expose
    @SerializedName(value="gt")
    private LinkedList<DistributedTraceId> relatedGlobalTraces;
    private DistributedTraceIds relatedGlobalTraces;

    /**
     * Create a trace segment, by given segmentId.
@@ -94,18 +95,18 @@ public class TraceSegment {
     */
    public TraceSegment(String applicationCode) {
        this();
        this.traceSegmentId = GlobalIdGenerator.generate(ID_TYPE);
        this.applicationCode = applicationCode;
        this.startTime = System.currentTimeMillis();
    }

    /**
     * Create a default/empty trace segment
     */
    public TraceSegment() {
        this.startTime = System.currentTimeMillis();
        this.traceSegmentId = GlobalIdGenerator.generate(ID_TYPE);
        this.spans = new LinkedList<Span>();
        this.relatedGlobalTraces = new LinkedList<DistributedTraceId>();
        this.relatedGlobalTraces.add(new NewDistributedTraceId());
        this.relatedGlobalTraces = new DistributedTraceIds();
        this.relatedGlobalTraces.append(new NewDistributedTraceId());
    }

    /**
@@ -126,13 +127,8 @@ public class TraceSegment {
        if (distributedTraceIds == null || distributedTraceIds.size() == 0) {
            return;
        }
        if (relatedGlobalTraces.getFirst() instanceof NewDistributedTraceId) {
            relatedGlobalTraces.removeFirst();
        }
        for (DistributedTraceId distributedTraceId : distributedTraceIds) {
            if(!relatedGlobalTraces.contains(distributedTraceId)){
                relatedGlobalTraces.add(distributedTraceId);
            }
            relatedGlobalTraces.append(distributedTraceId);
        }
    }

@@ -176,7 +172,7 @@ public class TraceSegment {
    }

    public List<DistributedTraceId> getRelatedGlobalTraces() {
        return Collections.unmodifiableList(relatedGlobalTraces);
        return relatedGlobalTraces.getRelatedGlobalTraces();
    }

    public List<Span> getSpans() {
@@ -187,7 +183,8 @@ public class TraceSegment {
        return applicationCode;
    }

    @Override public String toString() {
    @Override
    public String toString() {
        return "TraceSegment{" +
            "traceSegmentId='" + traceSegmentId + '\'' +
            ", startTime=" + startTime +
Loading