Java Code Examples for org.apache.skywalking.apm.agent.core.context.ContextManager#createEntrySpan()

The following examples show how to use org.apache.skywalking.apm.agent.core.context.ContextManager#createEntrySpan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SWServerRPCPlugin.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void serverReceiveRequest(RPCContext context) {
    Map meta = context.requestCallMeta();

    ContextCarrier carrier = new ContextCarrier();
    CarrierItem items = carrier.items();
    while (items.hasNext()) {
        items = items.next();
        ByteBuffer buffer = (ByteBuffer) meta.get(new Utf8(items.getHeadKey()));
        items.setHeadValue(new String(buffer.array()));
    }

    String operationName = prefix + context.getMessage().getName();
    AbstractSpan span = ContextManager.createEntrySpan(operationName, carrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.AVRO_SERVER);
}
 
Example 2
Source File: SofaRpcProviderInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    SofaRequest sofaRequest = (SofaRequest) allArguments[0];

    AbstractSpan span = null;

    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        final String headKey = next.getHeadKey();
        final Object attachment = sofaRequest.getRequestProp(SKYWALKING_PREFIX + headKey);
        if (attachment != null) {
            next.setHeadValue(attachment.toString());
        } else {
            next.setHeadValue("");
        }
    }
    span = ContextManager.createEntrySpan(generateViewPoint(sofaRequest), contextCarrier);

    span.setComponent(ComponentsDefine.SOFARPC);
    SpanLayer.asRPCFramework(span);
}
 
Example 3
Source File: PulsarConsumerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    if (allArguments[0] != null) {
        ConsumerEnhanceRequiredInfo requiredInfo = (ConsumerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
        Message msg = (Message) allArguments[0];
        ContextCarrier carrier = new ContextCarrier();
        CarrierItem next = carrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(msg.getProperty(next.getHeadKey()));
        }
        AbstractSpan activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + requiredInfo.getTopic() + CONSUMER_OPERATE_NAME + requiredInfo
            .getSubscriptionName(), carrier);
        activeSpan.setComponent(ComponentsDefine.PULSAR_CONSUMER);
        SpanLayer.asMQ(activeSpan);
        Tags.MQ_BROKER.set(activeSpan, requiredInfo.getServiceUrl());
        Tags.MQ_TOPIC.set(activeSpan, requiredInfo.getTopic());
    }
}
 
Example 4
Source File: AbstractMessageConsumeInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
    Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    List<MessageExt> msgs = (List<MessageExt>) allArguments[0];

    ContextCarrier contextCarrier = getContextCarrierFromMessage(msgs.get(0));
    AbstractSpan span = ContextManager.createEntrySpan(CONSUMER_OPERATION_NAME_PREFIX + msgs.get(0)
                                                                                            .getTopic() + "/Consumer", contextCarrier);

    span.setComponent(ComponentsDefine.ROCKET_MQ_CONSUMER);
    span.setLayer(SpanLayer.MQ);
    for (int i = 1; i < msgs.size(); i++) {
        ContextManager.extract(getContextCarrierFromMessage(msgs.get(i)));
    }

}
 
Example 5
Source File: Armeria085ServerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
                         final Class<?>[] argumentsTypes, final MethodInterceptResult result) {

    DefaultHttpRequest httpRequest = (DefaultHttpRequest) allArguments[1];
    HttpHeaders headers = httpRequest.headers();

    ContextCarrier carrier = new ContextCarrier();
    for (CarrierItem item = carrier.items(); item.hasNext(); ) {
        item = item.next();
        item.setHeadValue(headers.get(AsciiString.of(item.getHeadKey())));
    }

    AbstractSpan entrySpan = ContextManager.createEntrySpan(httpRequest.path(), carrier);
    entrySpan.setComponent(ComponentsDefine.ARMERIA);
    entrySpan.setLayer(SpanLayer.HTTP);
    entrySpan.setPeer(httpRequest.authority());
    Tags.URL.set(entrySpan, httpRequest.path());
    Tags.HTTP.METHOD.set(entrySpan, httpRequest.method().name());
}
 
