@Advice.OnMethodExit(onThrowable = Exception.class, inline = false) public static void meterException(@ExceptionMeteredSignature String signature, @MeterExceptionsFor Class<? extends Exception> cause, @Advice.Thrown Throwable e) { if (e != null && cause.isInstance(e)) { Stagemonitor.getMetric2Registry().meter(getMetricName(signature)).mark(); } }
/** * {@inheritDoc} */ public OffsetMapping make(ParameterDescription.InDefinedShape target, AnnotationDescription.Loadable<Thrown> annotation, AdviceType adviceType) { if (adviceType.isDelegation() && !annotation.loadSilent().readOnly()) { throw new IllegalStateException("Cannot use writable " + target + " on read-only parameter"); } else { return new ForThrowable(target.getType(), annotation.loadSilent()); } } }
@OnMethodExit(onThrowable = Throwable.class) public static void after( @Enter List<Object> hooks, @This(optional = true) Object that, @Origin Method method, @AllArguments Object[] args, @Return(typing = Assigner.Typing.DYNAMIC) Object returned, // support void == null and int == Integer @Thrown Throwable thrown ) { try { // The following code is equivalent to: // Delegator.after(hooks, method, args); // However, the Delegator class will not be available in the context of the instrumented method, // so we must use our agent class loader to load the Delegator class and do the call via reflection. Class<?> delegator = ClassLoaderCache.getInstance().currentClassLoader().loadClass("io.promagent.internal.Delegator"); Method afterMethod = delegator.getMethod("after", List.class, Method.class, Object[].class, Object.class, Throwable.class); afterMethod.invoke(null, hooks, method, args, returned, thrown); } catch (Exception e) { Class<?> clazz = that != null ? that.getClass() : method.getDeclaringClass(); System.err.println("Error executing Prometheus hook on " + clazz.getSimpleName()); e.printStackTrace(); } } }
@SuppressWarnings({"unchecked", "RedundantThrows"}) @Advice.OnMethodExit(onThrowable = Throwable.class) static void onMethodExit( @Advice.Local("exec") MethodExecution.Implementation exec, @Advice.Local("event") MethodEvent event, @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object returned, @Advice.Thrown(readOnly = false, typing = Assigner.Typing.DYNAMIC) Throwable thrown, @Advice.Enter boolean skipped ) throws Throwable { if (event == null) return; if (!skipped) { event.returned = returned; event.thrown = thrown; } exec._exit.accept(event); //noinspection UnusedAssignment returned = event.returned; //noinspection UnusedAssignment thrown = event.thrown; } }
/** * 如果需要返回值,在方法里添加注解和参数@Advice.Return(readOnly = false) Object result,result的类型要和实际返回值类型一致,需要修改参数readOnly置为false */ @Advice.OnMethodExit(onThrowable = Throwable.class) public static void exit(@Advice.Local("handler") IHandler handler, @Advice.Origin Method method, @Advice.This Object instance, @Advice.AllArguments Object[] allArguments, @Advice.Thrown Throwable t){ //TODO This is a demo ! } }
/** * 如果需要返回值,在方法里添加注解和参数@Advice.Return(readOnly = false) Object result,result的类型要和实际返回值类型一致,需要修改参数readOnly置为false */ @Advice.OnMethodExit(onThrowable = Throwable.class) public static void exit(@Advice.Local("handler") IHandler handler, @Advice.Origin Method method, @Advice.Origin Class<?> clazz, @Advice.AllArguments Object[] allArguments, @Advice.Thrown Throwable t){ //TODO This is a demo ! } }
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) public static void onMethodExit(@Nullable @Advice.Local("span") AbstractSpan<?> span, @Advice.Thrown Throwable t) { if (span != null) { span.captureException(t); span.deactivate().end(); } }
@Advice.OnMethodExit(onThrowable = Throwable.class, inline = false) public static void monitorStop(@Advice.Thrown Throwable exception) { final RequestMonitor requestMonitor = Stagemonitor.getPlugin(RequestMonitorPlugin.class).getRequestMonitor(); if (exception != null && exception instanceof Exception) { requestMonitor.recordException((Exception) exception); } requestMonitor.monitorStop(); }
@Advice.OnMethodExit(onThrowable = Throwable.class, inline = false) public static void monitorStop(@Advice.Thrown Throwable exception) { final RequestMonitor requestMonitor = Stagemonitor.getPlugin(TracingPlugin.class).getRequestMonitor(); if (exception != null && exception instanceof Exception) { requestMonitor.recordException((Exception) exception); } requestMonitor.monitorStop(); }
/** * Creates a new offset mapping for access of the exception that is thrown by the instrumented method.. * * @param target The type of parameter that is being accessed. * @param annotation The annotation to bind. */ protected ForThrowable(TypeDescription.Generic target, Thrown annotation) { this(target, annotation.readOnly(), annotation.typing()); }