@Override protected void activate(ActiveDescriptor<?> handle, Object service, Method method, Gauge annotation) { if (method.getParameterCount() != 0) { log.error("@Gauge placed on method {} which must have zero parameters, but has {}", method, method.getParameterCount()); return; } com.codahale.metrics.Gauge<?> gauge = () -> { try { return method.invoke(service); } catch (Exception e) { throw new RuntimeException("Failed to read gauge value from " + method, e instanceof InvocationTargetException ? ( (InvocationTargetException) e ).getTargetException() : e); } }; String name = metricNameService.getFormattedMetricName(method, com.codahale.metrics.Gauge.class); log.debug("Activating gauge {} monitoring {}", name, method); metricRegistry.register(name, gauge); }
private String metricName(Annotation annotation) { if (CachedGauge.class.isInstance(annotation)) return ((CachedGauge) annotation).name(); else if (Counted.class.isInstance(annotation)) return ((Counted) annotation).name(); else if (ExceptionMetered.class.isInstance(annotation)) return ((ExceptionMetered) annotation).name(); else if (Gauge.class.isInstance(annotation)) return ((Gauge) annotation).name(); else if (Metered.class.isInstance(annotation)) return ((Metered) annotation).name(); else if (Timed.class.isInstance(annotation)) return ((Timed) annotation).name(); else throw new IllegalArgumentException("Unsupported Metrics forMethod [" + annotation.getClass().getName() + "]"); }
private boolean isMetricAbsolute(Annotation annotation) { if (extension.getParameters().contains(MetricsParameter.useAbsoluteName)) return true; if (CachedGauge.class.isInstance(annotation)) return ((CachedGauge) annotation).absolute(); else if (Counted.class.isInstance(annotation)) return ((Counted) annotation).absolute(); else if (ExceptionMetered.class.isInstance(annotation)) return ((ExceptionMetered) annotation).absolute(); else if (Gauge.class.isInstance(annotation)) return ((Gauge) annotation).absolute(); else if (Metered.class.isInstance(annotation)) return ((Metered) annotation).absolute(); else if (Timed.class.isInstance(annotation)) return ((Timed) annotation).absolute(); else throw new IllegalArgumentException("Unsupported Metrics forMethod [" + annotation.getClass().getName() + "]"); }
@ExceptionMetered(name = "exception") @Gauge(name = "gauge") @Metered(name = "meter") @Timed(name = "timer") public static String metricsMethod() { return "value"; }
@ExceptionMetered(name = "exception") @Gauge(name = "gauge") @Metered(name = "meter") @Timed(name = "timer") public String metricsMethod() { return "value"; }
public static void monitorGauges(Object object) { for (final Method method : object.getClass().getDeclaredMethods()) { final Gauge gaugeAnnotation = method.getAnnotation(Gauge.class); // only create gauge, if method takes no parameters and is non-void if (gaugeAnnotation != null && methodTakesNoParamsAndIsNonVoid(method)) { method.setAccessible(true); final String signature = SignatureUtils.getSignature(object.getClass().getName(), method.getName(), gaugeAnnotation.name(), gaugeAnnotation.absolute()); registerGauge(object, method, signature); } } }
private static void registerGauge(final Object object, final Method method, final String signature) { Stagemonitor.getMetric2Registry().registerNewMetrics(name("gauge_" + signature).build(), new com.codahale.metrics.Gauge() { @Override public Object getValue() { try { return method.invoke(object); } catch (Exception e) { logger.warn("Error occurred while invoking gauge {}: {}", signature, e.getMessage()); return null; } } }); }
@Counted(name = "counter", monotonic = true) @ExceptionMetered(name = "exception") @Gauge(name = "gauge") @Metered(name = "meter") @Timed(name = "timer") public String metricsMethod() { return "value"; }
@Inject public GaugeAnnotationActivator(@NonNull MetricRegistry metricRegistry, @NonNull MetricNameService metricNameService) { super(Gauge.class); this.metricRegistry = metricRegistry; this.metricNameService = metricNameService; }
/** @return number of access tickets. */ @Gauge(name = "auth.sso.access_ticket_number") public int size() { return accessTickets.size(); }
@Gauge(name = "singleGaugeStaticMethod") public static long getSingleGauge() { return singleStaticGauge; }
@Gauge(name = "singleGaugeMethod") public long getSingleGauge() { return singleGauge; }
@Gauge(name = "publicGaugeMethod") public long getPublicGaugeMethod() { return publicGauge; }
@Gauge(name = "packagePrivateGaugeMethod") long getPackagePrivateGauge() { return packagePrivateGauge; }
@Gauge(name = "protectedGaugeMethod") protected long getProtectedGauge() { return protectedGauge; }
@Gauge(name = "privateGaugeMethod") private long getPrivateGauge() { return privateGauge; }
@Override @Gauge public int loggedInUsers() { return user2session.size(); }
@Gauge private int gaugeDefault() { return 1; }
@Gauge public static int staticGaugeDefault() { return 1; }
@Gauge(absolute = true) public void gaugeAbsolute() { }
@Gauge(name = "myGaugeName") public void gaugeName() { }
@Gauge(name = "myGaugeNameAbsolute", absolute = true) public void gaugeNameAbsolute() { }
@Gauge(name = "childGaugeMethod") public long getChildGauge() { return childGauge; }
@Gauge(name = "gaugeMethod") public long getGauge() { return gauge; }
private CachingGauge(com.codahale.metrics.Gauge<?> gauge, long timeout, TimeUnit timeoutUnit) { super(timeout, timeoutUnit); this.gauge = gauge; }
Of<Gauge> gauge(Class<?> topClass, Method method) { return resolverOf(topClass, method, Gauge.class); }
private <X> void metricsAnnotations(@Observes @WithAnnotations({CachedGauge.class, Counted.class, ExceptionMetered.class, Gauge.class, Metered.class, Timed.class}) ProcessAnnotatedType<X> pat) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(pat.getAnnotatedType(), METRICS_BINDING)); }