ServiceAMock.java 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

package org.apache.skywalking.oap.server.receiver.trace.mock;

import io.grpc.stub.StreamObserver;
22 23 24 25
import org.apache.skywalking.apm.network.language.agent.v3.SegmentObject;
import org.apache.skywalking.apm.network.language.agent.v3.SpanLayer;
import org.apache.skywalking.apm.network.language.agent.v3.SpanObject;
import org.apache.skywalking.apm.network.language.agent.v3.SpanType;
26 27 28
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;

class ServiceAMock {
29 30
    public static String SERVICE_NAME = "mock_a_service";
    public static String SERVICE_INSTANCE_NAME = "mock_a_service_instance";
31 32 33 34 35

    static String REST_ENDPOINT = "/dubbox-case/case/dubbox-rest";
    static String DUBBO_ENDPOINT = "org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()";
    static String DUBBO_ADDRESS = "DubboIPAddress:1000";

36 37 38
    void mock(StreamObserver<SegmentObject> streamObserver, String traceId,
              String segmentId, long startTimestamp) {
        streamObserver.onNext(createSegment(startTimestamp, traceId, segmentId).build());
39 40
    }

41
    private SegmentObject.Builder createSegment(long startTimestamp, String traceId, String segmentId) {
42
        SegmentObject.Builder segment = SegmentObject.newBuilder();
43
        segment.setTraceId(traceId);
44
        segment.setTraceSegmentId(segmentId);
45 46 47 48 49
        segment.setService(SERVICE_NAME);
        segment.setServiceInstance(SERVICE_INSTANCE_NAME);
        segment.addSpans(createEntrySpan(startTimestamp));
        segment.addSpans(createLocalSpan(startTimestamp));
        segment.addSpans(createExitSpan(startTimestamp));
50

51
        return segment;
52 53
    }

54 55
    private SpanObject.Builder createEntrySpan(long startTimestamp) {
        SpanObject.Builder span = SpanObject.newBuilder();
56 57 58 59 60 61 62
        span.setSpanId(0);
        span.setSpanType(SpanType.Entry);
        span.setSpanLayer(SpanLayer.Http);
        span.setParentSpanId(-1);
        span.setStartTime(startTimestamp);
        span.setEndTime(startTimestamp + 6000);
        span.setComponentId(ComponentsDefine.TOMCAT.getId());
63
        span.setOperationName(REST_ENDPOINT);
64 65 66 67
        span.setIsError(false);
        return span;
    }

68 69
    private SpanObject.Builder createLocalSpan(long startTimestamp) {
        SpanObject.Builder span = SpanObject.newBuilder();
70 71 72 73 74 75 76 77 78 79
        span.setSpanId(1);
        span.setSpanType(SpanType.Local);
        span.setParentSpanId(0);
        span.setStartTime(startTimestamp + 100);
        span.setEndTime(startTimestamp + 500);
        span.setOperationName("org.apache.skywalking.Local.do");
        span.setIsError(false);
        return span;
    }

80 81
    private SpanObject.Builder createExitSpan(long startTimestamp) {
        SpanObject.Builder span = SpanObject.newBuilder();
82 83 84 85 86 87 88 89
        span.setSpanId(2);
        span.setSpanType(SpanType.Exit);
        span.setSpanLayer(SpanLayer.RPCFramework);
        span.setParentSpanId(1);
        span.setStartTime(startTimestamp + 120);
        span.setEndTime(startTimestamp + 5800);
        span.setComponentId(ComponentsDefine.DUBBO.getId());
        span.setOperationName(DUBBO_ENDPOINT);
90
        span.setPeer(DUBBO_ADDRESS);
91 92 93 94
        span.setIsError(false);
        return span;
    }
}