Example 6
Source File: ServerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public <REQUEST, RESPONSE> ServerCall.Listener<REQUEST> interceptCall(ServerCall<REQUEST, RESPONSE> call,
    Metadata headers, ServerCallHandler<REQUEST, RESPONSE> handler) {
    final ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        String contextValue = headers.get(Metadata.Key.of(next.getHeadKey(), Metadata.ASCII_STRING_MARSHALLER));
        if (!StringUtil.isEmpty(contextValue)) {
            next.setHeadValue(contextValue);
        }
    }

    final AbstractSpan span = ContextManager.createEntrySpan(OperationNameFormatUtil.formatOperationName(call.getMethodDescriptor()), contextCarrier);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    try {
        return new TracingServerCallListener<>(handler.startCall(new TracingServerCall<>(call, ContextManager.capture()), headers), call
            .getMethodDescriptor(), ContextManager.capture());
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 7
Source File: ServerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    if (request.getKvAttachment() == null) {
        request.setKvAttachment(new HashMap<>());
    }
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue((String) request.getKvAttachment().get(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(generateOperationName(request), contextCarrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.BRPC_JAVA);
}
 
Example 8
Source File: SkywalkingTraceFactory.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void begin(final String name, final String component, final Map<String, String> tags) {
    Map<String, String> ctx = (Map<String, String>) invocation.removeAttachment(HIDDEN_KEY_TRACE_SKYWALKING);
    ContextCarrier contextCarrier = new ContextCarrier();
    if (ctx != null) {
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(ctx.get(next.getHeadKey()));
        }
    }
    span = ContextManager.createEntrySpan(name, contextCarrier);
    span.setComponent(new OfficialComponent(componentId, component));
    tag(tags);
    SpanLayer.asRPCFramework(span);
}
 
Example 9
Source File: TomcatInvokeInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
/**
 * * The {@link TraceSegment#refs} of current trace segment will reference to the trace segment id of the previous
 * level if the serialized context is not null.
 *
 * @param result change this result, if you want to truncate the method.
 */
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();

    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getHeader(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(request.getRequestURI(), contextCarrier);
    Tags.URL.set(span, request.getRequestURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    span.setComponent(ComponentsDefine.TOMCAT);
    SpanLayer.asHttp(span);

    if (Config.Plugin.Tomcat.COLLECT_HTTP_PARAMS) {
        collectHttpParam(request, span);
    }
}
 
Example 10
Source File: ServerConnectionHandleMessageInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    if (allArguments[0] instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) allArguments[0];
        ContextCarrier contextCarrier = new ContextCarrier();
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(request.headers().get(next.getHeadKey()));
            request.headers().remove(next.getHeadKey());
        }

        AbstractSpan span = ContextManager.createEntrySpan(toPath(request.getUri()), contextCarrier);
        span.setComponent(ComponentsDefine.VERTX);
        SpanLayer.asHttp(span);
        Tags.HTTP.METHOD.set(span, request.getMethod().toString());
        Tags.URL.set(span, request.getUri());

        objInst.setSkyWalkingDynamicField(new VertxContext(ContextManager.capture(), span.prepareForAsync()));
    }
}
 
Example 11
Source File: Struts2Interceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    HttpServletRequest request = ServletActionContext.getRequest();
    ContextCarrier contextCarrier = new ContextCarrier();

    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getHeader(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(request.getRequestURI(), contextCarrier);
    Tags.URL.set(span, request.getRequestURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    span.setComponent(ComponentsDefine.STRUTS2);
    SpanLayer.asHttp(span);
}
 
Example 12
Source File: AbstractMessageConsumeInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
    Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    List<MessageExt> msgs = (List<MessageExt>) allArguments[0];

    ContextCarrier contextCarrier = getContextCarrierFromMessage(msgs.get(0));
    AbstractSpan span = ContextManager.createEntrySpan(CONSUMER_OPERATION_NAME_PREFIX + msgs.get(0)
                                                                                            .getTopic() + "/Consumer", contextCarrier);

    span.setComponent(ComponentsDefine.ROCKET_MQ_CONSUMER);
    SpanLayer.asMQ(span);
    for (int i = 1; i < msgs.size(); i++) {
        ContextManager.extract(getContextCarrierFromMessage(msgs.get(i)));
    }

}
 
Example 13
Source File: TraceSegmentServiceClientTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendTraceSegmentWithException() throws InvalidProtocolBufferException {
    grpcServerRule.getServiceRegistry().addService(serviceImplBase);

    AbstractSpan firstEntrySpan = ContextManager.createEntrySpan("/testFirstEntry", null);
    firstEntrySpan.setComponent(ComponentsDefine.TOMCAT);
    Tags.HTTP.METHOD.set(firstEntrySpan, "GET");
    Tags.URL.set(firstEntrySpan, "127.0.0.1:8080");
    SpanLayer.asHttp(firstEntrySpan);
    ContextManager.stopSpan();
    grpcServerRule.getServer().shutdownNow();
    serviceClient.consume(storage.getTraceSegments());

    assertThat(upstreamSegments.size(), is(0));

    boolean reconnect = Whitebox.getInternalState(
        ServiceManager.INSTANCE.findService(GRPCChannelManager.class), "reconnect");
    assertThat(reconnect, is(true));

}
 
Example 14
Source File: TraceSegmentServiceClientTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendTraceSegmentWithoutException() throws InvalidProtocolBufferException {
    grpcServerRule.getServiceRegistry().addService(serviceImplBase);

    AbstractSpan firstEntrySpan = ContextManager.createEntrySpan("/testFirstEntry", null);
    firstEntrySpan.setComponent(ComponentsDefine.TOMCAT);
    Tags.HTTP.METHOD.set(firstEntrySpan, "GET");
    Tags.URL.set(firstEntrySpan, "127.0.0.1:8080");
    SpanLayer.asHttp(firstEntrySpan);
    ContextManager.stopSpan();

    serviceClient.consume(storage.getTraceSegments());

    assertThat(upstreamSegments.size(), is(1));
    SegmentObject traceSegmentObject = upstreamSegments.get(0);
    assertThat(traceSegmentObject.getSpans(0).getRefsCount(), is(0));
    assertThat(traceSegmentObject.getSpansCount(), is(1));

    SpanObject spanObject = traceSegmentObject.getSpans(0);
    assertThat(spanObject.getSpanType(), is(SpanType.Entry));
    assertThat(spanObject.getSpanId(), is(0));
    assertThat(spanObject.getParentSpanId(), is(-1));
}
 
Example 15
Source File: ServerTracingFilterInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethodImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects, Class<?>[] classes,
                             MethodInterceptResult methodInterceptResult) throws Throwable {
    AbstractSpan span = null;
    if (Contexts.broadcast().contains(SWContextCarrier$.MODULE$)) {
        SWContextCarrier swContextCarrier = Contexts.broadcast().apply(SWContextCarrier$.MODULE$);
        span = ContextManager.createEntrySpan(swContextCarrier.getOperationName(), swContextCarrier.getCarrier());
    } else {
        span = ContextManager.createEntrySpan("unknown", null);
    }

    span.setComponent(FINAGLE);
    SpanLayer.asRPCFramework(span);

    getLocalContextHolder().let(FinagleCtxs.SW_SPAN, span);
}
 
Example 16
Source File: Armeria084ServerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
    final Class<?>[] argumentsTypes, final MethodInterceptResult result) throws Throwable {

    DefaultHttpRequest httpRequest = (DefaultHttpRequest) allArguments[1];
    HttpHeaders headers = httpRequest.headers();

    ContextCarrier carrier = new ContextCarrier();
    for (CarrierItem item = carrier.items(); item.hasNext(); ) {
        item = item.next();
        item.setHeadValue(headers.get(AsciiString.of(item.getHeadKey())));
    }

    AbstractSpan entrySpan = ContextManager.createEntrySpan(httpRequest.path(), carrier);
    entrySpan.setComponent(ComponentsDefine.ARMERIA);
    entrySpan.setLayer(SpanLayer.HTTP);
    entrySpan.setPeer(httpRequest.authority());
    Tags.URL.set(entrySpan, httpRequest.path());
    Tags.HTTP.METHOD.set(entrySpan, httpRequest.method().name());
}
 
Example 17
Source File: DispatcherHandlerHandleMethodInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) throws Throwable {
    EnhancedInstance instance = getInstance(allArguments[0]);

    ServerWebExchange exchange = (ServerWebExchange) allArguments[0];

    ContextCarrier carrier = new ContextCarrier();
    CarrierItem next = carrier.items();
    HttpHeaders headers = exchange.getRequest().getHeaders();
    while (next.hasNext()) {
        next = next.next();
        List<String> header = headers.get(next.getHeadKey());
        if (header != null && header.size() > 0) {
            next.setHeadValue(header.get(0));
        }
    }

    AbstractSpan span = ContextManager.createEntrySpan(exchange.getRequest().getURI().getPath(), carrier);
    span.setComponent(ComponentsDefine.SPRING_WEBFLUX);
    SpanLayer.asHttp(span);
    Tags.URL.set(span, exchange.getRequest().getURI().toString());
    HTTP.METHOD.set(span, exchange.getRequest().getMethodValue());
    instance.setSkyWalkingDynamicField(ContextManager.capture());
    span.prepareForAsync();
    ContextManager.stopSpan(span);

    objInst.setSkyWalkingDynamicField(span);
}
 
Example 18
Source File: MotanProviderInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getAttachments().get(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(generateViewPoint(request), contextCarrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.MOTAN);
}
 
Example 19
Source File: NettySocketIOOnEventInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    String eventName = (String) allArguments[1];

    // no place to get context carrier
    ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan span = ContextManager.createEntrySpan("SocketIO/" + eventName + "/receive", contextCarrier);
    span.setComponent(ComponentsDefine.SOCKET_IO);
    SpanLayer.asHttp(span);
}
 
Example 20
Source File: NettySocketIOConnectionInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    SocketIOClient client = (SocketIOClient) allArguments[0];

    AbstractSpan span = ContextManager.createEntrySpan("SocketIO/" + method.getName(), new ContextCarrier());
    span.setComponent(ComponentsDefine.SOCKET_IO);
    SpanLayer.asHttp(span);

    // set client addr
    InetSocketAddress remoteAddress = (InetSocketAddress) client.getRemoteAddress();
    String clientAddress = remoteAddress.getAddress().getHostAddress() + ":" + remoteAddress.getPort();
    span.tag(Tags.ofKey("from"), clientAddress);
}