/* * Copyright 2016 Pinpoint contributors and NAVER Corp. * * Licensed 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 com.navercorp.pinpoint.plugin.jboss.interceptor; import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor; import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder; import com.navercorp.pinpoint.bootstrap.context.Trace; import com.navercorp.pinpoint.bootstrap.context.TraceContext; import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor; import com.navercorp.pinpoint.bootstrap.logging.PLogger; import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory; import com.navercorp.pinpoint.plugin.jboss.ContextInvocationMethodDescriptor; import com.navercorp.pinpoint.plugin.jboss.JbossConstants; /** * The Class ContextInvocationInterceptor. * * @author <a href="mailto:suraj.raturi89@gmail.com">Suraj Raturi</a> */ public class ContextInvocationInterceptor implements AroundInterceptor { /** The Constant CONTEXT_INVOCATION_API_TAG. */ public static final ContextInvocationMethodDescriptor CONTEXT_INVOCATION_API_TAG = new ContextInvocationMethodDescriptor(); /** The logger. */ private final PLogger logger = PLoggerFactory.getLogger(this.getClass()); /** The is debug. */ private final boolean isDebug = logger.isDebugEnabled(); /** The method descriptor. */ private final MethodDescriptor methodDescriptor; /** The trace context. */ private final TraceContext traceContext; /** * Instantiates a new invoke context interceptor. * * @param traceContext the trace context * @param descriptor the descriptor */ public ContextInvocationInterceptor(final TraceContext traceContext, final MethodDescriptor descriptor) { this.traceContext = traceContext; this.methodDescriptor = descriptor; traceContext.cacheApi(CONTEXT_INVOCATION_API_TAG); } /* * (non-Javadoc) * * @see com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor#before(java.lang.Object, java.lang.Object[]) */ @Override public void before(final Object target, final Object[] args) { if (isDebug) { logger.beforeInterceptor(target, args); } try { final Trace trace = traceContext.currentTraceObject(); if (trace == null) { return; } if (!trace.canSampled()) { return; } final SpanEventRecorder recorder = trace.traceBlockBegin(); recorder.recordServiceType(JbossConstants.JBOSS_METHOD); } catch (final Throwable th) { if (logger.isWarnEnabled()) { logger.warn("BEFORE. Caused:{}", th.getMessage(), th); } } } /* * (non-Javadoc) * * @see com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor#after(java.lang.Object, java.lang.Object[], * java.lang.Object, java.lang.Throwable) */ @Override public void after(final Object target, final Object[] args, final Object result, final Throwable throwable) { if (isDebug) { logger.afterInterceptor(target, args, result, throwable); } final Trace trace = traceContext.currentTraceObject(); if (trace == null) { return; } if (!trace.canSampled()) { traceContext.removeTraceObject(); return; } try { final SpanEventRecorder recorder = trace.currentSpanEventRecorder(); recorder.recordApi(methodDescriptor); recorder.recordException(throwable); } catch (final Throwable th) { if (logger.isWarnEnabled()) { logger.warn("AFTER. Caused:{}", th.getMessage(), th); } } finally { trace.traceBlockEnd(); } } }