|
| 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.jetty.v12.server; |
| 20 | + |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 26 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 28 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 29 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 30 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 31 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 32 | +import org.eclipse.jetty.http.HttpFields; |
| 33 | +import org.eclipse.jetty.http.HttpURI; |
| 34 | +import org.eclipse.jetty.server.Request; |
| 35 | +import org.eclipse.jetty.server.Response; |
| 36 | +import org.eclipse.jetty.util.Callback; |
| 37 | + |
| 38 | +/** |
| 39 | + * Enhances the core, namespace-neutral {@code org.eclipse.jetty.server.Server#handle(Request, Response, |
| 40 | + * Callback)} — the single top-level entry invoked once per request in Jetty 12. Handling is async, so |
| 41 | + * the entry span is prepared for async in beforeMethod, its synchronous segment ends in afterMethod, and |
| 42 | + * it is finished (with the response status) when the wrapped {@link SwCallback} completes. |
| 43 | + */ |
| 44 | +public class HandleInterceptor implements InstanceMethodsAroundInterceptor { |
| 45 | + |
| 46 | + private static final ThreadLocal<SwCallback> HOLDER = new ThreadLocal<SwCallback>(); |
| 47 | + |
| 48 | + @Override |
| 49 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 50 | + MethodInterceptResult result) throws Throwable { |
| 51 | + Request request = (Request) allArguments[0]; |
| 52 | + Response response = (Response) allArguments[1]; |
| 53 | + Callback callback = (Callback) allArguments[2]; |
| 54 | + |
| 55 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 56 | + HttpFields headers = request.getHeaders(); |
| 57 | + CarrierItem next = contextCarrier.items(); |
| 58 | + while (next.hasNext()) { |
| 59 | + next = next.next(); |
| 60 | + next.setHeadValue(headers.get(next.getHeadKey())); |
| 61 | + } |
| 62 | + |
| 63 | + HttpURI uri = request.getHttpURI(); |
| 64 | + AbstractSpan span = ContextManager.createEntrySpan(uri.getPath(), contextCarrier); |
| 65 | + Tags.URL.set(span, uri.asString()); |
| 66 | + Tags.HTTP.METHOD.set(span, request.getMethod()); |
| 67 | + span.setComponent(ComponentsDefine.JETTY_SERVER); |
| 68 | + SpanLayer.asHttp(span); |
| 69 | + span.prepareForAsync(); |
| 70 | + |
| 71 | + SwCallback swCallback = new SwCallback(callback, response, span); |
| 72 | + allArguments[2] = swCallback; |
| 73 | + HOLDER.set(swCallback); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 78 | + Object ret) throws Throwable { |
| 79 | + SwCallback swCallback = HOLDER.get(); |
| 80 | + HOLDER.remove(); |
| 81 | + ContextManager.stopSpan(); |
| 82 | + // ret == true: handled; the wrapped callback fires on response completion and finishes the span. |
| 83 | + // ret == false: not handled — Jetty writes 404 on the original callback, bypassing ours. |
| 84 | + // ret == null: handle() threw (see handleMethodException) — Jetty writes 500. |
| 85 | + // For the latter two the wrapped callback never fires and the status is written only after |
| 86 | + // handle() returns, so finish here with the status Jetty's contract guarantees. |
| 87 | + if (swCallback != null && !Boolean.TRUE.equals(ret)) { |
| 88 | + swCallback.finishWithStatus(ret == null ? 500 : 404); |
| 89 | + } |
| 90 | + return ret; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 95 | + Class<?>[] argumentsTypes, Throwable t) { |
| 96 | + AbstractSpan span = ContextManager.activeSpan(); |
| 97 | + span.log(t); |
| 98 | + span.errorOccurred(); |
| 99 | + } |
| 100 | +} |
0 commit comments