|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin.resteasy.v6.server; |
| 20 | + |
| 21 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 26 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 28 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 29 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 30 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 31 | +import org.jboss.resteasy.spi.HttpRequest; |
| 32 | +import org.jboss.resteasy.spi.HttpResponse; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import java.lang.reflect.Method; |
| 37 | + |
| 38 | +public class SynchronousDispatcherInterceptor implements InstanceMethodsAroundInterceptor { |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(SynchronousDispatcherInterceptor.class); |
| 40 | + |
| 41 | + @Override |
| 42 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 43 | + MethodInterceptResult result) throws Throwable { |
| 44 | + HttpRequest request = (HttpRequest) allArguments[0]; |
| 45 | + |
| 46 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 47 | + CarrierItem next = contextCarrier.items(); |
| 48 | + while (next.hasNext()) { |
| 49 | + next = next.next(); |
| 50 | + next.setHeadValue(request.getHttpHeaders().getHeaderString(next.getHeadKey())); |
| 51 | + } |
| 52 | + |
| 53 | + String operationName = request.getHttpMethod() + ":" + request.getUri().getPath(); |
| 54 | + AbstractSpan span = ContextManager.createEntrySpan(operationName, contextCarrier); |
| 55 | + span.tag(Tags.URL, toPath(request.getUri().getRequestUri().toString())); |
| 56 | + span.tag(Tags.HTTP.METHOD, request.getHttpMethod()); |
| 57 | + span.setComponent(ComponentsDefine.RESTEASY); |
| 58 | + SpanLayer.asHttp(span); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 63 | + Object ret) throws Throwable { |
| 64 | + if (!ContextManager.isActive()) { |
| 65 | + return ret; |
| 66 | + } |
| 67 | + HttpResponse response = (HttpResponse) allArguments[1]; |
| 68 | + AbstractSpan span = ContextManager.activeSpan(); |
| 69 | + if (response.getStatus() >= 400) { |
| 70 | + span.errorOccurred(); |
| 71 | + } |
| 72 | + Tags.HTTP_RESPONSE_STATUS_CODE.set(span, response.getStatus()); |
| 73 | + ContextManager.stopSpan(); |
| 74 | + return ret; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 79 | + Class<?>[] argumentsTypes, Throwable t) { |
| 80 | + if (ContextManager.isActive()) { |
| 81 | + ContextManager.activeSpan().log(t); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static String toPath(String uri) { |
| 86 | + int index = uri.indexOf("?"); |
| 87 | + if (index > -1) { |
| 88 | + return uri.substring(0, index); |
| 89 | + } else { |
| 90 | + return uri; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments