protected void readMeasurement(String name, Setter setter) { MeasurementNode node = tree.findChild(name); if (node == null) { return; } for (Measurement measurement : node.getMeasurements()) { String threadPoolName = Utils.getTagValue(measurement.id(), ThreadPoolMonitor.ID_TAG_NAME); if (threadPoolName == null) { continue; } ThreadPoolPublishModel model = threadPools.computeIfAbsent(threadPoolName, tpn -> { return new ThreadPoolPublishModel(); }); setter.set(model, measurement); } } }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param k * Key to search for. * @param v * Value that should be associated with k on the ids. * @return * Measurement or null if no matches are found. */ public static Measurement first(final Iterable<Measurement> ms, final String k, final String v) { return first(ms, value -> v.equals(getTagValue(value.id(), k))); }
/** Return the number of inflight requests associated with the given id. */ AtomicInteger inflightRequests(Id id) { return Utils.computeIfAbsent(inflightRequests, id, i -> new AtomicInteger()); }
/** * Returns a new iterable restricted to measurements that match the predicate. * * @param ms * A set of measurements. * @param k * Key to search for. * @param v * Value that should be associated with k on the ids. * @return * Measurements matching the predicate. */ public static Iterable<Measurement> filter( final Iterable<Measurement> ms, final String k, final String v) { return filter(ms, value -> v.equals(getTagValue(value.id(), k))); }
/** * Create a new instance. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Counter instance. */ public static IntervalCounter get(Registry registry, Id id) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, id, i -> new IntervalCounter(registry, i)); if (!(c instanceof IntervalCounter)) { Utils.propagateTypeError(registry, id, IntervalCounter.class, c.getClass()); c = new IntervalCounter(new NoopRegistry(), id); } return (IntervalCounter) c; }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param t * The key and value to search for. * @return * Measurement or null if no matches are found. */ public static Measurement first(Iterable<Measurement> ms, Tag t) { return first(ms, t.key(), t.value()); }
/** Return a method supplying a value for a gauge. */ static Method getGaugeMethod(Registry registry, Id id, Object obj, String method) { try { final Method m = Utils.getMethod(obj.getClass(), method); try { // Make sure we can cast the response to a Number final Number n = (Number) m.invoke(obj); REGISTRY_LOGGER.debug( "registering gauge {}, using method [{}], with initial value {}", id, m, n); return m; } catch (Exception e) { final String msg = "exception thrown invoking method [" + m + "], skipping registration of gauge " + id; registry.propagate(msg, e); } } catch (NoSuchMethodException e) { final String mname = obj.getClass().getName() + "." + method; final String msg = "invalid method [" + mname + "], skipping registration of gauge " + id; registry.propagate(msg, e); } return null; }
/** * Returns the first measurement that matches the predicate. * * @param ms * A set of measurements. * @param p * Predicate to use for selecting values. * @return * Measurement or null if no matches are found. */ public static Measurement first(Iterable<Measurement> ms, Predicate<Measurement> p) { Iterator<Measurement> it = filter(ms, p).iterator(); return it.hasNext() ? it.next() : null; }
/** Create a new instance. */ public MetricsRegistry(Clock clock, com.codahale.metrics.MetricRegistry impl) { this(clock, impl, id -> { Id normalized = Utils.normalize(id); StringBuilder buf = new StringBuilder(); buf.append(normalized.name()); for (Tag t : normalized.tags()) { buf.append('.').append(t.key()).append('-').append(t.value()); } return buf.toString(); }); }
/** * Returns a new iterable restricted to measurements that match the predicate. * * @param ms * A set of measurements. * @param k * Key to search for. * @param v * Value that should be associated with k on the ids. * @return * Measurements matching the predicate. */ public static Iterable<Measurement> filter( final Iterable<Measurement> ms, final String k, final String v) { return filter(ms, value -> v.equals(getTagValue(value.id(), k))); }
/** * Create a new instance. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Counter instance. */ public static IntervalCounter get(Registry registry, Id id) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, id, i -> new IntervalCounter(registry, i)); if (!(c instanceof IntervalCounter)) { Utils.propagateTypeError(registry, id, IntervalCounter.class, c.getClass()); c = new IntervalCounter(new NoopRegistry(), id); } return (IntervalCounter) c; }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param t * The key and value to search for. * @return * Measurement or null if no matches are found. */ public static Measurement first(Iterable<Measurement> ms, Tag t) { return first(ms, t.key(), t.value()); }
/** Return a method supplying a value for a gauge. */ static Method getGaugeMethod(Registry registry, Id id, Object obj, String method) { try { final Method m = Utils.getMethod(obj.getClass(), method); try { // Make sure we can cast the response to a Number final Number n = (Number) m.invoke(obj); REGISTRY_LOGGER.debug( "registering gauge {}, using method [{}], with initial value {}", id, m, n); return m; } catch (Exception e) { final String msg = "exception thrown invoking method [" + m + "], skipping registration of gauge " + id; registry.propagate(msg, e); } } catch (NoSuchMethodException e) { final String mname = obj.getClass().getName() + "." + method; final String msg = "invalid method [" + mname + "], skipping registration of gauge " + id; registry.propagate(msg, e); } return null; }
/** * Returns the first measurement that matches the predicate. * * @param ms * A set of measurements. * @param p * Predicate to use for selecting values. * @return * Measurement or null if no matches are found. */ public static Measurement first(Iterable<Measurement> ms, Predicate<Measurement> p) { Iterator<Measurement> it = filter(ms, p).iterator(); return it.hasNext() ? it.next() : null; }
/** Create a new instance. */ public MetricsRegistry(Clock clock, com.codahale.metrics.MetricRegistry impl) { this(clock, impl, id -> { Id normalized = Utils.normalize(id); StringBuilder buf = new StringBuilder(); buf.append(normalized.name()); for (Tag t : normalized.tags()) { buf.append('.').append(t.key()).append('-').append(t.value()); } return buf.toString(); }); }
private static boolean isPercentile(Id id) { final String stat = Utils.getTagValue(id, "statistic"); return "percentile".equals(stat); }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param k * Key to search for. * @param v * Value that should be associated with k on the ids. * @return * Measurement or null if no matches are found. */ public static Measurement first(final Iterable<Measurement> ms, final String k, final String v) { return first(ms, value -> v.equals(getTagValue(value.id(), k))); }
/** * Provided for backwards compatibility to support the {@link Registry#register(Meter)} * method. Use the builder created with {@link #using(Registry)} instead. * * @deprecated This method only exists to allow for backwards compatibility and should * be considered an internal detail. Scheduled to be removed in 2.0. */ @Deprecated public static void monitorMeter(Registry registry, Meter meter) { ConcurrentMap<Id, Object> state = registry.state(); Object c = Utils.computeIfAbsent(state, meter.id(), MeterState::new); if (!(c instanceof MeterState)) { Utils.propagateTypeError(registry, meter.id(), MeterState.class, c.getClass()); } else { MeterState t = (MeterState) c; t.add(meter); long delay = registry.config().gaugePollingFrequency().toMillis(); t.schedule(registry, null, delay); } }