Java Code Examples for net.bytebuddy.asm.Advice#Argument

The following examples show how to use net.bytebuddy.asm.Advice#Argument . 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: GrailsTransactionNameInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
static void setTransactionName(@Advice.Argument(2) Object handler) {
    if (tracer == null) {
        return;
    }
    final Transaction transaction = tracer.currentTransaction();
    if (transaction == null) {
        return;
    }
    final String className;
    final String methodName;
    if (handler instanceof GrailsControllerUrlMappingInfo) {
        GrailsControllerUrlMappingInfo urlMappingInfo = (GrailsControllerUrlMappingInfo) handler;
        GrailsControllerClass grailsControllerClass = urlMappingInfo.getControllerClass();
        className = grailsControllerClass.getShortName();
        String actionName = urlMappingInfo.getActionName();
        methodName = actionName != null ? actionName : INDEX_ACTION;
    } else {
        className = handler.getClass().getSimpleName();
        methodName = null;
    }
    setName(transaction, className, methodName);
}
 
Example 2
Source File: ServletVersionInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
@SuppressWarnings("Duplicates") // duplication is fine here as it allows to inline code
private static void onEnter(@Advice.Argument(0) @Nullable ServletConfig servletConfig) {
    if (alreadyLogged) {
        return;
    }
    alreadyLogged = true;

    int majorVersion = -1;
    int minorVersion = -1;
    String serverInfo = null;
    if (servletConfig != null) {
        ServletContext servletContext = servletConfig.getServletContext();
        if (null != servletContext) {
            majorVersion = servletContext.getMajorVersion();
            minorVersion = servletContext.getMinorVersion();
            serverInfo = servletContext.getServerInfo();
        }
    }

    logger.info("Servlet container info = {}", serverInfo);
    if (majorVersion < 3) {
        logger.warn("Unsupported servlet version detected: {}.{}, no Servlet transaction will be created", majorVersion, minorVersion);
    }
}
 
Example 3
Source File: CodeTemplates.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Advice.OnMethodExit
static void $$_hibernate_areCollectionFieldsDirty(
		@FieldName String fieldName,
		@FieldValue Map<?, ?> map,
		@Advice.Argument(0) DirtyTracker tracker,
		@Advice.FieldValue(EnhancerConstants.TRACKER_COLLECTION_NAME) CollectionTracker $$_hibernate_collectionTracker) {
	if ( $$_hibernate_collectionTracker != null ) {
		if ( map == null && $$_hibernate_collectionTracker.getSize( fieldName ) != -1 ) {
			tracker.add( fieldName );
		}
		else if ( map != null && $$_hibernate_collectionTracker.getSize( fieldName ) != map.size() ) {
			tracker.add( fieldName );
		}
	}
}
 
Example 4
Source File: ChannelInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onEnter(@Advice.This Channel channel,
                            @Advice.Argument(0) MethodDescriptor<?, ?> method,
                            @Advice.Local("span") Span span) {

    if (tracer == null || grpcHelperManager == null) {
        return;
    }

    GrpcHelper helper = grpcHelperManager.getForClassLoaderOfClass(ClientCall.class);
    if (helper != null) {
        span = helper.startSpan(tracer.getActive(), method, channel.authority());
    }

}
 
Example 5
Source File: ServerCallHandlerInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onEnter(@Advice.Origin Class<?> clazz,
                            @Advice.Argument(0) ServerCall<?, ?> serverCall,
                            @Advice.Argument(1) Metadata headers,
                            @Advice.Local("transaction") Transaction transaction) {

    if (tracer == null || grpcHelperManager == null) {
        return;
    }

    GrpcHelper helper = grpcHelperManager.getForClassLoaderOfClass(ServerCall.class);
    if (helper != null) {
        transaction = helper.startTransaction(tracer, clazz.getClassLoader(), serverCall, headers);
    }
}
 
