public Counter getCounter(MetricName metricName, MetricMatcher metricMatcher) { // use delta counters instead of regular counters. It helps with load balancers present in // front of proxy (PUB-125) MetricName newMetricName = DeltaCounter.getDeltaCounterMetricName(metricName); metricNamesForMetricMatchers.get(metricMatcher).add(newMetricName); return (Counter) metricCache.get(newMetricName, key -> DeltaCounter.get(metricsRegistry, newMetricName)); }
/** * A helper function to transform a counter name to a DeltaCounter name. The transformation * includes prepending a "\u2206" character to the name. * * @param name The name which needs to be transformed * @return The new name representing a DeltaCounter */ public static String getDeltaCounterName(String name) { if (!isDelta(name)) { return MetricConstants.DELTA_PREFIX + name; } else { return name; } }
/** * This method returns the current count of the DeltaCounter and resets the counter. * * @param counter The DeltaCounter whose value is requested * @return The current count of the DeltaCounter */ public static long processDeltaCounter(DeltaCounter counter) { long count = counter.count(); counter.dec(count); return count; }
public static TimeSeries fromMetricName(MetricName metricName) throws IOException { String name = metricName.getName(); // check if it is a delta counter, then remove the delta prefix before parsing // (delta prefix had to be removed, because the name contains json and jackson parser fails // to parse the delta prefix) boolean deltaCounter = DeltaCounter.isDelta(name); if (deltaCounter) { name = DeltaCounter.getNameWithoutDeltaPrefix(name); } Decoder decoder = DecoderFactory.get().jsonDecoder(TimeSeries.SCHEMA$, name); TimeSeries toReturn = datumReader.read(null, decoder); // add the delta prefix back if (deltaCounter) { String newName = DeltaCounter.getDeltaCounterName(toReturn.getMetric()); toReturn.setMetric(newName); } return toReturn; }
/** * This method transforms the MetricName into a new MetricName that represents a DeltaCounter. * The transformation includes prepending a "\u2206" character to the name. * * @param metricName The MetricName which needs to be transformed * @return The new MetricName representing a DeltaCounter */ public static MetricName getDeltaCounterMetricName(MetricName metricName) { if (isDelta(metricName.getName())) { return metricName; } else { String name = getDeltaCounterName(metricName.getName()); return new MetricName(metricName.getGroup(), metricName.getType(), name, metricName.getScope()); } }
/** * A static factory method to create an instance of Delta Counter. * * @param registry The MetricRegistry to use * @param metricName The MetricName to use * @return An instance of DeltaCounter */ @VisibleForTesting public static synchronized DeltaCounter get(MetricsRegistry registry, MetricName metricName) { if (registry == null || metricName == null || metricName.getName().isEmpty()) { throw new IllegalArgumentException("Invalid arguments"); } DeltaCounter counter = new DeltaCounter(); MetricName newMetricName = getDeltaCounterMetricName(metricName); registry.getOrAdd(newMetricName, counter); return counter; }
@Override public void processCounter(MetricName name, Counter counter, FlushProcessorContext context) throws Exception { long count; // handle delta counter if (counter instanceof DeltaCounter) { count = DeltaCounter.processDeltaCounter((DeltaCounter) counter); } else { count = counter.count(); } context.report(count); sentCounter.inc(); }
/** * A static factory method to create an instance of DeltaCounter. It uses the default * MetricsRegistry. * * @param metricName The MetricName to use * @return An instance of DeltaCounter */ public static DeltaCounter get(MetricName metricName) { return get(Metrics.defaultRegistry(), metricName); }
public static TimeSeries fromMetricName(MetricName metricName) throws IOException { String name = metricName.getName(); // check if it is a delta counter, then remove the delta prefix before parsing // (delta prefix had to be removed, because the name contains json and jackson parser fails // to parse the delta prefix) boolean deltaCounter = DeltaCounter.isDelta(name); if (deltaCounter) { name = DeltaCounter.getNameWithoutDeltaPrefix(name); } Decoder decoder = DecoderFactory.get().jsonDecoder(TimeSeries.SCHEMA$, name); TimeSeries toReturn = datumReader.read(null, decoder); // add the delta prefix back if (deltaCounter) { String newName = DeltaCounter.getDeltaCounterName(toReturn.getMetric()); toReturn.setMetric(newName); } return toReturn; }
/** * This method transforms the MetricName into a new MetricName that represents a DeltaCounter. * The transformation includes prepending a "\u2206" character to the name. * * @param metricName The MetricName which needs to be transformed * @return The new MetricName representing a DeltaCounter */ public static MetricName getDeltaCounterMetricName(MetricName metricName) { if (isDelta(metricName.getName())) { return metricName; } else { String name = getDeltaCounterName(metricName.getName()); return new MetricName(metricName.getGroup(), metricName.getType(), name, metricName.getScope()); } }
/** * A static factory method to create an instance of Delta Counter. * * @param registry The MetricRegistry to use * @param metricName The MetricName to use * @return An instance of DeltaCounter */ @VisibleForTesting public static synchronized DeltaCounter get(MetricsRegistry registry, MetricName metricName) { if (registry == null || metricName == null || metricName.getName().isEmpty()) { throw new IllegalArgumentException("Invalid arguments"); } DeltaCounter counter = new DeltaCounter(); MetricName newMetricName = getDeltaCounterMetricName(metricName); registry.getOrAdd(newMetricName, counter); return counter; }
@Override public void processCounter(MetricName name, Counter counter, FlushProcessorContext context) throws Exception { long count; // handle delta counter if (counter instanceof DeltaCounter) { count = DeltaCounter.processDeltaCounter((DeltaCounter) counter); } else { count = counter.count(); } context.report(count); sentCounter.inc(); }
/** * A static factory method to create an instance of DeltaCounter. It uses the default * MetricsRegistry. * * @param metricName The MetricName to use * @return An instance of DeltaCounter */ public static DeltaCounter get(MetricName metricName) { return get(Metrics.defaultRegistry(), metricName); }
public Counter getCounter(MetricName metricName, MetricMatcher metricMatcher) { // use delta counters instead of regular counters. It helps with load balancers present in // front of proxy (PUB-125) MetricName newMetricName = DeltaCounter.getDeltaCounterMetricName(metricName); metricNamesForMetricMatchers.get(metricMatcher).add(newMetricName); return (Counter) metricCache.get(newMetricName, key -> DeltaCounter.get(metricsRegistry, newMetricName)); }
@Override public void processCounter(MetricName name, Counter counter, Void context) throws Exception { if (!sendZeroCounters && counter.count() == 0) return; // handle delta counters if (counter instanceof DeltaCounter) { long count = DeltaCounter.processDeltaCounter((DeltaCounter) counter); writeMetric(name, null, count); } else { writeMetric(name, null, counter.count()); } }
/** * This method returns the current count of the DeltaCounter and resets the counter. * * @param counter The DeltaCounter whose value is requested * @return The current count of the DeltaCounter */ public static long processDeltaCounter(DeltaCounter counter) { long count = counter.count(); counter.dec(count); return count; }
/** * A helper function to transform a counter name to a DeltaCounter name. The transformation * includes prepending a "\u2206" character to the name. * * @param name The name which needs to be transformed * @return The new name representing a DeltaCounter */ public static String getDeltaCounterName(String name) { if (!isDelta(name)) { return MetricConstants.DELTA_PREFIX + name; } else { return name; } }