|
| 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.solon; |
| 20 | + |
| 21 | +import lombok.extern.slf4j.Slf4j; |
| 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.v2.InstanceMethodsAroundInterceptorV2; |
| 30 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext; |
| 31 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 32 | +import org.apache.skywalking.apm.util.StringUtil; |
| 33 | +import org.noear.solon.core.NvMap; |
| 34 | +import org.noear.solon.core.handle.Context; |
| 35 | + |
| 36 | +import java.lang.reflect.Method; |
| 37 | + |
| 38 | +@Slf4j |
| 39 | +public class SolonActionExecuteInterceptor implements InstanceMethodsAroundInterceptorV2 { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInvocationContext context) throws Throwable { |
| 43 | + Context ctx = (Context) allArguments[0]; |
| 44 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 45 | + CarrierItem next = contextCarrier.items(); |
| 46 | + while (next.hasNext()) { |
| 47 | + next = next.next(); |
| 48 | + next.setHeadValue(ctx.header(next.getHeadKey())); |
| 49 | + } |
| 50 | + String operationName = ctx.method() + ":" + ctx.path(); |
| 51 | + AbstractSpan span = ContextManager.createEntrySpan(operationName, contextCarrier); |
| 52 | + span.setComponent(ComponentsDefine.SOLON_MVC); |
| 53 | + SpanLayer.asHttp(span); |
| 54 | + Tags.URL.set(span, ctx.url()); |
| 55 | + Tags.HTTP.METHOD.set(span, ctx.method()); |
| 56 | + if (SolonPluginConfig.Plugin.Solon.INCLUDE_HTTP_HEADERS != null && !SolonPluginConfig.Plugin.Solon.INCLUDE_HTTP_HEADERS.isEmpty()) { |
| 57 | + NvMap includeHeaders = new NvMap(); |
| 58 | + for (String header : SolonPluginConfig.Plugin.Solon.INCLUDE_HTTP_HEADERS) { |
| 59 | + String value = ctx.header(header); |
| 60 | + if (StringUtil.isNotBlank(value)) { |
| 61 | + includeHeaders.put(header, value); |
| 62 | + } |
| 63 | + } |
| 64 | + Tags.HTTP.HEADERS.set(span, includeHeaders.toString()); |
| 65 | + } |
| 66 | + if (SolonPluginConfig.Plugin.Solon.HTTP_BODY_LENGTH_THRESHOLD != 0) { |
| 67 | + String body = ctx.body(); |
| 68 | + if (StringUtil.isNotBlank(body)) { |
| 69 | + if (SolonPluginConfig.Plugin.Solon.HTTP_BODY_LENGTH_THRESHOLD > 0 && body.length() > SolonPluginConfig.Plugin.Solon.HTTP_BODY_LENGTH_THRESHOLD) { |
| 70 | + body = body.substring(0, SolonPluginConfig.Plugin.Solon.HTTP_BODY_LENGTH_THRESHOLD); |
| 71 | + } |
| 72 | + Tags.HTTP.BODY.set(span, body); |
| 73 | + } |
| 74 | + } |
| 75 | + if (SolonPluginConfig.Plugin.Solon.HTTP_PARAMS_LENGTH_THRESHOLD != 0) { |
| 76 | + String param = ctx.paramMap().toString(); |
| 77 | + if (SolonPluginConfig.Plugin.Solon.HTTP_PARAMS_LENGTH_THRESHOLD > 0 && param.length() > SolonPluginConfig.Plugin.Solon.HTTP_PARAMS_LENGTH_THRESHOLD) { |
| 78 | + param = param.substring(0, SolonPluginConfig.Plugin.Solon.HTTP_PARAMS_LENGTH_THRESHOLD); |
| 79 | + } |
| 80 | + Tags.HTTP.PARAMS.set(span, param); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret, MethodInvocationContext context) { |
| 86 | + Context ctx = (Context) allArguments[0]; |
| 87 | + Tags.HTTP_RESPONSE_STATUS_CODE.set(ContextManager.activeSpan(), ctx.status()); |
| 88 | + if (ctx.errors != null && context.getContext() == null) { |
| 89 | + AbstractSpan activeSpan = ContextManager.activeSpan(); |
| 90 | + activeSpan.errorOccurred(); |
| 91 | + activeSpan.log(ctx.errors); |
| 92 | + } |
| 93 | + ContextManager.stopSpan(); |
| 94 | + return ret; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t, MethodInvocationContext context) { |
| 99 | + AbstractSpan activeSpan = ContextManager.activeSpan(); |
| 100 | + activeSpan.errorOccurred(); |
| 101 | + activeSpan.log(t); |
| 102 | + context.setContext(true); |
| 103 | + } |
| 104 | +} |
0 commit comments