Example 6
Source File: ServerCallInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
private static void onExit(@Advice.Thrown @Nullable Throwable thrown,
                           @Advice.This ServerCall<?,?> serverCall,
                           @Advice.Argument(0) Status status) {

    if (tracer == null || grpcHelperManager == null) {
        return;
    }

    GrpcHelper helper = grpcHelperManager.getForClassLoaderOfClass(ServerCall.class);
    if (helper != null) {
        helper.setTransactionStatus(status, thrown, serverCall);
    }
}
 
Example 7
Source File: ApmSpanBuilderInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class)
public static void createSpan(@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) @Nullable AbstractSpan<?> parentContext,
                              @Advice.Origin Class<?> spanBuilderClass,
                              @Advice.FieldValue(value = "tags") Map<String, Object> tags,
                              @Advice.FieldValue(value = "operationName") String operationName,
                              @Advice.FieldValue(value = "microseconds") long microseconds,
                              @Advice.Argument(1) @Nullable Iterable<Map.Entry<String, String>> baggage,
                              @Advice.Return(readOnly = false) Object span) {
    span = doCreateTransactionOrSpan(parentContext, tags, operationName, microseconds, baggage, spanBuilderClass.getClassLoader());
}
 
Example 8
Source File: SpymemcachedAgentRule.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(final @Advice.Thrown Throwable thrown, final @Advice.Argument(value = 1) Object callback, final @Advice.Argument(value = 2, optional = true) Object callback2) {
  if (thrown != null) {
    if (callback2 != null)
      SpymemcachedAgentIntercept.exception(thrown, callback2);
    else
      SpymemcachedAgentIntercept.exception(thrown, callback);
  }
}
 
Example 9
Source File: OkHttpClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeEnqueue(@Advice.Origin Class<? extends Call> clazz,
                                    @Advice.FieldValue(value = "originalRequest", typing = Assigner.Typing.DYNAMIC, readOnly = false) @Nullable Request originalRequest,
                                    @Advice.Argument(value = 0, readOnly = false) @Nullable Callback callback,
                                    @Advice.Local("span") Span span) {
    if (tracer == null || tracer.getActive() == null || callbackWrapperCreator == null) {
        return;
    }

    final WrapperCreator<Callback> wrapperCreator = callbackWrapperCreator.getForClassLoaderOfClass(clazz);
    if (originalRequest == null || callback == null || wrapperCreator == null) {
        return;
    }

    final AbstractSpan<?> parent = tracer.getActive();

    Request request = originalRequest;
    URL url = request.url();
    span = HttpClientHelper.startHttpClientSpan(parent, request.method(), url.toString(), url.getProtocol(),
        OkHttpClientHelper.computeHostName(url.getHost()), url.getPort());
    if (span != null) {
        span.activate();
        if (headerSetterHelperManager != null) {
            TextHeaderSetter<Request.Builder> headerSetter = headerSetterHelperManager.getForClassLoaderOfClass(Request.class);
            if (headerSetter != null) {
                Request.Builder builder = originalRequest.newBuilder();
                span.propagateTraceContext(builder, headerSetter);
                originalRequest = builder.build();
            }
        }
        callback = wrapperCreator.wrap(callback, span);
    }
}
 
Example 10
Source File: StatementInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void storeSql(@Advice.This Statement statement, @Advice.Argument(0) String sql) {
    if (jdbcHelperManager == null) {
        return;
    }

    JdbcHelper helperImpl = jdbcHelperManager.getForClassLoaderOfClass(Statement.class);
    if (helperImpl != null) {
        helperImpl.mapStatementToSql(statement, sql);
    }
}
 
Example 11
Source File: DubboRpcAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Argument(value = 1) Object key, @Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returned) {
  if (key instanceof String && ("service.filter".equals(key) || "reference.filter".equals(key)) && isAllowed(className, origin))
    returned = DubboRpcAgentIntercept.exit(returned);
}
 
Example 12
Source File: PulsarClientAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodEnter
public static void enter(final @ClassName String className, final @Advice.Origin String origin, final @Advice.This Object thiz, final @Advice.Argument(value = 0) Object message) {
  if (isAllowed(className, origin))
    PulsarClientAgentIntercept.internalSendAsyncEnter(thiz, message);
}
 
Example 13
Source File: SpymemcachedAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(final @Advice.Thrown Throwable thrown, final @Advice.Argument(value = 2) Object callback) {
  if (thrown != null)
    SpymemcachedAgentIntercept.exception(thrown, callback);
}
 
Example 14
Source File: ExecutorInstrumentation.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
private static void onExit(@Nullable @Advice.Thrown Throwable thrown,
                           @Advice.Argument(value = 0) @Nullable Callable<?> callable) {
    JavaConcurrent.doFinally(thrown, callable);
}
 
Example 15
Source File: FilterAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodEnter
public static void enter(final @ClassName String className, final @Advice.Origin String origin, final @Advice.This Object thiz, final @Advice.Argument(value = 0) Object servletConfig) {
  if (isAllowed(className, origin))
    ServletAgentIntercept.init(thiz, servletConfig);
}
 
Example 16
Source File: KafkaProducerHeadersInstrumentation.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unused", "DuplicatedCode", "ParameterCanBeLocal"})
@Advice.OnMethodEnter(suppress = Throwable.class)
@Nullable
public static Span beforeSend(@Advice.FieldValue("apiVersions") final ApiVersions apiVersions,
                              @Advice.Argument(0) final ProducerRecord record,
                              @Advice.Local("helper") @Nullable KafkaInstrumentationHelper<Callback, ProducerRecord, KafkaProducer> helper,
                              @Nullable @Advice.Argument(value = 1, readOnly = false) Callback callback) {
    if (tracer == null) {
        return null;
    }
    Span span = null;

    //noinspection ConstantConditions
    helper = kafkaInstrHelperManager.getForClassLoaderOfClass(KafkaProducer.class);

    if (helper != null) {
        span = helper.onSendStart(record);
    }
    if (span == null) {
        return null;
    }

    // Avoid adding headers to records sent to a version older than 0.11.0 - see specifications in
    // https://kafka.apache.org/0110/documentation.html#messageformat
    if (apiVersions.maxUsableProduceMagic() >= RecordBatch.MAGIC_VALUE_V2 && headersSupported) {
        try {
            //noinspection ConstantConditions
            KafkaInstrumentationHeadersHelper<ConsumerRecord, ProducerRecord> kafkaInstrumentationHelper =
                kafkaInstrHeadersHelperManager.getForClassLoaderOfClass(KafkaProducer.class);
            if (kafkaInstrumentationHelper != null) {
                kafkaInstrumentationHelper.setOutgoingTraceContextHeaders(span, record);
            }
        } catch (final IllegalStateException e) {
            // headers are in a read-only state
            logger.debug("Failed to add header to Kafka record {}, probably to headers' read-only state.", record);
        }
    }

    //noinspection UnusedAssignment
    callback = helper.wrapCallback(callback, span);
    return span;
}
 
Example 17
Source File: SpymemcachedAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodEnter
public static void enter(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Argument(value = 0) Object key, @Advice.Argument(value = 1, typing = Typing.DYNAMIC, readOnly = false) Object callback) {
  if (isAllowed(className, origin))
    callback = SpymemcachedAgentIntercept.get(key, callback);
}
 
Example 18
Source File: AbstractSpanInstrumentation.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@VisibleForAdvice
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void setName(@Advice.FieldValue(value = "span", typing = Assigner.Typing.DYNAMIC) AbstractSpan<?> context,
                           @Advice.Argument(0) String name) {
    context.withName(name, PRIO_USER_SUPPLIED);
}
 
Example 19
Source File: JdbcAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodEnter
public static void enter(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Argument(value = 1) Class<?> caller) throws Exception {
  if (isAllowed(className, origin))
    JdbcAgentIntercept.isDriverAllowed(caller);
}
 
Example 20
Source File: SpymemcachedAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(final @Advice.Thrown Throwable thrown, final @Advice.Argument(value = 5) Object callback) {
  if (thrown != null)
    SpymemcachedAgentIntercept.exception(thrown, callback);
}