From 288101b658f36501429a1724a1fba87fbeb15933 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Mon, 8 Jul 2024 18:08:42 -0700 Subject: [PATCH] remove deprecated codahale/yammer/dropwizard components. --- README.md | 4 +- pom.xml | 2 - signalfx-codahale/README.md | 11 - signalfx-codahale/pom.xml | 79 --- ...settingExponentiallyDecayingReservoir.java | 219 ------- .../com/signalfx/codahale/SfxMetrics.java | 594 ------------------ .../codahale/metrics/MetricBuilder.java | 91 --- .../codahale/metrics/ResettingHistogram.java | 38 -- .../codahale/metrics/ResettingTimer.java | 33 - .../codahale/metrics/SettableDoubleGauge.java | 63 -- .../codahale/metrics/SettableLongGauge.java | 64 -- .../AggregateMetricSenderSessionWrapper.java | 236 ------- .../codahale/reporter/DimensionInclusion.java | 59 -- .../codahale/reporter/IncrementalCounter.java | 66 -- .../codahale/reporter/MetricMetadata.java | 109 ---- .../codahale/reporter/MetricMetadataImpl.java | 252 -------- .../codahale/reporter/SignalFxReporter.java | 350 ----------- .../codahale/util/BasicJvmMetrics.java | 324 ---------- .../codahale/metrics/MetricMetadataTest.java | 41 -- .../metrics/SignalFxReporterTest.java | 325 ---------- .../metrics/util/BasicJvmMetrisTest.java | 48 -- signalfx-yammer/README.md | 11 - signalfx-yammer/pom.xml | 79 --- .../AggregateMetricSenderSessionWrapper.java | 364 ----------- .../reporter/CustomScheduledReporter.java | 204 ------ .../codahale/reporter/MetricMetadata.java | 81 --- .../codahale/reporter/MetricMetadataImpl.java | 159 ----- .../signalfx/codahale/reporter/SfUtil.java | 44 -- .../codahale/reporter/SignalFxReporter.java | 323 ---------- .../metrics/SignalFxReporterTest.java | 252 -------- 30 files changed, 1 insertion(+), 4524 deletions(-) delete mode 100644 signalfx-codahale/README.md delete mode 100644 signalfx-codahale/pom.xml delete mode 100644 signalfx-codahale/src/main/java/com/codahale/metrics/ResettingExponentiallyDecayingReservoir.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/SfxMetrics.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/MetricBuilder.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingHistogram.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingTimer.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableDoubleGauge.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableLongGauge.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/DimensionInclusion.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/IncrementalCounter.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java delete mode 100644 signalfx-codahale/src/main/java/com/signalfx/codahale/util/BasicJvmMetrics.java delete mode 100644 signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/MetricMetadataTest.java delete mode 100644 signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java delete mode 100644 signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/util/BasicJvmMetrisTest.java delete mode 100644 signalfx-yammer/README.md delete mode 100644 signalfx-yammer/pom.xml delete mode 100644 signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java delete mode 100644 signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/CustomScheduledReporter.java delete mode 100644 signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java delete mode 100644 signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java delete mode 100644 signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SfUtil.java delete mode 100644 signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java delete mode 100644 signalfx-yammer/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java diff --git a/README.md b/README.md index c19012a2..2880f93d 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,7 @@ to this repo are organizational or security related patches. No additional features will be added, and the repository will be archived and the final versions published on or prior to February 1, 2025. -* :warning: `signalfx-codahale` will be deleted in July 2024. -* :warning: `signalfx-yammer` will be deleted in July 2024. -* :warning: Everything else will be archived February 1st 2025. +* :warning:This repo will be archived February 1st 2025. Splunk has adopted OpenTelemetry. Use the [OpenTelemetry Java SDK](https://github.com/open-telemetry/opentelemetry-java) or diff --git a/pom.xml b/pom.xml index fbb5c7e5..417cbc94 100644 --- a/pom.xml +++ b/pom.xml @@ -385,8 +385,6 @@ signalfx-connection signalfx-java signalfx-commons-protoc-java - signalfx-codahale - signalfx-yammer signalfx-signalflow signalfx-metrics diff --git a/signalfx-codahale/README.md b/signalfx-codahale/README.md deleted file mode 100644 index a8f975e0..00000000 --- a/signalfx-codahale/README.md +++ /dev/null @@ -1,11 +0,0 @@ - -# signalfx-codahale - -:warning: `signalfx-codahale` has been deprecated and will be deleted -in July 2024. No further releases will be available after June 2024. - -Users are encouraged to use the -[OpenTelemetry Java SDK](https://github.com/open-telemetry/opentelemetry-java) or the -[Splunk Distribution of OpenTelemetry Java Instrumentation](https://github.com/signalfx/splunk-otel-java) -to send metrics to Splunk. - diff --git a/signalfx-codahale/pom.xml b/signalfx-codahale/pom.xml deleted file mode 100644 index 6661e3bb..00000000 --- a/signalfx-codahale/pom.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - 4.0.0 - - - com.signalfx.public - clients-parent - 1.0.43 - - - signalfx-codahale - Codahale to SignalFx - jar - - Dropwizard Codahale metrics plugin for signalfx - - - http://www.signalfx.com - - - - Apache License 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html - repo - - - - - scm:git:git@github.com:signalfx/signalfx-java.git - scm:git:git@github.com:signalfx/signalfx-java.git - git@github.com:signalfx/signalfx-java.git - - - - - signalfx - SignalFx - support+java@signalfx.com - SignalFx, Inc - http://www.signalfx.com - - - - - - - com.signalfx.public - signalfx-java - - - com.signalfx.public - signalfx-protoc - - - com.google.guava - guava - - - io.dropwizard.metrics - metrics-core - - - - - org.slf4j - slf4j-simple - - - org.hamcrest - hamcrest-core - - - junit - junit - - - diff --git a/signalfx-codahale/src/main/java/com/codahale/metrics/ResettingExponentiallyDecayingReservoir.java b/signalfx-codahale/src/main/java/com/codahale/metrics/ResettingExponentiallyDecayingReservoir.java deleted file mode 100644 index e9538648..00000000 --- a/signalfx-codahale/src/main/java/com/codahale/metrics/ResettingExponentiallyDecayingReservoir.java +++ /dev/null @@ -1,219 +0,0 @@ -package com.codahale.metrics; - -import static java.lang.Math.exp; -import static java.lang.Math.min; - -import java.util.ArrayList; -import java.util.concurrent.ConcurrentSkipListMap; -import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -import com.codahale.metrics.WeightedSnapshot.WeightedSample; - -/** - * An exponentially-decaying random reservoir of {@code long}s. Uses Cormode et al's - * forward-decaying priority reservoir sampling method to produce a statistically representative - * sampling reservoir, exponentially biased towards newer entries. - * - *

- * This class was taken verbatim from the Dropwizard Metrics library. The only modification is - * that {@link #getSnapshot()} clears the reservoir after the snapshot is taken. - *

- * - * @see - * Cormode et al. Forward Decay: A Practical Time Decay Model for Streaming Systems. ICDE '09: - * Proceedings of the 2009 IEEE International Conference on Data Engineering (2009) - */ -public class ResettingExponentiallyDecayingReservoir implements Reservoir { - private static final int DEFAULT_SIZE = 1028; - private static final double DEFAULT_ALPHA = 0.015; - private static final long RESCALE_THRESHOLD = TimeUnit.HOURS.toNanos(1); - - private final ConcurrentSkipListMap values; - private final ReentrantReadWriteLock lock; - private final double alpha; - private final int size; - private final AtomicLong count; - private volatile long startTime; - private final AtomicLong nextScaleTime; - private final Clock clock; - - /** - * Creates a new {@link ResettingExponentiallyDecayingReservoir} of 1028 elements, which offers - * a 99.9% confidence level with a 5% margin of error assuming a normal distribution, and an - * alpha factor of 0.015, which heavily biases the reservoir to the past 5 minutes of - * measurements. - */ - public ResettingExponentiallyDecayingReservoir() { - this(DEFAULT_SIZE, DEFAULT_ALPHA); - } - - /** - * Creates a new {@link ResettingExponentiallyDecayingReservoir}. - * - * @param size the number of samples to keep in the sampling reservoir - * @param alpha the exponential decay factor; the higher this is, the more biased the reservoir - * will be towards newer values - */ - public ResettingExponentiallyDecayingReservoir(int size, double alpha) { - this(size, alpha, Clock.defaultClock()); - } - - /** - * Creates a new {@link ResettingExponentiallyDecayingReservoir}. - * - * @param size the number of samples to keep in the sampling reservoir - * @param alpha the exponential decay factor; the higher this is, the more biased the reservoir - * will be towards newer values - * @param clock the clock used to timestamp samples and track rescaling - */ - public ResettingExponentiallyDecayingReservoir(int size, double alpha, Clock clock) { - this.values = new ConcurrentSkipListMap<>(); - this.lock = new ReentrantReadWriteLock(); - this.alpha = alpha; - this.size = size; - this.clock = clock; - this.count = new AtomicLong(0); - this.startTime = currentTimeInSeconds(); - this.nextScaleTime = new AtomicLong(clock.getTick() + RESCALE_THRESHOLD); - } - - @Override - public int size() { - return (int) min(size, count.get()); - } - - @Override - public void update(long value) { - update(value, currentTimeInSeconds()); - } - - /** - * Adds an old value with a fixed timestamp to the reservoir. - * - * @param value the value to be added - * @param timestamp the epoch timestamp of {@code value} in seconds - */ - public void update(long value, long timestamp) { - rescaleIfNeeded(); - lockForRegularUsage(); - try { - final double itemWeight = weight(timestamp - startTime); - final WeightedSample sample = new WeightedSample(value, itemWeight); - final double priority = itemWeight / ThreadLocalRandom.current().nextDouble(); - - final long newCount = count.incrementAndGet(); - if (newCount <= size) { - values.put(priority, sample); - } else { - Double first = values.firstKey(); - if (first < priority && values.putIfAbsent(priority, sample) == null) { - // ensure we always remove an item - while (values.remove(first) == null) { - first = values.firstKey(); - } - } - } - } finally { - unlockForRegularUsage(); - } - } - - private void rescaleIfNeeded() { - final long now = clock.getTick(); - final long next = nextScaleTime.get(); - if (now >= next) { - rescale(now, next); - } - } - - /** - * Get a weighted snapshot of the values in this reservoir. - * - * The reservoir is reset once the snapshot is taken (all values are discarded). - */ - @Override - public Snapshot getSnapshot() { - lockForRescale(); - rescaleIfNeeded(); - try { - Snapshot snapshot = new WeightedSnapshot(values.values()); - values.clear(); - count.set(0); - return snapshot; - } finally { - unlockForRescale(); - } - } - - private long currentTimeInSeconds() { - return TimeUnit.MILLISECONDS.toSeconds(clock.getTime()); - } - - private double weight(long t) { - return exp(alpha * t); - } - - /* "A common feature of the above techniques—indeed, the key technique that - * allows us to track the decayed weights efficiently—is that they maintain - * counts and other quantities based on g(ti − L), and only scale by g(t − L) - * at query time. But while g(ti −L)/g(t−L) is guaranteed to lie between zero - * and one, the intermediate values of g(ti − L) could become very large. For - * polynomial functions, these values should not grow too large, and should be - * effectively represented in practice by floating point values without loss of - * precision. For exponential functions, these values could grow quite large as - * new values of (ti − L) become large, and potentially exceed the capacity of - * common floating point types. However, since the values stored by the - * algorithms are linear combinations of g values (scaled sums), they can be - * rescaled relative to a new landmark. That is, by the analysis of exponential - * decay in Section III-A, the choice of L does not affect the final result. We - * can therefore multiply each value based on L by a factor of exp(−α(L′ − L)), - * and obtain the correct value as if we had instead computed relative to a new - * landmark L′ (and then use this new L′ at query time). This can be done with - * a linear pass over whatever data structure is being used." - */ - private void rescale(long now, long next) { - lockForRescale(); - try { - if (nextScaleTime.compareAndSet(next, now + RESCALE_THRESHOLD)) { - final long oldStartTime = startTime; - this.startTime = currentTimeInSeconds(); - final double scalingFactor = exp(-alpha * (startTime - oldStartTime)); - if (Double.compare(scalingFactor, 0) == 0) { - values.clear(); - } else { - final ArrayList keys = new ArrayList<>(values.keySet()); - for (Double key : keys) { - final WeightedSample sample = values.remove(key); - final WeightedSample newSample = new WeightedSample(sample.value, sample.weight * scalingFactor); - values.put(key * scalingFactor, newSample); - } - } - - // make sure the counter is in sync with the number of stored samples. - count.set(values.size()); - } - } finally { - unlockForRescale(); - } - } - - private void unlockForRescale() { - lock.writeLock().unlock(); - } - - private void lockForRescale() { - lock.writeLock().lock(); - } - - private void lockForRegularUsage() { - lock.readLock().lock(); - } - - private void unlockForRegularUsage() { - lock.readLock().unlock(); - } -} - diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/SfxMetrics.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/SfxMetrics.java deleted file mode 100644 index 3a5ebf58..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/SfxMetrics.java +++ /dev/null @@ -1,594 +0,0 @@ -/** - * Copyright (C) 2018 SignalFx, Inc. All rights reserved. - */ -package com.signalfx.codahale; - -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.Gauge; -import com.codahale.metrics.Histogram; -import com.codahale.metrics.Meter; -import com.codahale.metrics.Metric; -import com.codahale.metrics.MetricFilter; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.Reservoir; -import com.codahale.metrics.ResettingExponentiallyDecayingReservoir; -import com.codahale.metrics.Timer; -import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableSet; -import com.signalfx.codahale.metrics.MetricBuilder; -import com.signalfx.codahale.metrics.ResettingHistogram; -import com.signalfx.codahale.metrics.ResettingTimer; -import com.signalfx.codahale.metrics.SettableDoubleGauge; -import com.signalfx.codahale.metrics.SettableLongGauge; -import com.signalfx.codahale.reporter.IncrementalCounter; -import com.signalfx.codahale.reporter.MetricMetadata; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * A utility class for declaring Codahale metrics with additional dimensions. - * @deprecated Migrate to OpenTelemetry to send metric telemetry to Splunk. - */ -@Deprecated -public class SfxMetrics { - - private final MetricRegistry metricRegistry; - private final MetricMetadata metricMetadata; - - /** - * Create a new instance of this class backed by the given metric registry. - * - * @param metricRegistry - * The Codahale {@link MetricRegistry}. - * @param metricMetadata - * The SignalFx {@link MetricMetadata} registry. - */ - public SfxMetrics(MetricRegistry metricRegistry, MetricMetadata metricMetadata) { - this.metricRegistry = metricRegistry; - this.metricMetadata = metricMetadata; - } - - - public MetricRegistry getMetricRegistry() { - return metricRegistry; - } - - public MetricMetadata getMetricMetadata() { - return metricMetadata; - } - - /** - * Get or create a new counter. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link Counter} instance. - */ - public Counter counter(String metricName, String... dimensions) { - if (dimensions.length == 0) { - return metricRegistry.counter(metricName); - } - return build(MetricBuilder.COUNTERS, metricName, dimensions); - } - - /** - * Get or create a new counter. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link Counter} instance. - */ - public Counter counter(String metricName, Map dimensions) { - if (dimensions == null || dimensions.isEmpty()) { - return metricRegistry.counter(metricName); - } - return build(MetricBuilder.COUNTERS, metricName, dimensions); - } - - /** - * Get or create a new meter. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link Meter} instance. - */ - public Meter meter(String metricName, String... dimensions) { - if (dimensions.length == 0) { - return metricRegistry.meter(metricName); - } - return build(MetricBuilder.METERS, metricName, dimensions); - } - - /** - * Get or create a new meter. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link Meter} instance. - */ - public Meter meter(String metricName, Map dimensions) { - if (dimensions == null || dimensions.isEmpty()) { - return metricRegistry.meter(metricName); - } - return build(MetricBuilder.METERS, metricName, dimensions); - } - - /** - * Get or create a new incremental counter. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link IncrementalCounter} instance. - */ - public IncrementalCounter incrementalCounter(String metricName, String... dimensions) { - MetricMetadata.BuilderTagger metric = metricMetadata - .forBuilder(IncrementalCounter.Builder.INSTANCE); - return getT(metricName, metric, dimensions); - } - - /** - * Get or create a new incremental counter. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link IncrementalCounter} instance. - */ - public IncrementalCounter incrementalCounter(String metricName, - Map dimensions) { - MetricMetadata.BuilderTagger metric = metricMetadata - .forBuilder(IncrementalCounter.Builder.INSTANCE); - return getT(metricName, metric, dimensions); - } - - /** - * Get or create a new histogram. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link Histogram} instance. - */ - public Histogram histogram(String metricName, String... dimensions) { - if (dimensions.length == 0) { - return metricRegistry.histogram(metricName); - } - return build(MetricBuilder.HISTOGRAMS, metricName, dimensions); - } - - /** - * Get or create a new histogram. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link Histogram} instance. - */ - public Histogram histogram(String metricName, Map dimensions) { - if (dimensions == null || dimensions.isEmpty()) { - return metricRegistry.histogram(metricName); - } - return build(MetricBuilder.HISTOGRAMS, metricName, dimensions); - } - - /** - * Get or create a new histogram with {@link ResettingExponentiallyDecayingReservoir} that - * clears the reservoir after each {@link Reservoir#getSnapshot()}. Clearing reservoir after - * snapshot removes the effect of old data points and provides better visibility (where needed). - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link Histogram} instance. - */ - public Histogram resettingHistogram(String metricName, String... dimensions) { - if (dimensions.length == 0) { - return metricRegistry.histogram(metricName, ResettingHistogram::new); - } - return build(MetricBuilder.RESETTING_HISTOGRAMS, metricName, dimensions); - } - - /** - * Get or create a new histogram with {@link ResettingExponentiallyDecayingReservoir} that - * clears the reservoir after each {@link Reservoir#getSnapshot()}. Clearing reservoir after - * snapshot removes the effect of old data points and provides better visibility (where needed). - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link Histogram} instance. - */ - public Histogram resettingHistogram(String metricName, Map dimensions) { - if (dimensions == null || dimensions.isEmpty()) { - return metricRegistry.histogram(metricName, ResettingHistogram::new); - } - return build(MetricBuilder.RESETTING_HISTOGRAMS, metricName, dimensions); - } - - /** - * Get or create a new timer. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link Timer} instance. - */ - public Timer timer(String metricName, String... dimensions) { - if (dimensions.length == 0) { - return metricRegistry.timer(metricName); - } - return build(MetricBuilder.TIMERS, metricName, dimensions); - } - - /** - * Get or create a new timer. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link Timer} instance. - */ - public Timer timer(String metricName, Map dimensions) { - if (dimensions == null || dimensions.isEmpty()) { - return metricRegistry.timer(metricName); - } - return build(MetricBuilder.TIMERS, metricName, dimensions); - } - - /** - * Get or create a new timer with {@link ResettingExponentiallyDecayingReservoir} that - * clears the reservoir after each {@link Reservoir#getSnapshot()}. Clearing reservoir after - * snapshot removes the effect of old data points and provides better visibility (where needed). - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link Timer} instance. - */ - public Timer resettingTimer(String metricName, String... dimensions) { - if (dimensions.length == 0) { - return metricRegistry.timer(metricName, ResettingTimer::new); - } - return build(MetricBuilder.RESETTING_TIMERS, metricName, dimensions); - } - - /** - * Get or create a new timer with {@link ResettingExponentiallyDecayingReservoir} that - * clears the reservoir after each {@link Reservoir#getSnapshot()}. Clearing reservoir after - * snapshot removes the effect of old data points and provides better visibility (where needed). - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link Timer} instance. - */ - public Timer resettingTimer(String metricName, Map dimensions) { - if (dimensions == null || dimensions.isEmpty()) { - return metricRegistry.timer(metricName, ResettingTimer::new); - } - return build(MetricBuilder.RESETTING_TIMERS, metricName, dimensions); - } - - /** - * Track the execution of the given {@link Callable} with success, failure and timer metrics. - * - * @param function - * The {@link Callable} to execute. - * @param metricPrefix - * A prefix for the metric names. Successes are counted by a "prefix.success" metric; - * failures by a "prefix.failure" metric, and the {@link Callable}'s execution is - * tracked by a "prefix.time" timer. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @param - * The return type of the {@link Callable}. - * @return The return value of the {@link Callable}. - */ - public T track(Callable function, String metricPrefix, String... dimensions) { - long startTime = System.currentTimeMillis(); - try { - T result = function.call(); - counter(metricPrefix + ".success", dimensions).inc(); - return result; - } catch (Exception ex) { - counter(metricPrefix + ".failure", dimensions).inc(); - Throwables.throwIfUnchecked(ex); - throw new RuntimeException(ex); - } finally { - long duration = System.currentTimeMillis() - startTime; - timer(metricPrefix + ".time", dimensions).update(duration, TimeUnit.MILLISECONDS); - } - } - - /** - * Execute, with retries and tracking, the execution of the given {@link Callable}. - * - * @param function - * The {@link Callable} to execute. - * @param maxRetries - * The maximum number of retries of the execution. - * @param delay - * How long to wait between retries. - * @param unit - * The unit of the retry delay. - * @param metricPrefix - * A prefix for the metric names. Successes are counted by a "prefix.success" metric; - * failures by a "prefix.failure" metric, and the {@link Callable}'s execution is - * tracked by a "prefix.time" timer. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @param - * The return type of the {@link Callable}. - * @return The return value of the {@link Callable}. - */ - public T trackWithRetries(Callable function, int maxRetries, long delay, TimeUnit unit, - String metricPrefix, String... dimensions) { - int retryCounter = 0; - while (true) { - try { - return track(function, metricPrefix, dimensions); - } catch (RuntimeException ex) { - counter(metricPrefix + ".retries", dimensions).inc(); - retryCounter++; - if (retryCounter > maxRetries) { - counter(metricPrefix + ".maxRetriesReached", dimensions).inc(); - throw ex; - } - try { - unit.sleep(delay); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new RuntimeException(e); - } - } - } - } - - /** - * Get or create a new settable gauge metric for long integer values. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link SettableLongGauge} instance. - */ - public SettableLongGauge longGauge(String metricName, String... dimensions) { - return build(SettableLongGauge.Builder.INSTANCE, metricName, dimensions); - } - - /** - * Get or create a new settable gauge metric for long integer values. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link SettableLongGauge} instance. - */ - public SettableLongGauge longGauge(String metricName, Map dimensions) { - return build(SettableLongGauge.Builder.INSTANCE, metricName, dimensions); - } - - /** - * Get or create a new settable gauge metric for double precision floating point values. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @return The {@link SettableDoubleGauge} instance. - */ - public SettableDoubleGauge doubleGauge(String metricName, String... dimensions) { - return build(SettableDoubleGauge.Builder.INSTANCE, metricName, dimensions); - } - - /** - * Get or create a new settable gauge metric for double precision floating point values. - * - * @param metricName - * The metric name. - * @param dimensions - * Additional dimension key/value pairs, as a map. - * @return The {@link SettableDoubleGauge} instance. - */ - public SettableDoubleGauge doubleGauge(String metricName, Map dimensions) { - return build(SettableDoubleGauge.Builder.INSTANCE, metricName, dimensions); - } - - /** - * Register the given {@link Gauge} as a cumulative counter. - * - * Cumulative counters fundamentally function like gauges, but use a delta rollup by default. - * This method allows you to report a gauge that measures a monotonically increasing value as a - * cumulative counter to SignalFx. - * - * @param metricName - * The metric name. - * @param gauge - * The {@link Gauge} instance. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - */ - public void registerGaugeAsCumulativeCounter(String metricName, Gauge gauge, - String... dimensions) { - MetricMetadata.Tagger> tagger = metricMetadata - .forMetric(metricRegistry.register(metricName, gauge)) - .withMetricName(metricName) - .withMetricType(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER); - for (int i = 0; i < dimensions.length - 1; i += 2) { - tagger.withDimension(dimensions[i], dimensions[i + 1]); - } - } - - /** - * Register the given {@link Gauge} as a cumulative counter. - * - * Cumulative counters fundamentally function like gauges, but use a delta rollup by default. - * This method allows you to report a gauge that measures a monotonically increasing value as a - * cumulative counter to SignalFx. - * - * @param metricName - * The metric name. - * @param gauge - * The {@link Gauge} instance. - * @param dimensions - * Additional dimension key/value pairs, as a map. - */ - public void registerGaugeAsCumulativeCounter(String metricName, Gauge gauge, - Map dimensions) { - MetricMetadata.Tagger> tagger = metricMetadata - .forMetric(metricRegistry.register(metricName, gauge)) - .withMetricName(metricName) - .withMetricType(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER); - if (dimensions != null) { - for (Map.Entry entry : dimensions.entrySet()) { - tagger.withDimension(entry.getKey(), entry.getValue()); - } - } - } - - /** - * Register an existing {@link Gauge} instance to start reporting it. - * - * @param metricName - * The metric name. - * @param gauge - * The {@link Gauge} instance. - * @param dimensions - * Additional dimension key/value pairs (an even number of strings must be provided). - * @param - * The concrete type of the gauge instance. - * @return The gauge instance itself. - */ - public > T registerGauge(String metricName, T gauge, String... dimensions) { - return build(builderForMetric(gauge), metricName, dimensions); - } - - /** - * Register an existing {@link Gauge} instance to start reporting it. - * - * @param metricName - * The metric name. - * @param gauge - * The {@link Gauge} instance. - * @param dimensions - * Additional dimension key/value pairs, as a map - * @param - * The concrete type of the gauge instance. - * @return The gauge instance itself. - */ - public > T registerGauge(String metricName, T gauge, - Map dimensions) { - return build(builderForMetric(gauge), metricName, dimensions); - } - - /** - * Unregister the given metric to stop reporting it. - * - * @param metric - * The metric instance. - * @param - * The concrete type of the metric object. - * @return True iff the metric was previously registered and was removed; false otherwise. - */ - public boolean unregister(T metric) { - return metricMetadata.removeMetric(metric, metricRegistry); - } - - /** - * Unregister a set of metrics at once. - * - * @param metricsToRemove - * An array of metric instances to stop reporting. - * @return The number of metrics that were actually unregistered. - */ - public int unregister(Metric... metricsToRemove) { - final Set toRemove = ImmutableSet.copyOf(metricsToRemove); - final AtomicInteger totalRemoved = new AtomicInteger(0); - metricRegistry.removeMatching(new MetricFilter() { - @Override - public boolean matches(String name, Metric metric) { - final boolean shouldRemove = toRemove.contains(metric); - if (shouldRemove) { - totalRemoved.incrementAndGet(); - } - return shouldRemove; - } - }); - return totalRemoved.get(); - } - - private > MetricBuilder builderForMetric(final T metric) { - return new MetricBuilder() { - @Override - public T newMetric() { - return metric; - } - - @Override - public boolean isInstance(Metric otherMetric) { - return otherMetric.getClass().isInstance(metric); - } - }; - } - - private T build(MetricBuilder builder, String metricName, - String... dimensions) { - MetricMetadata.BuilderTagger tagger = metricMetadata.forBuilder(builder); - return getT(metricName, tagger, dimensions); - } - - private T build(MetricBuilder builder, String metricName, - Map dimensions) { - MetricMetadata.BuilderTagger tagger = metricMetadata.forBuilder(builder); - return getT(metricName, tagger, dimensions); - } - - private T getT(String metricName, MetricMetadata.BuilderTagger tagger, - String[] dimensions) { - Preconditions.checkArgument(dimensions.length % 2 == 0, - "Dimensions parameter should have even number of elements"); - tagger.withMetricName(metricName); - for (int i = 0; i < dimensions.length - 1; i += 2) { - tagger.withDimension(dimensions[i], dimensions[i + 1]); - } - return tagger.createOrGet(metricRegistry); - } - - private T getT(String metricName, MetricMetadata.BuilderTagger tagger, - Map dimensions) { - tagger.withMetricName(metricName); - if (dimensions != null) { - for (Map.Entry entry : dimensions.entrySet()) { - tagger.withDimension(entry.getKey(), entry.getValue()); - } - } - return tagger.createOrGet(metricRegistry); - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/MetricBuilder.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/MetricBuilder.java deleted file mode 100644 index 8162b056..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/MetricBuilder.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.signalfx.codahale.metrics; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.ExponentiallyDecayingReservoir; -import com.codahale.metrics.Histogram; -import com.codahale.metrics.Meter; -import com.codahale.metrics.Metric; -import com.codahale.metrics.Timer; - -/** - * A copy of the MetricBuilder defined inside {@link com.codahale.metrics.MetricRegistry}, but a - * public version. - * @param Which metric type this builds - */ -public interface MetricBuilder { - public T newMetric(); - - public boolean isInstance(Metric metric); - - public MetricBuilder COUNTERS = new MetricBuilder() { - @Override - public Counter newMetric() { - return new Counter(); - } - - @Override - public boolean isInstance(Metric metric) { - return Counter.class.isInstance(metric); - } - }; - - public MetricBuilder HISTOGRAMS = new MetricBuilder() { - @Override - public Histogram newMetric() { - return new Histogram(new ExponentiallyDecayingReservoir()); - } - - @Override - public boolean isInstance(Metric metric) { - return Histogram.class.isInstance(metric); - } - }; - - public MetricBuilder RESETTING_HISTOGRAMS = new MetricBuilder() { - @Override - public Histogram newMetric() { - return new ResettingHistogram(); - } - - @Override - public boolean isInstance(Metric metric) { - return Histogram.class.isInstance(metric); - } - }; - - public MetricBuilder METERS = new MetricBuilder() { - @Override - public Meter newMetric() { - return new Meter(); - } - - @Override - public boolean isInstance(Metric metric) { - return Meter.class.isInstance(metric); - } - }; - - public MetricBuilder TIMERS = new MetricBuilder() { - @Override - public Timer newMetric() { - return new Timer(); - } - - @Override - public boolean isInstance(Metric metric) { - return Timer.class.isInstance(metric); - } - }; - - public MetricBuilder RESETTING_TIMERS = new MetricBuilder() { - @Override - public Timer newMetric() { - return new ResettingTimer(); - } - - @Override - public boolean isInstance(Metric metric) { - return Timer.class.isInstance(metric); - } - }; -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingHistogram.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingHistogram.java deleted file mode 100644 index 4c57c8aa..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingHistogram.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (C) 2017 SignalFx, Inc. All rights reserved. - */ -package com.signalfx.codahale.metrics; - -import com.codahale.metrics.Clock; -import com.codahale.metrics.Histogram; -import com.codahale.metrics.ResettingExponentiallyDecayingReservoir; - -/** - * A histogram that resets its reservoir every time it is snapshotted. - * - *

- * Usage example: - *

- * - *
- * MetricRegistry metrics = new MetricRegistry();
- * Histogram histo = metrics.register("my.histogram", new ResettingHistogram());
- * histo.update(42);
- * 
- * - * @see com.codahale.metrics.Histogram - */ -public class ResettingHistogram extends Histogram { - - public ResettingHistogram() { - super(new ResettingExponentiallyDecayingReservoir()); - } - - public ResettingHistogram(int size, double alpha) { - super(new ResettingExponentiallyDecayingReservoir(size, alpha)); - } - - public ResettingHistogram(int size, double alpha, Clock clock) { - super(new ResettingExponentiallyDecayingReservoir(size, alpha, clock)); - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingTimer.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingTimer.java deleted file mode 100644 index c3c98712..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/ResettingTimer.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.signalfx.codahale.metrics; - -import com.codahale.metrics.Clock; -import com.codahale.metrics.ResettingExponentiallyDecayingReservoir; -import com.codahale.metrics.Timer; - -/** - * An extension of {@link Timer} that resets its reservoir every time is snapshotted. This is - * useful when you want to ignore the effects of very old data points on the current reported value. - * - *

- * Usage example: - *

- * - *
- * MetricRegistry metrics = new MetricRegistry();
- * Timer timer = metrics.register("my.timer", new ResettingTimer());
- * try (Timer.Context ignored = timer.time()) {
- *     // do something.
- * }
- * 
- * - */ -public class ResettingTimer extends Timer { - - public ResettingTimer() { - super(new ResettingExponentiallyDecayingReservoir()); - } - - public ResettingTimer(Clock clock) { - super(new ResettingExponentiallyDecayingReservoir(), clock); - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableDoubleGauge.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableDoubleGauge.java deleted file mode 100644 index 1310edb1..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableDoubleGauge.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.signalfx.codahale.metrics; - -import com.codahale.metrics.Gauge; -import com.codahale.metrics.Metric; - -/** - *

- * Works like a Gauge, but rather than getting its value from a callback, the value - * is set when needed. This can be somewhat convienent, but direct use of a Gauge is likely better - *

- *

- * Usage example: - *

{@code
- *       MetricRegister metricRegistry;
- *       SettableDoubleGauge settable = metricRegistry.register("metric.name", new SettableDoubleGauge());
- *       // ...
- *       settable.setValue(1.234);
- *       // ...
- *       settable.setValue(3.156);
- *     }
- *     
- */ -public class SettableDoubleGauge implements Metric, Gauge { - /** - * Current value. Assignment will be atomic. See 17.7 - */ - private volatile double value; - - /** - * Set the current value the {@link Gauge} will return to something else. - * @param value last set value - * @return itself - */ - public SettableDoubleGauge setValue(double value) { - this.value = value; - return this; - } - - /** - * The last value set by {@link #setValue(double)}} - * @return Last set value, or zero. - */ - public Double getValue() { - return value; - } - - - public final static class Builder implements MetricBuilder { - public static final Builder INSTANCE = new Builder(); - private Builder() { - } - - @Override - public SettableDoubleGauge newMetric() { - return new SettableDoubleGauge(); - } - - @Override - public boolean isInstance(Metric metric) { - return metric instanceof SettableDoubleGauge; - } - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableLongGauge.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableLongGauge.java deleted file mode 100644 index 3dbcbe30..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/metrics/SettableLongGauge.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.signalfx.codahale.metrics; - -import com.codahale.metrics.Gauge; -import com.codahale.metrics.Metric; - -/** - *

- * Works like a Gauge, but rather than getting its value from a callback, the value - * is set when needed. This can be somewhat convienent, but direct use of a Gauge is likely better - *

- *

- * Usage example: - *

{@code
- *       MetricRegister metricRegistry;
- *       SettableLongGauge settable = metricRegistry.register("metric.name", new SettableLongGauge());
- *       // ...
- *       settable.setValue(100);
- *       // ...
- *       settable.setValue(200);
- *     }
- *
- *     
- */ -public class SettableLongGauge implements Metric, Gauge { - /** - * Current value. Assignment will be atomic. See 17.7 - */ - private volatile long value = 0; - - /** - * Set the current value the {@link Gauge} will return to something else. - * @param value last set value - * @return itself - */ - public SettableLongGauge setValue(long value) { - this.value = value; - return this; - } - - /** - * The last value set by {@link #setValue(long)}} - * @return Last set value, or zero. - */ - public Long getValue() { - return value; - } - - public final static class Builder implements MetricBuilder { - public static final Builder INSTANCE = new Builder(); - - private Builder() { - } - - @Override - public SettableLongGauge newMetric() { - return new SettableLongGauge(); - } - - @Override - public boolean isInstance(Metric metric) { - return metric instanceof SettableLongGauge; - } - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java deleted file mode 100644 index 2e226847..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java +++ /dev/null @@ -1,236 +0,0 @@ -/** -* Copyright (C) 2015 SignalFx, Inc. -*/ -package com.signalfx.codahale.reporter; - -import java.io.Closeable; -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -import com.codahale.metrics.Histogram; -import com.codahale.metrics.Metered; -import com.codahale.metrics.Metric; -import com.codahale.metrics.Sampling; -import com.codahale.metrics.Snapshot; -import com.codahale.metrics.Timer; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.signalfx.metrics.SignalFxMetricsException; -import com.signalfx.metrics.flush.AggregateMetricSender; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -class AggregateMetricSenderSessionWrapper implements Closeable { - - private static final ImmutableSet ignoredDimensions = ImmutableSet.of( - MetricMetadata.SOURCE, MetricMetadata.METRIC); - - private final AggregateMetricSender.Session metricSenderSession; - private final Set detailsToAdd; - private final MetricMetadata metricMetadata; - private final String defaultSourceName; - private final String sourceDimension; - private final boolean injectCurrentTimestamp; - private final ImmutableMap defaultDimensions; - - AggregateMetricSenderSessionWrapper( - AggregateMetricSender.Session metricSenderSession, - Set detailsToAdd, - MetricMetadata metricMetadata, - String defaultSourceName, - String sourceDimension) { - this(metricSenderSession, detailsToAdd, metricMetadata, defaultSourceName, sourceDimension, - false, Collections. emptyMap()); - } - - AggregateMetricSenderSessionWrapper( - AggregateMetricSender.Session metricSenderSession, - Set detailsToAdd, - MetricMetadata metricMetadata, - String defaultSourceName, - String sourceDimension, - boolean injectCurrentTimestamp, - Map defaultDimensions) { - this.metricSenderSession = metricSenderSession; - this.detailsToAdd = detailsToAdd; - this.metricMetadata = metricMetadata; - this.defaultSourceName = defaultSourceName; - this.sourceDimension = sourceDimension; - this.injectCurrentTimestamp = injectCurrentTimestamp; - this.defaultDimensions = ImmutableMap.copyOf(defaultDimensions); - } - - @Override - public void close() { - try { - metricSenderSession.close(); - } catch (Exception e) { - throw new SignalFxMetricsException("Unable to close session and send metrics", e); - } - } - - // These three called from report - void addTimer(String key, Timer value) { - addMetered(key, value); - addSampling(key, value); - } - - void addHistogram(String baseName, - Histogram histogram) { - addMetric(histogram, baseName, - Optional.of(SignalFxReporter.MetricDetails.COUNT), - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, histogram.getCount()); - addSampling(baseName, histogram); - } - - void addMetered(String baseName, Metered metered) { - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.COUNT, - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, metered.getCount()); - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_15_MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.getFifteenMinuteRate()); - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_1_MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.getOneMinuteRate()); - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_5_MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.getFiveMinuteRate()); - - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_MEAN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.getMeanRate()); - } - - private void addSampling(String baseName, Sampling sampling) { - Metric metric = (Metric)sampling; - final Snapshot snapshot = sampling.getSnapshot(); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MEDIAN, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMedian()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_75, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get75thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_95, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get95thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_98, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get98thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_99, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get99thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_999, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get999thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MAX, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMax()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMin()); - - - // These are slower to calculate. Only calculate if we need. - if (detailsToAdd.contains(SignalFxReporter.MetricDetails.STD_DEV)) { - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.STD_DEV, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getStdDev()); - } - if (detailsToAdd.contains(SignalFxReporter.MetricDetails.MEAN)) { - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MEAN, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMean()); - } - } - - void addMetric(Metric metric, String codahaleName, - SignalFxProtocolBuffers.MetricType defaultMetricType, - Object originalValue) { - addMetric(metric, codahaleName, Optional.absent(), - defaultMetricType, originalValue); - } - - private void addMetric(Metric metric, String codahaleName, SignalFxReporter.MetricDetails metricDetails, - SignalFxProtocolBuffers.MetricType defaultMetricType, - Object originalValue) { - addMetric(metric, codahaleName, Optional.of(metricDetails), - defaultMetricType, originalValue); - - } - - void addMetric(Metric metric, String codahaleName, - Optional metricDetails, - SignalFxProtocolBuffers.MetricType defaultMetricType, Object originalValue) { - final Number value; - if (originalValue instanceof Number) { - value = (Number) originalValue; - } else if (originalValue instanceof Boolean) { - value = ((Boolean)originalValue).booleanValue() ? 1 : 0; - } else { - // Unsupported type - return; - } - final String metricDetailsMetricNameSuffix; - if (metricDetails.isPresent()) { - if (!detailsToAdd.contains(metricDetails.get())) { - return; - } - metricDetailsMetricNameSuffix = "." + metricDetails.get().getDescription(); - } else { - metricDetailsMetricNameSuffix = ""; - } - Optional userSetMetricType = metricMetadata.getMetricType(metric); - SignalFxProtocolBuffers.MetricType metricType = userSetMetricType.or(defaultMetricType); - Map tags = metricMetadata.getTags(metric); - final String sourceName = Optional.fromNullable(tags.get(MetricMetadata.SOURCE)).or(defaultSourceName); - final String metricName = Optional.fromNullable(tags.get(MetricMetadata.METRIC)).or(codahaleName) + metricDetailsMetricNameSuffix; - - SignalFxProtocolBuffers.DataPoint.Builder builder = SignalFxProtocolBuffers.DataPoint - .newBuilder() - .setMetric(metricName) - .setMetricType(metricType); - - if (!sourceDimension.equals("") && !tags.containsKey(sourceDimension)) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder() - .setKey(sourceDimension).setValue(sourceName)); - } - - for (Map.Entry entry: tags.entrySet()) { - if (!ignoredDimensions.contains(entry.getKey())) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder() - .setKey(entry.getKey()).setValue(entry.getValue())); - } - } - - for (Map.Entry entry : defaultDimensions.entrySet()) { - String dimName = entry.getKey(); - DimensionInclusion dimValue = entry.getValue(); - if (!ignoredDimensions.contains(dimName) && !tags.containsKey(dimName) - && dimValue.shouldInclude(metricType)) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder().setKey(dimName) - .setValue(dimValue.getValue())); - } - } - - if (injectCurrentTimestamp) { - final long currentTimestamp = System.currentTimeMillis(); - builder.setTimestamp(currentTimestamp); - } - - if (value instanceof Long || value instanceof Integer || value instanceof Short) { - builder.setValue(SignalFxProtocolBuffers.Datum - .newBuilder().setIntValue(value.longValue())); - } else { - final double doubleToSend = value.doubleValue(); - if (Double.isInfinite(doubleToSend) || Double.isNaN(doubleToSend)) { - return; - } - builder.setValue(SignalFxProtocolBuffers.Datum.newBuilder() - .setDoubleValue(doubleToSend)); - } - - metricSenderSession.setDatapoint(builder.build()); - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/DimensionInclusion.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/DimensionInclusion.java deleted file mode 100644 index 14521697..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/DimensionInclusion.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (C) 2015 SignalFx, Inc. - */ -package com.signalfx.codahale.reporter; - -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * Collection of flags to indicate if a default dimension should be added to a type of metric - * - */ -public class DimensionInclusion { - - public static final short COUNTER = 1 << 0; - public static final short CUMULATIVE_COUNTER = 1 << 1; - public static final short GAUGE = 1 << 2; - - public static final short ALL = COUNTER | CUMULATIVE_COUNTER | GAUGE; - public static final short NOT_COUNTER = CUMULATIVE_COUNTER | GAUGE; - - private final String value; - private final short inclusion; - - private DimensionInclusion(String value, short inclusion) { - this.value = value; - this.inclusion = inclusion; - } - - public static DimensionInclusion unique(String value) { - return new DimensionInclusion(value, NOT_COUNTER); - } - - public static DimensionInclusion shared(String value) { - return new DimensionInclusion(value, ALL); - } - - public boolean shouldInclude(SignalFxProtocolBuffers.MetricType metricType) { - switch (metricType) { - case GAUGE: - return checkBit(GAUGE); - case COUNTER: - return checkBit(COUNTER); - case CUMULATIVE_COUNTER: - return checkBit(CUMULATIVE_COUNTER); - case ENUM: - default: - return false; - } - } - - public String getValue() { - return value; - } - - private boolean checkBit(short bit) { - - return (inclusion & bit) == bit; - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/IncrementalCounter.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/IncrementalCounter.java deleted file mode 100644 index b0cc0e97..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/IncrementalCounter.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.signalfx.codahale.reporter; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.Metric; -import com.signalfx.codahale.metrics.MetricBuilder; - -/** - *

- * An {@link com.signalfx.codahale.reporter.IncrementalCounter} is a counter that reports - * incremental values to SignalFx rather than absolute counts. For example, - * a regular {@link com.codahale.metrics.Counter} reports a monotonically increasing series of - * values (1, 2, 3, 4, ...) while this class reports a series of increments (+1, +1, +1, +1), but - * both represent the same rate of 1 unit per reporting interval. A - * {@link com.codahale.metrics.Counter} created the regular Codahale way is the preferred way - * to report incremental values to SignalFx when possible. - *

- *

- * An example use case of this class would be if you wanted to count the number of requests to a webpage, - * but didn't care about that as a dimension of the code serving the request. So instead - * of reporting source=hostname metric=webpage.user_login.hits", which multiplies by the - * number of different source=hostname that are reporting, you can report the metric as - * source=webpage metric=user_login.hits and all servers will increment the same metric, even though - * they have different rolling counts. - *

- *

- * A {@link com.codahale.metrics.Counter} assumes metric type - * {@link com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType#CUMULATIVE_COUNTER}, - * while this class assumes metric type - * {@link com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType#COUNTER} - */ -public class IncrementalCounter extends Counter { - /** - * The last value when {@link #getCountChange()} was called. - */ - private long lastValue; - - /** - * Returns the difference between the current value of the counter and the value when this - * function was last called. - * - * @return Counter difference - */ - public synchronized long getCountChange() { - final long currentCount = getCount(); - final long countChange = currentCount - lastValue; - lastValue = currentCount; - return countChange; - } - - public final static class Builder implements MetricBuilder { - public static final Builder INSTANCE = new Builder(); - - private Builder() { - } - - @Override - public IncrementalCounter newMetric() { - return new IncrementalCounter(); - } - - @Override - public boolean isInstance(Metric metric) { - return metric instanceof IncrementalCounter; - } - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java deleted file mode 100644 index 8b61ebaf..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.signalfx.codahale.reporter; - -import java.util.Map; -import com.codahale.metrics.Metric; -import com.codahale.metrics.MetricRegistry; -import com.google.common.base.Optional; -import com.signalfx.codahale.metrics.MetricBuilder; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * Allows users to modify a metric with different source or metric parts than the default we pick - * from codahale. Note: This class must be thread safe. - */ -public interface MetricMetadata { - public static final String SOURCE = "source"; - public static final String METRIC = "metric"; - public Map getTags(Metric metric); - public Optional getMetricType(Metric metric); - - /** - * Create an object to tag a metric with data. Registering two different metrics with the same - * metadata will result in an exception. In that case, use {@link #forBuilder(com.signalfx.codahale.metrics.MetricBuilder)} - * @param metric The metric will tag. - * @param The type of metric. It is implied by the metric type. - * @return An object to tag the given metric. - */ - public Tagger forMetric(M metric); - - @Deprecated - public Tagger tagMetric(M metric); - - /** - * Create a tagger for a type of objects. This is different than {@link #forMetric(com.codahale.metrics.Metric)} - * because it will not use the builder to create a metric unless if it already exists. - * @param metricBuilder The builder that creates metrics. - * @param The type of metric to create. - * @return An object to tag metrics. - */ - public BuilderTagger forBuilder(MetricBuilder metricBuilder); - - /** - * Removes the specified metric from the metric metadata and registry. - * @param metric The metric to remove, cannot be null. - * @param metricRegistry Registry to remove the metric from - * @return True if the metric was found and removed, false otherwise - */ - public boolean removeMetric(M metric, MetricRegistry metricRegistry); - - public interface TaggerBase> { - /** - * Tag the metric with a sf_source - * @param sourceName Source name for the sf_source - * @return this - * @deprecated The use of the build in source parameter is deprecated and discouraged. Use - * {@link #withDimension(String, String)} instead. - */ - @Deprecated - T withSourceName(String sourceName); - - /** - * Changes the metric name of this metric from the default (which is the codahale metric - * name), to another string - * @param metricName The new name in SignalFx of this metric - * @return this - */ - T withMetricName(String metricName); - - /** - * Adds a dimension to this metric - * @param key The dimension key to add - * @param value The dimensino value to add - * @return this - */ - T withDimension(String key, String value); - - /** - * Changes the default metric type of this metric to the SignalFx metric type passed in - * @param metricType The new metric type of this metric - * @return this - */ - T withMetricType(SignalFxProtocolBuffers.MetricType metricType); - } - - public interface Tagger extends TaggerBase> { - /** - * Helper that registers a metric in the registry, throwing an exception if it already - * exists. - * @param metricRegistry Where to register the metric. - * @return The new metric - */ - M register(MetricRegistry metricRegistry); - - /** - * Returns the tagged metric - * @return the tagged metric - */ - M metric(); - } - - public interface BuilderTagger extends TaggerBase> { - /** - * Create this metric in the registry, or return the currently registered metric if it - * already exists. - * @param metricRegistry Registry to create the metric in - * @return The new (or existing) metric. - */ - M createOrGet(MetricRegistry metricRegistry); - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java deleted file mode 100644 index 631e3acc..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java +++ /dev/null @@ -1,252 +0,0 @@ -package com.signalfx.codahale.reporter; - -import java.util.Collections; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import com.codahale.metrics.Metric; -import com.codahale.metrics.MetricRegistry; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.signalfx.codahale.metrics.MetricBuilder; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType; - -public class MetricMetadataImpl implements MetricMetadata { - private final ConcurrentMap metaDataCollection; - - public MetricMetadataImpl() { - // This map must be thread safe - metaDataCollection = new ConcurrentHashMap(); - } - - @Override - public Map getTags(Metric metric) { - Metadata existingMetaData = metaDataCollection.get(metric); - if (existingMetaData == null) { - return Collections.emptyMap(); - } else { - return Collections.unmodifiableMap(existingMetaData.tags); - } - } - - @Override - public Optional getMetricType(Metric metric) { - Metadata existingMetaData = metaDataCollection.get(metric); - if (existingMetaData == null || existingMetaData.metricType == null) { - return Optional.absent(); - } else { - return Optional.of(existingMetaData.metricType); - } - } - - @Override - public Tagger tagMetric(M metric) { - return forMetric(metric); - } - - @Override - public Tagger forMetric(M metric) { - Metadata metadata = metaDataCollection.get(metric); - if (metadata == null) { - synchronized (this) { - metadata = metaDataCollection.get(metric); - if (metadata == null) { - metadata = new Metadata(); - Metadata oldMetaData = metaDataCollection.put(metric, metadata); - Preconditions.checkArgument(oldMetaData == null, - "Concurrency issue adding metadata"); - } - } - } - return new TaggerImpl(metric, metadata); - } - - @Override - public BuilderTagger forBuilder( - MetricBuilder metricBuilder) { - return new BuilderTaggerImpl(metricBuilder, metaDataCollection, new Metadata()); - } - - @Override - public boolean removeMetric(M metric, MetricRegistry metricRegistry) { - Metadata metadata = metaDataCollection.remove(metric); - if (metadata == null) { - return false; - } - metricRegistry.remove(metadata.getCodahaleName()); - return true; - } - - private static abstract class TaggerBaseImpl> - implements TaggerBase{ - protected final Metadata thisMetricsMetadata; - - @Override - public T withDimension(String key, String value) { - thisMetricsMetadata.tags.put(key, value); - return (T) this; - } - - TaggerBaseImpl(Metadata thisMetricsMetadata) { - this.thisMetricsMetadata = thisMetricsMetadata; - } - - @Override - public T withSourceName(String sourceName) { - thisMetricsMetadata.tags.put(SOURCE, sourceName); - return (T) this; - } - - @Override - public T withMetricName(String metricName) { - thisMetricsMetadata.tags.put(METRIC, metricName); - return (T) this; - } - - @Override - public T withMetricType(MetricType metricType) { - thisMetricsMetadata.metricType = metricType; - return (T) this; - } - - protected String createCodahaleName() { - return thisMetricsMetadata.getCodahaleName(); - } - } - - private static final class TaggerImpl extends TaggerBaseImpl> - implements Tagger { - private final M metric; - - TaggerImpl(M metric, Metadata thisMetricsMetadata) { - super(thisMetricsMetadata); - this.metric = metric; - } - - @Override - public M register(MetricRegistry metricRegistry) { - String compositeName = createCodahaleName(); - return metricRegistry.register(compositeName, metric); - } - - @Override public M metric() { - return metric; - } - } - - private class BuilderTaggerImpl extends TaggerBaseImpl> - implements BuilderTagger { - private final MetricBuilder metricBuilder; - private final ConcurrentMap metaDataCollection; - - public BuilderTaggerImpl(MetricBuilder metricBuilder, - ConcurrentMap metaDataCollection, - Metadata thisMetricsMetadata) { - super(thisMetricsMetadata); - this.metricBuilder = metricBuilder; - this.metaDataCollection = metaDataCollection; - } - - @Override - public M createOrGet(MetricRegistry metricRegistry) { - String compositeName = createCodahaleName(); - Metric existingMetric = metricRegistry.getMetrics().get(compositeName); - if (existingMetric != null && metaDataCollection.get(existingMetric) != null) { - return validateMetric(existingMetric, compositeName); - } - // Lock on an object that is shared by the metadata tagger, not *this* which is not. - synchronized (metaDataCollection) { - existingMetric = metricRegistry.getMetrics().get(compositeName); - if (existingMetric != null) { - return validateMetric(existingMetric, compositeName); - } - // This could throw a IllegalArgumentException. That would only happen if another - // metric was made with our name, but not constructed by the metadata tagger. This - // is super strange and deserves an exception. - Metric newMetric = metricRegistry.register(compositeName, - metricBuilder.newMetric()); - Preconditions.checkArgument( - null == metaDataCollection.put(newMetric, thisMetricsMetadata)); - return (M) newMetric; - } - } - - private M validateMetric(Metric existingMetric, String compositeName) { - if (!metricBuilder.isInstance(existingMetric)) { - throw new IllegalArgumentException( - String.format("The metric %s is not of the correct type", - compositeName)); - } - if (!thisMetricsMetadata.equals(metaDataCollection.get(existingMetric))) { - throw new IllegalArgumentException(String.format( - "Existing metric has different tags. Unable to differentiate " + - "metrics: %s", - compositeName)); - } - return (M) existingMetric; - } - } - - private static final class Metadata { - private final Map tags; - private MetricType metricType; - - private Metadata() { - tags = new ConcurrentHashMap(6); - } - - public String getCodahaleName() { - final String existingMetricName = Preconditions.checkNotNull( - tags.get(MetricMetadata.METRIC), - "The register helper needs a base metric name to build a readable " - + "metric. use withMetricName or codahale directly"); - - // The names should be unique so we sort each parameter by the tag key. - SortedMap extraParameters = new TreeMap(); - for (Map.Entry entry : tags.entrySet()) { - // Don't re-add the metric name - if (!MetricMetadata.METRIC.equals(entry.getKey())) { - extraParameters.put(entry.getKey(), entry.getValue()); - } - } - StringBuilder compositeName = new StringBuilder(); - // Add each entry in sorted order - for (Map.Entry entry : extraParameters.entrySet()) { - compositeName.append(entry.getValue()).append('.'); - } - compositeName.append(existingMetricName); - return compositeName.toString(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof Metadata)) { - return false; - } - - Metadata metadata = (Metadata) o; - - if (metricType != metadata.metricType) { - return false; - } - if (tags != null ? !tags.equals(metadata.tags) : metadata.tags != null) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - int result = tags != null ? tags.hashCode() : 0; - result = 31 * result + (metricType != null ? metricType.hashCode() : 0); - return result; - } - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java deleted file mode 100644 index 153fb7d9..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java +++ /dev/null @@ -1,350 +0,0 @@ -/** -* Copyright (C) 2015 SignalFx, Inc. -*/ -package com.signalfx.codahale.reporter; - -import static java.util.Objects.requireNonNull; - -import java.util.Collection; -import java.util.Collections; -import java.util.EnumSet; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.concurrent.TimeUnit; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.Gauge; -import com.codahale.metrics.Histogram; -import com.codahale.metrics.Meter; -import com.codahale.metrics.MetricFilter; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.ScheduledReporter; -import com.codahale.metrics.Timer; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.signalfx.endpoint.SignalFxEndpoint; -import com.signalfx.endpoint.SignalFxReceiverEndpoint; -import com.signalfx.metrics.SourceNameHelper; -import com.signalfx.metrics.auth.AuthToken; -import com.signalfx.metrics.auth.StaticAuthToken; -import com.signalfx.metrics.connection.DataPointReceiverFactory; -import com.signalfx.metrics.connection.HttpDataPointProtobufReceiverFactory; -import com.signalfx.metrics.errorhandler.OnSendErrorHandler; -import com.signalfx.metrics.flush.AggregateMetricSender; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * Reporter object for codahale metrics that reports values to com.signalfx.signalfx at some - * interval. - * @deprecated Migrate to OpenTelemetry to send metric telemetry to Splunk. - */ -@Deprecated -public class SignalFxReporter extends ScheduledReporter { - private final AggregateMetricSender aggregateMetricSender; - private final Set detailsToAdd; - private final MetricMetadata metricMetadata; - private final boolean useLocalTime; - private final ImmutableMap defaultDimensions; - - protected SignalFxReporter(MetricRegistry registry, String name, MetricFilter filter, - TimeUnit rateUnit, TimeUnit durationUnit, - AggregateMetricSender aggregateMetricSender, - Set detailsToAdd, - MetricMetadata metricMetadata) { - this(registry, name, filter, rateUnit, durationUnit, aggregateMetricSender, detailsToAdd, - metricMetadata, false, Collections. emptyMap()); - } - - public SignalFxReporter(MetricRegistry registry, String name, MetricFilter filter, - TimeUnit rateUnit, TimeUnit durationUnit, - AggregateMetricSender aggregateMetricSender, - Set detailsToAdd, MetricMetadata metricMetadata, - boolean useLocalTime, Map defaultDimensions) { - super(registry, name, filter, rateUnit, durationUnit); - this.aggregateMetricSender = aggregateMetricSender; - this.useLocalTime = useLocalTime; - this.detailsToAdd = detailsToAdd; - this.metricMetadata = metricMetadata; - this.defaultDimensions = ImmutableMap.copyOf(defaultDimensions); - } - - @Override - public void report(SortedMap gauges, SortedMap counters, - SortedMap histograms, SortedMap meters, - SortedMap timers) { - AggregateMetricSenderSessionWrapper session = new AggregateMetricSenderSessionWrapper( - aggregateMetricSender.createSession(), Collections.unmodifiableSet(detailsToAdd), metricMetadata, - aggregateMetricSender.getDefaultSourceName(), "sf_source", useLocalTime, defaultDimensions); - - try { - for (Map.Entry entry : gauges.entrySet()) { - session.addMetric(entry.getValue(), entry.getKey(), - SignalFxProtocolBuffers.MetricType.GAUGE, entry.getValue().getValue()); - } - for (Map.Entry entry : counters.entrySet()) { - if (entry.getValue() instanceof IncrementalCounter) { - session.addMetric(entry.getValue(), entry.getKey(), - SignalFxProtocolBuffers.MetricType.COUNTER, - ((IncrementalCounter)entry.getValue()).getCountChange()); - } else { - session.addMetric(entry.getValue(), entry.getKey(), - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, - entry.getValue().getCount()); - } - } - for (Map.Entry entry : histograms.entrySet()) { - session.addHistogram(entry.getKey(), entry.getValue()); - } - for (Map.Entry entry : meters.entrySet()) { - session.addMetered(entry.getKey(), entry.getValue()); - } - for (Map.Entry entry : timers.entrySet()) { - session.addTimer(entry.getKey(), entry.getValue()); - } - } finally { - try { - session.close(); - } catch (Exception e) { - // Unable to register... these exceptions handled by AggregateMetricSender - } - } - } - - public MetricMetadata getMetricMetadata() { - return metricMetadata; - } - - public enum MetricDetails { - // For {@link com.codahale.metrics.Sampling} - MEDIAN("median"), - PERCENT_75("75th"), - PERCENT_95("95th"), - PERCENT_98("98th"), - PERCENT_99("99th"), - PERCENT_999("999th"), - MAX("max"), - MIN("min"), - STD_DEV("stddev"), - MEAN("mean"), - - // For {@link com.codahale.metrics.Counting} - COUNT("count"), - - // For {@link com.codahale.metrics.Metered} - RATE_MEAN("rate.mean"), - RATE_1_MIN("rate.1min"), - RATE_5_MIN("rate.5min"), - RATE_15_MIN("rate.15min"); - public static final Set ALL = Collections.unmodifiableSet(EnumSet.allOf(MetricDetails.class)); - public static final Set DEFAULTS = ImmutableSet.of(COUNT, MIN, MEAN, MAX); - - private final String description; - - MetricDetails(String description) { - this.description = description; - } - - public String getDescription() { - return description; - } - } - - public static final class Builder { - private final MetricRegistry registry; - private String defaultSourceName; - private AuthToken authToken; - private SignalFxReceiverEndpoint endpoint = new SignalFxEndpoint(); - private String name = "signalfx-reporter"; - private int timeoutMs = HttpDataPointProtobufReceiverFactory.DEFAULT_TIMEOUT_MS; - private DataPointReceiverFactory dataPointReceiverFactory = new - HttpDataPointProtobufReceiverFactory(endpoint); - private MetricFilter filter = MetricFilter.ALL; - private TimeUnit rateUnit = TimeUnit.SECONDS; - private TimeUnit durationUnit = TimeUnit.MILLISECONDS; // Maybe nano eventually? - private Set detailsToAdd = MetricDetails.DEFAULTS; - private Collection onSendErrorHandlerCollection = Collections.emptyList(); - private MetricMetadata metricMetadata = new MetricMetadataImpl(); - private boolean useLocalTime = false; - private final ImmutableMap.Builder defaultDimensions = new ImmutableMap.Builder(); - - public Builder(MetricRegistry registry, String authToken) { - this(registry, new StaticAuthToken(authToken)); - } - - public Builder(MetricRegistry registry, AuthToken authToken) { - this(registry, authToken, SourceNameHelper.getDefaultSourceName()); - } - - public Builder(MetricRegistry registry, AuthToken authToken, String defaultSourceName) { - this.registry = registry; - this.authToken = authToken; - this.defaultSourceName = requireNonNull(defaultSourceName, "defaultSourceName must be a non-null value"); - } - - public Builder setDefaultSourceName(String defaultSourceName) { - this.defaultSourceName = requireNonNull(defaultSourceName, "defaultSourceName must be a non-null value"); - return this; - } - - public Builder setAuthToken(AuthToken authToken) { - this.authToken = authToken; - return this; - } - - public Builder setEndpoint(SignalFxReceiverEndpoint endpoint) { - this.endpoint = endpoint; - this.dataPointReceiverFactory = - new HttpDataPointProtobufReceiverFactory(endpoint) - .setTimeoutMs(this.timeoutMs); - return this; - } - - public Builder setName(String name) { - this.name = name; - return this; - } - - public Builder setTimeoutMs(int timeoutMs) { - this.timeoutMs = timeoutMs; - this.dataPointReceiverFactory = - new HttpDataPointProtobufReceiverFactory(endpoint) - .setTimeoutMs(this.timeoutMs); - return this; - } - - @Deprecated - public Builder setVersion(int version) { - return this; - } - - public Builder setDataPointReceiverFactory( - DataPointReceiverFactory dataPointReceiverFactory) { - this.dataPointReceiverFactory = dataPointReceiverFactory; - return this; - } - - public Builder setFilter(MetricFilter filter) { - this.filter = filter; - return this; - } - - public Builder setRateUnit(TimeUnit rateUnit) { - this.rateUnit = rateUnit; - return this; - } - - public Builder setDurationUnit(TimeUnit durationUnit) { - this.durationUnit = durationUnit; - return this; - } - - public Builder setDetailsToAdd(Set detailsToAdd) { - this.detailsToAdd = detailsToAdd; - return this; - } - - public Builder setOnSendErrorHandlerCollection( - Collection onSendErrorHandlerCollection) { - this.onSendErrorHandlerCollection = onSendErrorHandlerCollection; - return this; - } - - public Builder setMetricMetadata(MetricMetadata metricMetadata) { - this.metricMetadata = metricMetadata; - return this; - } - - /** - * Will use the local system time, rather than zero, on sent datapoints. - * @param useLocalTime If true, use local system time - * @return this - */ - public Builder useLocalTime(boolean useLocalTime) { - this.useLocalTime = useLocalTime; - return this; - } - - /** - * Adds all dimensions to the default dimensions to be sent with every datapoint from this - * reporter. This means they will be added to distributed counters (as opposed to cumulative counters), - * which means that if there is any dimension in this map which is unique to the emitter (i.e a hostname) - * then the distributed counter will not aggregate as expected. For dimension that are unique to each emitter use - * either {@link #addUniqueDimension(String, String)} or {@link #addUniqueDimensions(Map)} - * - * @param dimensions - * non-null map of string value pairs - * @return this - */ - public Builder addDimensions(Map dimensions) { - // loop here to get "null value" protection of addDimension - for (Map.Entry entry: dimensions.entrySet()) { - this.addDimension(entry.getKey(), entry.getValue()); - } - return this; - } - - /** - * Adds a dimension to the default dimensions to be sent with every datapoint from this - * reporter. This means they will be added to distributed counters (as opposed to cumulative counters), - * which means that if the name and value is unique to the emitter (i.e a hostname) - * then the distributed counter will not aggregate as expected. For dimension that are unique to each emitter use - * {@link #addUniqueDimension(String, String)} - * - * @param name - * Name of the dimension - * @param value - * Value of the dimension. If null then the dimension is not added. - * @return this - */ - public Builder addDimension(String name, String value) { - if (value != null) { - this.defaultDimensions.put(name, DimensionInclusion.shared(value)); - } - return this; - } - - /** - * Adds all dimensions to the default dimensions to be sent with every datapoint that is not a distributed counter. - * This method should be used for adding dimensions which are unique to the emitter (such as host name), for shared - * dimensions which can be sent with distributed counters use either {@link #addDimension(String, String)} or {@link #addDimensions(Map)} - * @param dimensions - * non-null map of string value pairs - * @return this - */ - public Builder addUniqueDimensions(Map dimensions) { - // loop here to get "null value" protection of addDimension - for (Map.Entry entry: dimensions.entrySet()) { - this.addUniqueDimension(entry.getKey(), entry.getValue()); - } - return this; - } - - /** - * Adds a dimension to the default dimensions to be sent with every datapoint that is not a distributed counter. - * This method should be used for adding dimensions which are unique to the emitter (such as host name), for shared - * dimensions use {@link #addDimension(String, String)} - * - * @param name - * Name of the dimension - * @param value - * Value of the dimension. If null then the dimension is not added. - * @return this - */ - public Builder addUniqueDimension(String name, String value) { - if (value != null) { - this.defaultDimensions.put(name, DimensionInclusion.unique(value)); - } - return this; - } - - public SignalFxReporter build() { - AggregateMetricSender aggregateMetricSender = new AggregateMetricSender( - defaultSourceName, dataPointReceiverFactory, authToken, - onSendErrorHandlerCollection); - return new SignalFxReporter(registry, name, filter, rateUnit, durationUnit, - aggregateMetricSender, detailsToAdd, metricMetadata, useLocalTime, - defaultDimensions.build()); - } - } -} diff --git a/signalfx-codahale/src/main/java/com/signalfx/codahale/util/BasicJvmMetrics.java b/signalfx-codahale/src/main/java/com/signalfx/codahale/util/BasicJvmMetrics.java deleted file mode 100644 index 54ac83e3..00000000 --- a/signalfx-codahale/src/main/java/com/signalfx/codahale/util/BasicJvmMetrics.java +++ /dev/null @@ -1,324 +0,0 @@ -/** - * Copyright (C) 2014 SignalFx, Inc. - */ -package com.signalfx.codahale.util; - - -import java.lang.management.GarbageCollectorMXBean; -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryMXBean; -import java.lang.management.RuntimeMXBean; -import java.lang.management.ThreadMXBean; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import com.codahale.metrics.Gauge; -import com.codahale.metrics.MetricRegistry; - - -/** - * Report a basic set of JVM metrics to SignalFx. - * - */ -public class BasicJvmMetrics { - /** - * Report JVM uptime (milliseconds). - */ - public final Gauge uptimeGauge; - /** - * Reports total memory used by the JVM heap (bytes). - */ - public final Gauge totalMemoryGauge; - /** - * Reports current in-use memory in the JVP heap (bytes). - */ - public final Gauge usedMemoryGauge; - /** - * Reports maximum size of JVM heap (bytes). - */ - public final Gauge maxMemoryGauge; - /** - * Reports current CPU load (percent, normalized by number of CPUs available to the JVM. - */ - public final Gauge cpuLoadGauge; - /** - * Reports total number of user and daemon threads. - */ - public final Gauge totalThreadCountGauge; - /** - * Reports number of daemon threads. - */ - public final Gauge daemonThreadCountGauge; - /** - * Reports total time spent in garbage collection (nanoseconds). - */ - public final Gauge gcTimeGauge; - /** - * Reports number of young-generation garbage collections. - */ - public final Gauge gcYoungCountGauge; - /** - * Reports number of old-generation garbage collections. - */ - public final Gauge gcOldCountGauge; - /** - * Reports current GC load (percent, normalized by number of CPUs available to the JVM. - */ - public final Gauge gcLoadGauge; - - private final RuntimeMXBean runtimeBean; - private final MemoryMXBean memoryBean; - private final ThreadMXBean threadBean; - private final List oldGenGcBeans = new ArrayList(); - private final List youngGenGcBeans = new ArrayList(); - private final List allGcBeans = new ArrayList(); - - // observed name of the old generation memory pool. - private static final String OLD_GEN_POOL_NAME = "PS Old Gen"; - - /** - * Construct the basic JVM metrics using a supplied SignalFx MetricFactory. - * - * @param metricRegistry The registry to give these metrics to - */ - public BasicJvmMetrics(MetricRegistry metricRegistry) { - - runtimeBean = ManagementFactory.getRuntimeMXBean(); - memoryBean = ManagementFactory.getMemoryMXBean(); - threadBean = ManagementFactory.getThreadMXBean(); - - for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { - allGcBeans.add(gcBean); - - Set poolNames = new HashSet(Arrays.asList(gcBean.getMemoryPoolNames())); - - if (poolNames.contains(OLD_GEN_POOL_NAME)) { - // We'll count garbage collectors managing the OLD_GEN_POOL_NAME as 'old generation' - oldGenGcBeans.add(gcBean); - } else { - // and all others as 'young generation' - youngGenGcBeans.add(gcBean); - } - } - - this.uptimeGauge = createPeriodicGauge(metricRegistry, "jvm.uptime", new UptimeCallback()); - - this.totalMemoryGauge = createPeriodicGauge(metricRegistry, "jvm.heap.size", - new TotalMemoryCallback()); - - this.usedMemoryGauge = createPeriodicGauge(metricRegistry, "jvm.heap.used", - new UsedMemoryCallback()); - - this.maxMemoryGauge = createPeriodicGauge(metricRegistry, "jvm.heap.max", - new MaxMemoryCallback()); - - this.cpuLoadGauge = createDoublePeriodicGauge(metricRegistry, "jvm.cpu.load", new CpuLoadCallback()); - - this.totalThreadCountGauge = createIntegerPeriodicGauge(metricRegistry, "jvm.threads.total", - new TotalThreadCountCallback()); - - this.daemonThreadCountGauge = createIntegerPeriodicGauge(metricRegistry, "jvm.threads.daemon", - new DaemonThreadCountCallback()); - - this.gcTimeGauge = createPeriodicGauge(metricRegistry, "jvm.gc.time", new GcTimeCallback()); - - this.gcLoadGauge = createDoublePeriodicGauge(metricRegistry, "jvm.gc.load", new GcLoadCallback()); - - this.gcYoungCountGauge = createPeriodicGauge(metricRegistry, "jvm.gc.young.count", - new GcCountCallback(youngGenGcBeans)); - - this.gcOldCountGauge = createPeriodicGauge(metricRegistry, "jvm.gc.old.count", - new GcCountCallback(oldGenGcBeans)); - } - - private Gauge createPeriodicGauge(MetricRegistry metricRegistry, String name, - Gauge gauge) { - return metricRegistry.register(name, gauge); - } - - private Gauge createIntegerPeriodicGauge(MetricRegistry metricRegistry, String name, - Gauge gauge) { - return metricRegistry.register(name, gauge); - } - - private Gauge createDoublePeriodicGauge(MetricRegistry metricRegistry, String name, - Gauge gauge) { - return metricRegistry.register(name, gauge); - } - - private class UptimeCallback implements Gauge { - @Override - public Long getValue() { - return runtimeBean.getUptime(); - } - } - - private class TotalMemoryCallback implements Gauge { - @Override - public Long getValue() { - return memoryBean.getHeapMemoryUsage().getCommitted(); - } - } - - private class UsedMemoryCallback implements Gauge { - @Override - public Long getValue() { - return memoryBean.getHeapMemoryUsage().getUsed(); - } - } - - private class MaxMemoryCallback implements Gauge { - @Override - public Long getValue() { - return memoryBean.getHeapMemoryUsage().getMax(); - } - } - - private class TotalThreadCountCallback implements Gauge { - @Override - public Integer getValue() { - return threadBean.getThreadCount(); - } - } - - private class DaemonThreadCountCallback implements Gauge { - @Override - public Integer getValue() { - return threadBean.getDaemonThreadCount(); - } - } - - private class GcTimeCallback implements Gauge { - @Override - public Long getValue() { - long total = 0; - for (GarbageCollectorMXBean gcBean : allGcBeans) { - long sample = gcBean.getCollectionTime(); - if (sample > 0) { - total += sample; - } - } - return total; - } - } - - private static class GcCountCallback implements Gauge { - final private List gcBeans; - - private GcCountCallback(List gcBeans) { - this.gcBeans = gcBeans; - } - - @Override - public Long getValue() { - long total = 0; - for (GarbageCollectorMXBean gcBean : gcBeans) { - long sample = gcBean.getCollectionCount(); - if (sample > 0) { - total += sample; - } - } - return total; - } - } - - private abstract class LoadCallback implements Gauge { - private static final int PERCENT = 100; - - private long previousTime; - private double previousValue; - private Map samples = new HashMap(); - private final TimeUnit timeUnit; - - LoadCallback(TimeUnit timeUnit) { - this.previousTime = 0; - this.previousValue = 0; - this.timeUnit = timeUnit; - } - - protected abstract Map getSamples(); - - private long computeDelta(Map newSamples) { - long delta = 0; - - for (Map.Entry entry : newSamples.entrySet()) { - T key = entry.getKey(); - Long sample = entry.getValue(); - if (sample < 0) { - // not valid for this key - } else { - Long previous = samples.get(key); - if (previous == null) { - delta += sample; // first sample - } else { - delta += sample - previous; - } - } - } - samples = newSamples; - - return delta; - } - - @Override - public Double getValue() { - long time = runtimeBean.getUptime(); - long deltaTime = time - previousTime; - - if (deltaTime < 100) { - return previousValue; - } - - Map samples = getSamples(); - - long deltaLoad = computeDelta(samples); - - previousValue = (double) PERCENT * timeUnit.toNanos(deltaLoad) - / TimeUnit.MILLISECONDS.toNanos(deltaTime) - / Runtime.getRuntime().availableProcessors(); - - previousTime = time; - - return previousValue; - } - } - - // com.sun.management.OperatingSystemMXBean has a getProcessCpuTime() - // but java.lang.management.OperatingSystemMXBean does not - private class CpuLoadCallback extends LoadCallback { - CpuLoadCallback() { - super(TimeUnit.NANOSECONDS); - } - - @Override - protected Map getSamples() { - Map samples = new HashMap(); - for (long threadId : threadBean.getAllThreadIds()) { - samples.put(threadId, threadBean.getThreadCpuTime(threadId)); - } - return samples; - } - } - - // factor stuff out of this and CpuLoadCallback - private class GcLoadCallback extends LoadCallback { - GcLoadCallback() { - super(TimeUnit.MILLISECONDS); - } - - @Override - protected Map getSamples() { - Map samples = new HashMap(); - for (GarbageCollectorMXBean gcBean : allGcBeans) { - samples.put(gcBean.getName(), gcBean.getCollectionTime()); - } - return samples; - } - } -} diff --git a/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/MetricMetadataTest.java b/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/MetricMetadataTest.java deleted file mode 100644 index c404a553..00000000 --- a/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/MetricMetadataTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright (C) 2017 SignalFx, Inc. All rights reserved. - */ -package com.signalfx.codahale.metrics; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.Metric; -import com.codahale.metrics.MetricRegistry; -import com.signalfx.codahale.reporter.MetricMetadata; -import com.signalfx.codahale.reporter.MetricMetadataImpl; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType; - -public class MetricMetadataTest { - - @Test - public void testRemoveExisting() { - MetricRegistry metricRegistry = new MetricRegistry(); - MetricMetadata metadata = new MetricMetadataImpl(); - Metric metric = metadata.forBuilder(SettableLongGauge.Builder.INSTANCE) - .withMetricName("gauge").withDimension("host", "myhost") - .withMetricType(MetricType.GAUGE).createOrGet(metricRegistry); - assertFalse(metricRegistry.getMetrics().isEmpty()); - assertTrue(metadata.getMetricType(metric).isPresent()); - assertTrue(metadata.removeMetric(metric, metricRegistry)); - assertFalse(metadata.getMetricType(metric).isPresent()); - assertTrue(metricRegistry.getMetrics().isEmpty()); - } - - @Test - public void testRemoveMissing() { - MetricRegistry metricRegistry = new MetricRegistry(); - MetricMetadata metadata = new MetricMetadataImpl(); - Counter counter = metricRegistry.counter("counter"); - assertFalse(metadata.removeMetric(counter, metricRegistry)); - } -} diff --git a/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java b/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java deleted file mode 100644 index ce437d0b..00000000 --- a/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java +++ /dev/null @@ -1,325 +0,0 @@ -package com.signalfx.codahale.metrics; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.Gauge; -import com.codahale.metrics.Histogram; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.Timer; -import com.google.common.collect.ImmutableSet; -import com.signalfx.codahale.SfxMetrics; -import com.signalfx.codahale.reporter.IncrementalCounter; -import com.signalfx.codahale.reporter.SignalFxReporter; -import com.signalfx.metrics.auth.StaticAuthToken; -import com.signalfx.metrics.connection.StaticDataPointReceiverFactory; -import com.signalfx.metrics.connection.StoredDataPointReceiver; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; -import java.util.concurrent.TimeUnit; -import org.junit.Before; -import org.junit.Test; - -public class SignalFxReporterTest { - - private static final String SOURCE_NAME = "myserver"; - - private MetricRegistry metricRegistry; - private SignalFxReporter reporter; - private SfxMetrics sfxMetrics; - private StoredDataPointReceiver dbank; - - @Before - public void setup() { - metricRegistry = new MetricRegistry(); - dbank = new StoredDataPointReceiver(); - reporter = new SignalFxReporter - .Builder(metricRegistry, new StaticAuthToken(""), SOURCE_NAME) - .setDataPointReceiverFactory(new StaticDataPointReceiverFactory(dbank)) - .setDetailsToAdd(ImmutableSet.of(SignalFxReporter.MetricDetails.COUNT, - SignalFxReporter.MetricDetails.MIN, - SignalFxReporter.MetricDetails.MAX)) - .build(); - sfxMetrics = new SfxMetrics(metricRegistry, reporter.getMetricMetadata()); - } - - @Test - public void testEmptyDataBank() { - StoredDataPointReceiver dbank = new StoredDataPointReceiver(); - assertEquals(0, dbank.addDataPoints.size()); - } - - @Test - public void testGauge() { - Gauge gauge = sfxMetrics.registerGauge("gauge", new Gauge() { - @Override - public Integer getValue() { - return 1; - } - }); - reporter.report(); - assertEquals(1, dbank.addDataPoints.size()); - assertTrue(sfxMetrics.unregister(gauge)); - } - - @Test - public void testCounter() { - Counter counter = sfxMetrics.counter("counter"); - counter.inc(); - counter.inc(); - reporter.report(); - assertEquals(1, dbank.addDataPoints.size()); - // counter without dimensions doesn't get into MetricMetadata, so we cannot unregister - // from SfxMetrics. But we can remove it from MetricRegistry. - assertFalse(sfxMetrics.unregister(counter)); - assertTrue(metricRegistry.remove("counter")); - } - - @Test - public void testCounterWithDimensions() { - Counter counter = sfxMetrics.counter("counter", "dimName", "dimValue"); - counter.inc(); - counter.inc(); - reporter.report(); - assertEquals(1, dbank.addDataPoints.size()); - assertTrue(sfxMetrics.unregister(counter)); - } - - @Test - public void testRawCounter() { - Counter rawCounter = reporter.getMetricMetadata() - .forMetric(metricRegistry.counter("rawCounter")) - .withMetricType(SignalFxProtocolBuffers.MetricType.COUNTER).metric(); - rawCounter.inc(10); - reporter.report(); - rawCounter.inc(14); - reporter.report(); - // Users have to use an IncrementalCounter if they want to see the count value of 14 each time - assertEquals(24, dbank.lastValueFor(SOURCE_NAME, "rawCounter").getIntValue()); - assertEquals(SignalFxProtocolBuffers.MetricType.COUNTER, - dbank.registeredMetrics.get("rawCounter")); - } - - @Test - public void testIncrementalCounter() { - IncrementalCounter incrementalCounter = sfxMetrics - .incrementalCounter("incrementalCounter"); - incrementalCounter.inc(3); - assertEquals(false, sfxMetrics.unregister(new Counter())); - reporter.report(); - assertEquals(1, dbank.addDataPoints.size()); - assertEquals(3, dbank.lastValueFor(SOURCE_NAME, "incrementalCounter").getIntValue()); - } - - @Test - public void testTimer() throws Exception { - Timer timer = sfxMetrics.timer("timer", "dimName", "dimValue"); - // track time taken. - for (int i = 0; i < 4; i++) { - Timer.Context context = timer.time(); - try { - Thread.sleep(10 + i * 10); - } finally { - context.close(); - } - } - /* - Java 7 alternative: - try (Timer.Context ignored = t.time()) { - System.out.println("Doing store things"); - } - */ - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, - dbank.registeredMetrics.get("timer.count")); - assertEquals(SignalFxProtocolBuffers.MetricType.GAUGE, - dbank.registeredMetrics.get("timer.max")); - assertEquals(SignalFxProtocolBuffers.MetricType.GAUGE, - dbank.registeredMetrics.get("timer.min")); - assertTrue(sfxMetrics.unregister(timer)); - } - - @Test - public void testResettingTimer() { - Timer resettingTimer = sfxMetrics.resettingTimer("resettingTimer", "dimName", "dimValue"); - resettingTimer.update(20, TimeUnit.MILLISECONDS); - resettingTimer.update(30, TimeUnit.MILLISECONDS); - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(2, dbank.lastValueFor(SOURCE_NAME, "resettingTimer.count").getIntValue()); - assertEquals(20000000, dbank.lastValueFor(SOURCE_NAME, "resettingTimer.min").getIntValue()); - assertEquals(30000000, dbank.lastValueFor(SOURCE_NAME, "resettingTimer.max").getIntValue()); - dbank.addDataPoints.clear(); - resettingTimer.update(25, TimeUnit.MILLISECONDS); - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(3, dbank.lastValueFor(SOURCE_NAME, "resettingTimer.count").getIntValue()); - assertEquals(25000000, dbank.lastValueFor(SOURCE_NAME, "resettingTimer.min").getIntValue()); - assertEquals(25000000, dbank.lastValueFor(SOURCE_NAME, "resettingTimer.max").getIntValue()); - } - - @Test - public void testHistogram() { - Histogram histogram = sfxMetrics.histogram("histogram", "dimName", "dimValue"); - histogram.update(20); - histogram.update(30); - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(2, dbank.lastValueFor(SOURCE_NAME, "histogram.count").getIntValue()); - assertEquals(20, dbank.lastValueFor(SOURCE_NAME, "histogram.min").getIntValue()); - assertEquals(30, dbank.lastValueFor(SOURCE_NAME, "histogram.max").getIntValue()); - dbank.addDataPoints.clear(); - histogram.update(25); - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(3, dbank.lastValueFor(SOURCE_NAME, "histogram.count").getIntValue()); - assertEquals(20, dbank.lastValueFor(SOURCE_NAME, "histogram.min").getIntValue()); - assertEquals(30, dbank.lastValueFor(SOURCE_NAME, "histogram.max").getIntValue()); - } - - @Test - public void testResettingHistogram() { - Histogram resettingHistogram = sfxMetrics.resettingHistogram("resettingHistogram"); - resettingHistogram.update(20); - resettingHistogram.update(30); - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(2, dbank.lastValueFor(SOURCE_NAME, "resettingHistogram.count").getIntValue()); - assertEquals(20, dbank.lastValueFor(SOURCE_NAME, "resettingHistogram.min").getIntValue()); - assertEquals(30, dbank.lastValueFor(SOURCE_NAME, "resettingHistogram.max").getIntValue()); - dbank.addDataPoints.clear(); - resettingHistogram.update(25); - reporter.report(); - assertEquals(3, dbank.addDataPoints.size()); - assertEquals(3, dbank.lastValueFor(SOURCE_NAME, "resettingHistogram.count").getIntValue()); - assertEquals(25, dbank.lastValueFor(SOURCE_NAME, "resettingHistogram.min").getIntValue()); - assertEquals(25, dbank.lastValueFor(SOURCE_NAME, "resettingHistogram.max").getIntValue()); - } - - @Test - public void testMultipleRegistersOfResettingHistogramWithSameName() { - Histogram resettingHistogram = sfxMetrics.resettingHistogram("resettingHistogram"); - resettingHistogram.update(20); - Histogram resettingHistogram1 = sfxMetrics.resettingHistogram("resettingHistogram"); - assertEquals(resettingHistogram, resettingHistogram1); - assertEquals(1, resettingHistogram1.getCount()); - assertEquals(20, resettingHistogram1.getSnapshot().getMax()); - - Histogram resettingHistogramWithDims = sfxMetrics.resettingHistogram("resettingHistogram", "foo", "bar"); - assertNotEquals(resettingHistogram, resettingHistogramWithDims); - resettingHistogramWithDims.update(30); - Histogram resettingHistogramWithDims1 = sfxMetrics.resettingHistogram("resettingHistogram", "foo", "bar"); - assertEquals(1, resettingHistogramWithDims1.getCount()); - assertEquals(30, resettingHistogramWithDims1.getSnapshot().getMax()); - } - - @Test - public void testMultipleRegistersOfResettingTimerWithSameName() { - Timer resettingTimer = sfxMetrics.resettingTimer("resettingTimer"); - long timeInNanos = TimeUnit.NANOSECONDS.convert(20, TimeUnit.SECONDS); - resettingTimer.update(timeInNanos, TimeUnit.NANOSECONDS); - Timer resettingTimer1 = sfxMetrics.resettingTimer("resettingTimer"); - assertEquals(resettingTimer, resettingTimer1); - assertEquals(1, resettingTimer1.getCount()); - assertEquals(timeInNanos, resettingTimer1.getSnapshot().getMax()); - - Timer resettingTimerWithDims = sfxMetrics.resettingTimer("resettingTimer", "foo", "bar"); - assertNotEquals(resettingTimer, resettingTimerWithDims); - timeInNanos = TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS); - resettingTimerWithDims.update(timeInNanos, TimeUnit.NANOSECONDS); - Timer resettingTimerWithDims1 = sfxMetrics.resettingTimer("resettingTimer", "foo", "bar"); - assertEquals(1, resettingTimerWithDims1.getCount()); - assertEquals(timeInNanos, resettingTimerWithDims1.getSnapshot().getMax()); - } - - @Test(expected = IllegalArgumentException.class) - public void testDuplicateMetricRegistrationAroundResettingHistogramTimers() { - sfxMetrics.resettingHistogram("countstuff"); - sfxMetrics.resettingTimer("countstuff"); - fail("I expect an already registered metric on this name"); - } - - @Test - public void testReportingGaugeAsCummulativeCounter() { - String cumulativeCounterCallback = "cumulativeCounterCallback"; - sfxMetrics.registerGaugeAsCumulativeCounter(cumulativeCounterCallback, - new Gauge() { - private long i = 0; - - @Override - public Long getValue() { - return i++; - } - }); - reporter.report(); - assertEquals(0, dbank.lastValueFor(SOURCE_NAME, cumulativeCounterCallback).getIntValue()); - reporter.report(); - reporter.report(); - assertEquals(2, dbank.lastValueFor(SOURCE_NAME, cumulativeCounterCallback).getIntValue()); - assertEquals(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, - dbank.registeredMetrics.get(cumulativeCounterCallback)); - } - - @Test(expected = IllegalArgumentException.class) - public void testDuplicateMetricRegistration() { - sfxMetrics.counter("countstuff"); - sfxMetrics.incrementalCounter("countstuff"); - fail("I expect an already registered metric on this name"); - } - - @Test(expected = IllegalArgumentException.class) - public void testUpdatingMetricTypeToIncompatibleValue() { - reporter.getMetricMetadata() - .forBuilder(IncrementalCounter.Builder.INSTANCE) - .withSourceName(SOURCE_NAME) - .withMetricName("name") - .createOrGet(metricRegistry) - .inc(3); - reporter.getMetricMetadata() - .forBuilder(IncrementalCounter.Builder.INSTANCE) - .withSourceName(SOURCE_NAME) - .withMetricName("name") - .withMetricType(SignalFxProtocolBuffers.MetricType.GAUGE) - .createOrGet(metricRegistry); - fail("I expect an error if it's a gauge"); - } - - @Test(expected = IllegalArgumentException.class) - public void testCreateOrGetMetricSameNameDifferentType() { - reporter.getMetricMetadata().forBuilder(IncrementalCounter.Builder.INSTANCE) - .withSourceName("asource") - .withMetricName("name") - .createOrGet(metricRegistry) - .inc(1); - reporter.getMetricMetadata().forBuilder(SettableLongGauge.Builder.INSTANCE) - .withSourceName("asource") - .withMetricName("name") - .createOrGet(metricRegistry); - fail("We shouldn't be able to make it with the same name and different type"); - } - - @Test - public void shouldFailOnNullDefaultSourceMethod_constructor() { - try { - new SignalFxReporter.Builder(null, null, null); - fail("NPE was expected"); - } catch (NullPointerException npe) { - assertTrue(npe.getMessage().contains("defaultSourceName")); - } - } - - @Test - public void shouldFailOnNullDefaultSourceMethod_method() { - try { - new SignalFxReporter.Builder(null, "test") - .setDefaultSourceName(null); - fail("NPE was expected"); - } catch (NullPointerException npe) { - assertTrue(npe.getMessage().contains("defaultSourceName")); - } - } -} diff --git a/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/util/BasicJvmMetrisTest.java b/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/util/BasicJvmMetrisTest.java deleted file mode 100644 index 92e20d3d..00000000 --- a/signalfx-codahale/src/test/java/com/signalfx/codahale/metrics/util/BasicJvmMetrisTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (C) 2014-2016 SignalFx, Inc. All rights reserved. - */ -package com.signalfx.codahale.metrics.util; - -import java.util.Map; -import java.util.SortedMap; -import java.util.concurrent.TimeUnit; - -import org.junit.Assert; -import org.junit.Test; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.Gauge; -import com.codahale.metrics.Histogram; -import com.codahale.metrics.Meter; -import com.codahale.metrics.MetricFilter; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.ScheduledReporter; -import com.codahale.metrics.Timer; -import com.signalfx.codahale.util.BasicJvmMetrics; - -public class BasicJvmMetrisTest { - - @Test - public void testPointsSent() throws Exception { - MetricRegistry registry = new MetricRegistry(); - new BasicJvmMetrics(registry); - - ScheduledReporter reporter = new ScheduledReporter(registry, "test", MetricFilter.ALL, - TimeUnit.SECONDS, TimeUnit.MILLISECONDS) { - - @Override - public void report(SortedMap gauges, SortedMap counters, - SortedMap histograms, - SortedMap meters, SortedMap timers) { - Assert.assertFalse(gauges.isEmpty()); - Assert.assertNotNull(gauges.get("jvm.uptime")); - for (Map.Entry entry : gauges.entrySet()) { - Assert.assertNotNull(entry.getValue().getValue()); - } - } - }; - - reporter.report(); - reporter.close(); - } -} diff --git a/signalfx-yammer/README.md b/signalfx-yammer/README.md deleted file mode 100644 index a9b56678..00000000 --- a/signalfx-yammer/README.md +++ /dev/null @@ -1,11 +0,0 @@ - -# signalfx-yammer - -:warning: `signalfx-yammer` has been deprecated and will be deleted -in July 2024. No further releases will be available after June 2024. - -Users are encouraged to use the -[OpenTelemetry Java SDK](https://github.com/open-telemetry/opentelemetry-java) or the -[Splunk Distribution of OpenTelemetry Java Instrumentation](https://github.com/signalfx/splunk-otel-java) -to send metrics to Splunk. - diff --git a/signalfx-yammer/pom.xml b/signalfx-yammer/pom.xml deleted file mode 100644 index b8fc27a0..00000000 --- a/signalfx-yammer/pom.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - 4.0.0 - - - com.signalfx.public - clients-parent - 1.0.43 - - - signalfx-yammer - Yammer to SignalFx - jar - - Codahale yammer metrics plugin for signalfx / yammer metrics lib v2.0 - - - http://www.signalfx.com - - - - Apache License 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html - repo - - - - - scm:git:git@github.com:signalfx/signalfx-java.git - scm:git:git@github.com:signalfx/signalfx-java.git - git@github.com:signalfx/signalfx-java.git - - - - - signalfx - SignalFx - support+java@signalfx.com - SignalFx, Inc - http://www.signalfx.com - - - - - - - com.signalfx.public - signalfx-java - - - com.signalfx.public - signalfx-protoc - - - com.google.guava - guava - - - com.yammer.metrics - metrics-core - - - - - org.slf4j - slf4j-simple - - - org.hamcrest - hamcrest-core - - - junit - junit - - - diff --git a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java b/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java deleted file mode 100644 index 88434a29..00000000 --- a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/AggregateMetricSenderSessionWrapper.java +++ /dev/null @@ -1,364 +0,0 @@ -package com.signalfx.codahale.reporter; - -import java.io.Closeable; -import java.util.Map; -import java.util.Set; - -import com.google.common.base.Optional; -import com.google.common.base.Strings; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.signalfx.metrics.SignalFxMetricsException; -import com.signalfx.metrics.flush.AggregateMetricSender; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; -import com.yammer.metrics.core.Histogram; -import com.yammer.metrics.core.Metered; -import com.yammer.metrics.core.Metric; -import com.yammer.metrics.core.MetricName; -import com.yammer.metrics.core.Sampling; -import com.yammer.metrics.core.Timer; -import com.yammer.metrics.stats.Snapshot; - -/** - * - * class to Aggregate and Send metrics - * used by SignalFxReporter in report() method - * - */ - -class AggregateMetricSenderSessionWrapper implements Closeable { - private final AggregateMetricSender.Session metricSenderSession; - private final Set detailsToAdd; - private final MetricMetadata metricMetadata; - private final String defaultSourceName; - private final String sourceDimension; - private final boolean injectCurrentTimestamp; - private final boolean sendExtraMetricDimensions; - private final ImmutableMap defaultDimensions; - - AggregateMetricSenderSessionWrapper( - AggregateMetricSender.Session metricSenderSession, - Set detailsToAdd, - MetricMetadata metricMetadata, - String defaultSourceName, - String sourceDimension, - boolean injectCurrentTimestamp, - boolean sendExtraMetricDimensions, - ImmutableMap defaultDimensions) { - this.metricSenderSession = metricSenderSession; - this.detailsToAdd = detailsToAdd; - this.metricMetadata = metricMetadata; - this.defaultSourceName = defaultSourceName; - this.sourceDimension = sourceDimension; - this.injectCurrentTimestamp = injectCurrentTimestamp; - this.sendExtraMetricDimensions = sendExtraMetricDimensions; - this.defaultDimensions = defaultDimensions; - } - - @Override - public void close() { - try { - metricSenderSession.close(); - } catch (Exception e) { - throw new SignalFxMetricsException("Unable to close session and send metrics", e); - } - } - - // These three called from report - void addTimer(MetricName key, Timer value) { - addMetered(key, value); - addSampling(key, value); - } - - /** - * Add Histogram metric - * @param baseName - * @param histogram - */ - - void addHistogram(MetricName baseName, - Histogram histogram) { - addMetric(histogram, baseName, - Optional.of(SignalFxReporter.MetricDetails.COUNT), - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, histogram.count()); - addSampling(baseName, histogram); - } - - /** - * Add Metered metric - * @param baseName - * @param metered - */ - - void addMetered(MetricName baseName, Metered metered) { - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.COUNT, - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, metered.count()); - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_15_MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.fifteenMinuteRate()); - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_1_MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.oneMinuteRate()); - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_5_MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.fiveMinuteRate()); - - addMetric(metered, baseName, - SignalFxReporter.MetricDetails.RATE_MEAN, - SignalFxProtocolBuffers.MetricType.GAUGE, metered.meanRate()); - } - - /** - * Add sampling - * @param baseName - * @param sampling - */ - - private void addSampling(MetricName baseName, Sampling sampling) { - Metric metric = (Metric)sampling; - final Snapshot snapshot = sampling.getSnapshot(); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MEDIAN, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMedian()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_75, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get75thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_95, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get95thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_98, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get98thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_99, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get99thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.PERCENT_999, - SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get999thPercentile()); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MAX, - SignalFxProtocolBuffers.MetricType.GAUGE, getMax(snapshot)); - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MIN, - SignalFxProtocolBuffers.MetricType.GAUGE, getMin(snapshot)); - - - // These are slower to calculate. Only calculate if we need. - if (detailsToAdd.contains(SignalFxReporter.MetricDetails.STD_DEV)) { - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.STD_DEV, - SignalFxProtocolBuffers.MetricType.GAUGE, getStdDev(snapshot)); - } - if (detailsToAdd.contains(SignalFxReporter.MetricDetails.MEAN)) { - addMetric(metric, baseName, - SignalFxReporter.MetricDetails.MEAN, - SignalFxProtocolBuffers.MetricType.GAUGE, getMean(snapshot)); - } - } - - /** - * Add metric - * @param metric - * @param codahaleName - * @param defaultMetricType - * @param originalValue - */ - - void addMetric(Metric metric, MetricName codahaleName, - SignalFxProtocolBuffers.MetricType defaultMetricType, - Object originalValue) { - addMetric(metric, codahaleName, Optional.absent(), - defaultMetricType, originalValue); - } - - /** - * Add metric - * @param metric - * @param codahaleName - * @param metricDetails - * @param defaultMetricType - * @param originalValue - */ - - private void addMetric(Metric metric, MetricName codahaleName, SignalFxReporter.MetricDetails metricDetails, - SignalFxProtocolBuffers.MetricType defaultMetricType, - Object originalValue) { - addMetric(metric, codahaleName, Optional.of(metricDetails), - defaultMetricType, originalValue); - - } - - /** - * Add metric - * @param metric - * @param codahaleName - * @param metricDetails - * @param defaultMetricType - * @param originalValue - */ - - void addMetric(Metric metric, MetricName codahaleName, - Optional metricDetails, - SignalFxProtocolBuffers.MetricType defaultMetricType, Object originalValue) { - final Number value; - if (originalValue instanceof Number) { - value = (Number) originalValue; - } else if (originalValue instanceof Boolean) { - value = ((Boolean)originalValue).booleanValue() ? 1 : 0; - } else { - // Unsupported type - return; - } - final String metricDetailsMetricNameSuffix; - if (metricDetails.isPresent()) { - if (!detailsToAdd.contains(metricDetails.get())) { - return; - } - metricDetailsMetricNameSuffix = "." + metricDetails.get().getDescription(); - } else { - metricDetailsMetricNameSuffix = ""; - } - Optional userSetMetricType = metricMetadata.getMetricType(metric); - SignalFxProtocolBuffers.MetricType metricType = userSetMetricType.or(defaultMetricType); - Map tags = metricMetadata.getTags(metric); - final String sourceName = Optional.fromNullable(tags.get(MetricMetadata.SOURCE)).or(defaultSourceName); - final String metricName = Optional.fromNullable(tags.get(MetricMetadata.METRIC)).or(codahaleName.getName()) + metricDetailsMetricNameSuffix; - - SignalFxProtocolBuffers.DataPoint.Builder builder = SignalFxProtocolBuffers.DataPoint - .newBuilder() - .setMetric(metricName) - .setMetricType(metricType); - - if (!sourceDimension.equals("") && !tags.containsKey(sourceDimension)) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder() - .setKey(sourceDimension).setValue(sourceName)); - } - - ImmutableSet ignoredDimensions = ImmutableSet.of( - MetricMetadata.SOURCE, MetricMetadata.METRIC); - - for (Map.Entry entry: tags.entrySet()) { - if (!ignoredDimensions.contains(entry.getKey())) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder() - .setKey(entry.getKey()).setValue(entry.getValue())); - } - } - - if (sendExtraMetricDimensions) { - if (!Strings.isNullOrEmpty(codahaleName.getGroup())) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder() - .setKey("metric_group").setValue(codahaleName.getGroup())); - } - if (!Strings.isNullOrEmpty(codahaleName.getType())) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder() - .setKey("metric_type").setValue(codahaleName.getType())); - } - } - - for (Map.Entry entry : defaultDimensions.entrySet()) { - String dimName = entry.getKey(); - String dimValue = entry.getValue(); - if (!ignoredDimensions.contains(dimName) && !tags.containsKey(dimName)) { - builder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder().setKey(dimName) - .setValue(dimValue)); - } - } - - if (injectCurrentTimestamp) { - final long currentTimestamp = System.currentTimeMillis(); - builder.setTimestamp(currentTimestamp); - } - - if (value instanceof Long || value instanceof Integer || value instanceof Short) { - builder.setValue(SignalFxProtocolBuffers.Datum - .newBuilder().setIntValue(value.longValue())); - } else { - final double doubleToSend = value.doubleValue(); - if (Double.isInfinite(doubleToSend) || Double.isNaN(doubleToSend)) { - return; - } - builder.setValue(SignalFxProtocolBuffers.Datum.newBuilder() - .setDoubleValue(doubleToSend)); - } - metricSenderSession.setDatapoint(builder.build()); - } - - //--- Aditional Methods - - /** - * calculate Max value for snapshot - * @param shapshot - * @return - */ - - private double getMax(Snapshot snapshot) { - double[] values = snapshot.getValues(); - if (values.length == 0) { - return 0; - } - return values[values.length - 1]; - } - - /** - * calculate Min value for snapshot - * @param shapshot - * @return - */ - - private double getMin(Snapshot snapshot) { - double[] values = snapshot.getValues(); - if (values.length == 0) { - return 0; - } - return values[0]; - } - - /** - * calculate standard deviation for snapshot - * @param shapshot - * @return - */ - - private double getStdDev(Snapshot snapshot) { - // two-pass algorithm for variance, avoids numeric overflow - double[] values = snapshot.getValues(); - - if (values.length <= 1) { - return 0; - } - - final double mean = getMean(snapshot); - double sum = 0; - - for (double value : values) { - final double diff = value - mean; - sum += diff * diff; - } - - final double variance = sum / (values.length - 1); - return Math.sqrt(variance); - } - - /** - * calculate Mean value for snapshot - * @param shapshot - * @return - */ - - private double getMean(Snapshot shapshot) { - - double[] values = shapshot.getValues(); - - if (values.length == 0) { - return 0; - } - - double sum = 0; - for (double value : values) { - sum += value; - } - return sum / values.length; - } - -} diff --git a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/CustomScheduledReporter.java b/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/CustomScheduledReporter.java deleted file mode 100644 index f43b5482..00000000 --- a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/CustomScheduledReporter.java +++ /dev/null @@ -1,204 +0,0 @@ -package com.signalfx.codahale.reporter; - -import java.util.SortedMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.*; - -import com.yammer.metrics.core.Counter; -import com.yammer.metrics.core.Gauge; -import com.yammer.metrics.core.Histogram; -import com.yammer.metrics.core.Meter; -import com.yammer.metrics.core.MetricPredicate; -import com.yammer.metrics.core.MetricsRegistry; -import com.yammer.metrics.core.Timer; -import com.yammer.metrics.core.MetricName; -import com.yammer.metrics.core.Metric; - -/** - * The abstract base class for all scheduled reporters (i.e., reporters which process a registry's - * metrics periodically). - * - */ -public abstract class CustomScheduledReporter { - /** - * A simple named thread factory. - */ - @SuppressWarnings("NullableProblems") - private static class NamedThreadFactory implements ThreadFactory { - private final ThreadGroup group; - private final AtomicInteger threadNumber = new AtomicInteger(1); - private final String namePrefix; - - private NamedThreadFactory(String name) { - final SecurityManager s = System.getSecurityManager(); - this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); - this.namePrefix = "metrics-" + name + "-thread-"; - } - - @Override - public Thread newThread(Runnable r) { - final Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); - t.setDaemon(true); - if (t.getPriority() != Thread.NORM_PRIORITY) { - t.setPriority(Thread.NORM_PRIORITY); - } - return t; - } - } - - private final MetricsRegistry registry; - private final ScheduledExecutorService executor; - private final MetricPredicate filter; - - /** - * Creates a new {@link CustomScheduledReporter} instance. - * - * @param registry the MetricsRegistry containing the metrics this - * reporter will report - * @param name the reporter's name - * @param filter the filter for which metrics to report - * @param rateUnit - * @param durationUnit - */ - protected CustomScheduledReporter(MetricsRegistry registry, - String name, - MetricPredicate filter, - TimeUnit rateUnit, - TimeUnit durationUnit) { - this.registry = registry; - this.filter = filter; - this.executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory(name)); - } - - /** - * get Metrics by class and predicate - * - * @param klass - * @param filter - * @return - */ - - @SuppressWarnings("unchecked") - private SortedMap getMetrics(Class klass, MetricPredicate filter) { - - Map allMetrics = registry.allMetrics(); - final TreeMap timers = new TreeMap(); - - for (Map.Entry entry : allMetrics.entrySet()) { - if (klass.isInstance(entry.getValue()) && filter.matches(entry.getKey(), - entry.getValue())) { - timers.put(entry.getKey(), (T) entry.getValue()); - } - } - - return Collections.unmodifiableSortedMap(timers); - } - - /** - * get all Gauge metrics - * @param filter - * @return - */ - - private SortedMap getGauges(MetricPredicate filter) { - return getMetrics(Gauge.class, filter); - } - - /** - * get all Counter metrics - * @param filter - * @return - */ - - private SortedMap getCounters(MetricPredicate filter) { - return getMetrics(Counter.class, filter); - } - - /** - * get all Histogram metrics - * @param filter - * @return - */ - - private SortedMap getHistograms(MetricPredicate filter) { - return getMetrics(Histogram.class, filter); - } - - /** - * get all Meters metrics - * @param filter - * @return - */ - - private SortedMap getMeters(MetricPredicate filter) { - return getMetrics(Meter.class, filter); - } - - /** - * get all Timers metrics - * @param filter - * @return - */ - - private SortedMap getTimers(MetricPredicate filter) { - return getMetrics(Timer.class, filter); - } - - /** - * Starts the reporter polling at the given period. - * - * @param period the amount of time between polls - * @param unit the unit for {@code period} - */ - public void start(long period, TimeUnit unit) { - executor.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - report(); - } - }, period, period, unit); - } - - /** - * Stops the reporter and shuts down its thread of execution. - */ - public void stop() { - executor.shutdown(); - try { - executor.awaitTermination(1, TimeUnit.SECONDS); - } catch (InterruptedException ignored) { - // do nothing - } - } - - /** - * Report the current values of all metrics in the registry. - */ - public void report() { - report(getGauges(filter), - getCounters(filter), - getHistograms(filter), - getMeters(filter), - getTimers(filter)); - } - - /** - * Called periodically by the polling thread. Subclasses should report all the given metrics. - * - * @param gauges all of the gauges in the registry - * @param counters all of the counters in the registry - * @param histograms all of the histograms in the registry - * @param meters all of the meters in the registry - * @param timers all of the timers in the registry - */ - public abstract void report(SortedMap gauges, - SortedMap counters, - SortedMap histograms, - SortedMap meters, - SortedMap timers); - -} diff --git a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java b/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java deleted file mode 100644 index 3d525189..00000000 --- a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadata.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.signalfx.codahale.reporter; - -import java.util.Map; -import com.yammer.metrics.core.Metric; -import com.google.common.base.Optional; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * Allows users to modify a metric with different source or metric parts than the default we pick - * from codahale. Note: This class must be thread safe. - */ -public interface MetricMetadata { - public static final String SOURCE = "source"; - public static final String METRIC = "metric"; - public Map getTags(Metric metric); - public Optional getMetricType(Metric metric); - - /** - * Create an object to tag a metric with data. Registering two different metrics with the same - * metadata will result in an exception. - * @param metric The metric will tag. - * @param The type of metric. It is implied by the metric type. - * @return An object to tag the given metric. - */ - public Tagger forMetric(M metric); - - @Deprecated - public Tagger tagMetric(M metric); - - /** - * Removes the specified metric from the metric metadata. - * @param metric The metric to remove, cannot be null. - * @return True if the metric was found and removed, false otherwise - */ - public boolean removeMetric(M metric); - - public interface TaggerBase> { - /** - * Tag the metric with a sf_source - * @param sourceName Source name for the sf_source - * @return this - * @deprecated The use of the build in source parameter is deprecated and discouraged. Use - * {@link #withDimension(String, String)} instead. - */ - @Deprecated - T withSourceName(String sourceName); - - /** - * Changes the metric name of this metric from the default (which is the codahale metric - * name), to another string - * @param metricName The new name in SignalFx of this metric - * @return this - */ - T withMetricName(String metricName); - - /** - * Adds a dimension to this metric - * @param key The dimension key to add - * @param value The dimensino value to add - * @return this - */ - T withDimension(String key, String value); - - /** - * Changes the default metric type of this metric to the SignalFx metric type passed in - * @param metricType The new metric type of this metric - * @return this - */ - T withMetricType(SignalFxProtocolBuffers.MetricType metricType); - } - - public interface Tagger extends TaggerBase> { - - /** - * Returns the tagged metric - * @return the tagged metric - */ - M metric(); - } - -} diff --git a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java b/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java deleted file mode 100644 index 48efbce8..00000000 --- a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/MetricMetadataImpl.java +++ /dev/null @@ -1,159 +0,0 @@ -package com.signalfx.codahale.reporter; - -import java.util.Collections; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import com.yammer.metrics.core.Metric; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * - * Implementation of MetricMetadata - * - */ - -public class MetricMetadataImpl implements MetricMetadata { - private final ConcurrentMap metaDataCollection; - - public MetricMetadataImpl() { - // This map must be thread safe - metaDataCollection = new ConcurrentHashMap(); - } - - @Override - public Map getTags(Metric metric) { - Metadata existingMetaData = metaDataCollection.get(metric); - if (existingMetaData == null) { - return Collections.emptyMap(); - } else { - return Collections.unmodifiableMap(existingMetaData.tags); - } - } - - @Override - public Optional getMetricType(Metric metric) { - Metadata existingMetaData = metaDataCollection.get(metric); - if (existingMetaData == null || existingMetaData.metricType == null) { - return Optional.absent(); - } else { - return Optional.of(existingMetaData.metricType); - } - } - - @Override - public Tagger tagMetric(M metric) { - return forMetric(metric); - } - - @Override - public Tagger forMetric(M metric) { - Metadata metadata = metaDataCollection.get(metric); - if (metadata == null) { - synchronized (this) { - if (metadata == null) { - metadata = new Metadata(); - Metadata oldMetaData = metaDataCollection.put(metric, metadata); - Preconditions.checkArgument(oldMetaData == null, - "Concurrency issue adding metadata"); - } - } - } - return new TaggerImpl(metric, metadata); - } - - @Override - public boolean removeMetric(M metric) { - return metaDataCollection.remove(metric) != null; - } - - private static abstract class TaggerBaseImpl> - implements TaggerBase{ - protected final Metadata thisMetricsMetadata; - - @Override - public T withDimension(String key, String value) { - thisMetricsMetadata.tags.put(key, value); - return (T) this; - } - - TaggerBaseImpl(Metadata thisMetricsMetadata) { - this.thisMetricsMetadata = thisMetricsMetadata; - } - - @Override - public T withSourceName(String sourceName) { - thisMetricsMetadata.tags.put(SOURCE, sourceName); - return (T) this; - } - - @Override - public T withMetricName(String metricName) { - thisMetricsMetadata.tags.put(METRIC, metricName); - return (T) this; - } - - @Override - public T withMetricType( - SignalFxProtocolBuffers.MetricType metricType) { - thisMetricsMetadata.metricType = metricType; - return (T) this; - } - - } - - private static final class TaggerImpl extends TaggerBaseImpl> - implements Tagger { - private final M metric; - - TaggerImpl(M metric, Metadata thisMetricsMetadata) { - super(thisMetricsMetadata); - this.metric = metric; - } - - @Override public M metric() { - return metric; - } - } - - private static final class Metadata { - private final Map tags; - private SignalFxProtocolBuffers.MetricType metricType; - - private Metadata() { - tags = new ConcurrentHashMap(6); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof Metadata)) { - return false; - } - - Metadata metadata = (Metadata) o; - - if (metricType != metadata.metricType) { - return false; - } - if (tags != null ? !tags.equals(metadata.tags) : metadata.tags != null) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - int result = tags != null ? tags.hashCode() : 0; - result = 31 * result + (metricType != null ? metricType.hashCode() : 0); - return result; - } - } - -} diff --git a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SfUtil.java b/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SfUtil.java deleted file mode 100644 index c86abb66..00000000 --- a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SfUtil.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.signalfx.codahale.reporter; - -import com.yammer.metrics.core.Gauge; -import com.yammer.metrics.core.Metric; -import com.yammer.metrics.core.MetricsRegistry; -import com.yammer.metrics.core.MetricName; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -/** - * Utility functions that make common SignalFx operations easier to do. - * @deprecated Migrate to OpenTelemetry to send metric telemetry to Splunk. - */ -@Deprecated -public class SfUtil { - - /** - *

- * Creates a {@link com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType#CUMULATIVE_COUNTER} - * type metric who's value is returned from a callback. The metric is internally stored as the - * Gauge type inside the MetricsRegistry, - * but the callback is expected to behave like a cumulative counter and the value is sent to - * SignalFx as a cumulative counter. - *

- *

- * This is useful when you can query for an absolute number of events, but cannot register - * a callback per event. Rather than behaving like a Gauge, it will signal a rate of events - * to SignalFx. - *

- * @param metricRegistry Where the counter lives - * @param name Name of the counter - * @param metricMetadata Where your metric metadata is tagged - * @param callback The callback that gets the counter's current value - * @return The registered metric - */ - public static Metric cumulativeCounter(MetricsRegistry metricRegistry, - MetricName name, - MetricMetadata metricMetadata, - Gauge callback) { - return metricMetadata.forMetric(metricRegistry.newGauge(name, callback)).withMetricType( - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER).metric(); - } - - -} diff --git a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java b/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java deleted file mode 100644 index b2bc3924..00000000 --- a/signalfx-yammer/src/main/java/com/signalfx/codahale/reporter/SignalFxReporter.java +++ /dev/null @@ -1,323 +0,0 @@ -package com.signalfx.codahale.reporter; - -import static java.util.Objects.requireNonNull; - -import java.util.Collection; -import java.util.Collections; -import java.util.EnumSet; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.concurrent.TimeUnit; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.signalfx.endpoint.SignalFxEndpoint; -import com.signalfx.endpoint.SignalFxReceiverEndpoint; -import com.signalfx.metrics.SourceNameHelper; -import com.signalfx.metrics.auth.AuthToken; -import com.signalfx.metrics.auth.StaticAuthToken; -import com.signalfx.metrics.connection.DataPointReceiverFactory; -import com.signalfx.metrics.connection.HttpDataPointProtobufReceiverFactory; -import com.signalfx.metrics.errorhandler.OnSendErrorHandler; -import com.signalfx.metrics.flush.AggregateMetricSender; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; -import com.yammer.metrics.core.Counter; -import com.yammer.metrics.core.Gauge; -import com.yammer.metrics.core.Histogram; -import com.yammer.metrics.core.Meter; -import com.yammer.metrics.core.MetricName; -import com.yammer.metrics.core.MetricPredicate; -import com.yammer.metrics.core.MetricsRegistry; -import com.yammer.metrics.core.Timer; - -/** - * Reporter object for codahale metrics that reports values to com.signalfx.signalfx at some - * interval. - * @deprecated Migrate to OpenTelemetry to send metric telemetry to Splunk. - */ -@Deprecated -public class SignalFxReporter extends CustomScheduledReporter { - private final AggregateMetricSender aggregateMetricSender; - private final Set detailsToAdd; - private final MetricMetadata metricMetadata; - private final boolean useLocalTime; - private final boolean sendExtraMetricDimensions; - private final ImmutableMap defaultDimensions; - - protected SignalFxReporter(MetricsRegistry registry, String name, MetricPredicate filter, - TimeUnit rateUnit, TimeUnit durationUnit, - AggregateMetricSender aggregateMetricSender, - Set detailsToAdd, - MetricMetadata metricMetadata) { - this(registry, name, filter, rateUnit, durationUnit, aggregateMetricSender, detailsToAdd, - metricMetadata, false, false, Collections. emptyMap()); - } - - public SignalFxReporter(MetricsRegistry registry, String name, MetricPredicate filter, - TimeUnit rateUnit, TimeUnit durationUnit, - AggregateMetricSender aggregateMetricSender, - Set detailsToAdd, MetricMetadata metricMetadata, - boolean useLocalTime, - boolean sendExtraMetricDimensions, - Map defaultDimensions) { - super(registry, name, filter, rateUnit, durationUnit); - this.aggregateMetricSender = aggregateMetricSender; - this.useLocalTime = useLocalTime; - this.detailsToAdd = detailsToAdd; - this.metricMetadata = metricMetadata; - this.sendExtraMetricDimensions = sendExtraMetricDimensions; - this.defaultDimensions = ImmutableMap.copyOf(defaultDimensions); - } - - /** - * - * Reports all given metrics here - * - */ - - @Override - public void report(SortedMap gauges, SortedMap counters, - SortedMap histograms, - SortedMap meters, SortedMap timers) { - - AggregateMetricSenderSessionWrapper session = new AggregateMetricSenderSessionWrapper( - aggregateMetricSender.createSession(), Collections.unmodifiableSet(detailsToAdd), metricMetadata, - aggregateMetricSender.getDefaultSourceName(), "sf_source", useLocalTime, sendExtraMetricDimensions, - defaultDimensions); - try { - for (Map.Entry entry : gauges.entrySet()) { - session.addMetric(entry.getValue(), entry.getKey(), - SignalFxProtocolBuffers.MetricType.GAUGE, entry.getValue().value()); - } - for (Map.Entry entry : counters.entrySet()) { - session.addMetric(entry.getValue(), entry.getKey(), - SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, - entry.getValue().count()); - } - for (Map.Entry entry : histograms.entrySet()) { - session.addHistogram(entry.getKey(), entry.getValue()); - } - for (Map.Entry entry : meters.entrySet()) { - session.addMetered(entry.getKey(), entry.getValue()); - } - for (Map.Entry entry : timers.entrySet()) { - session.addTimer(entry.getKey(), entry.getValue()); - } - } finally { - try { - session.close(); - } catch (Exception e) { - // Unable to register... these exceptions handled by AggregateMetricSender - } - } - } - - public MetricMetadata getMetricMetadata() { - return metricMetadata; - } - - public enum MetricDetails { - // For {@link com.codahale.metrics.Sampling} - MEDIAN("median"), - PERCENT_75("75th"), - PERCENT_95("95th"), - PERCENT_98("98th"), - PERCENT_99("99th"), - PERCENT_999("999th"), - MAX("max"), - MIN("min"), - STD_DEV("stddev"), - MEAN("mean"), - - // For {@link com.codahale.metrics.Counting} - COUNT("count"), - - // For {@link com.codahale.metrics.Metered} - RATE_MEAN("rate.mean"), - RATE_1_MIN("rate.1min"), - RATE_5_MIN("rate.5min"), - RATE_15_MIN("rate.15min"); - public static final Set ALL = Collections.unmodifiableSet(EnumSet.allOf(MetricDetails.class)); - public static final Set DEFAULTS = ImmutableSet.of(COUNT, MIN, MEAN, MAX); - - private final String description; - - MetricDetails(String description) { - this.description = description; - } - - public String getDescription() { - return description; - } - } - - public static final class Builder { - private final MetricsRegistry registry; - private String defaultSourceName; - private AuthToken authToken; - private SignalFxReceiverEndpoint endpoint = new SignalFxEndpoint(); - private String name = "signalfx-reporter"; - private int timeoutMs = HttpDataPointProtobufReceiverFactory.DEFAULT_TIMEOUT_MS; - private DataPointReceiverFactory dataPointReceiverFactory = new - HttpDataPointProtobufReceiverFactory(endpoint); - private MetricPredicate filter = MetricPredicate.ALL; - private TimeUnit rateUnit = TimeUnit.SECONDS; - private TimeUnit durationUnit = TimeUnit.MILLISECONDS; // Maybe nano eventually? - private Set detailsToAdd = MetricDetails.DEFAULTS; - private Collection onSendErrorHandlerCollection = Collections.emptyList(); - private MetricMetadata metricMetadata = new MetricMetadataImpl(); - private boolean useLocalTime = false; - private boolean sendExtraMetricDimensions = false; - private final ImmutableMap.Builder defaultDimensions = new ImmutableMap.Builder(); - - public Builder(MetricsRegistry registry, String authToken) { - this(registry, new StaticAuthToken(authToken)); - } - - public Builder(MetricsRegistry registry, AuthToken authToken) { - this(registry, authToken, SourceNameHelper.getDefaultSourceName()); - } - - public Builder(MetricsRegistry registry, AuthToken authToken, String defaultSourceName) { - this.registry = registry; - this.authToken = authToken; - this.defaultSourceName = requireNonNull(defaultSourceName, "defaultSourceName must be a non-null value"); - } - - public Builder setDefaultSourceName(String defaultSourceName) { - this.defaultSourceName = requireNonNull(defaultSourceName, "defaultSourceName must be a non-null value"); - return this; - } - - public Builder setAuthToken(AuthToken authToken) { - this.authToken = authToken; - return this; - } - - public Builder setEndpoint(SignalFxReceiverEndpoint endpoint) { - this.endpoint = endpoint; - this.dataPointReceiverFactory = - new HttpDataPointProtobufReceiverFactory(endpoint) - .setTimeoutMs(this.timeoutMs); - return this; - } - - public Builder setName(String name) { - this.name = name; - return this; - } - - public Builder setTimeoutMs(int timeoutMs) { - this.timeoutMs = timeoutMs; - this.dataPointReceiverFactory = - new HttpDataPointProtobufReceiverFactory(endpoint) - .setTimeoutMs(this.timeoutMs); - return this; - } - - @Deprecated - public Builder setVersion(int version) { - return this; - } - - public Builder setDataPointReceiverFactory( - DataPointReceiverFactory dataPointReceiverFactory) { - this.dataPointReceiverFactory = dataPointReceiverFactory; - return this; - } - - public Builder setFilter(MetricPredicate filter) { - this.filter = filter; - return this; - } - - public Builder setRateUnit(TimeUnit rateUnit) { - this.rateUnit = rateUnit; - return this; - } - - public Builder setDurationUnit(TimeUnit durationUnit) { - this.durationUnit = durationUnit; - return this; - } - - public Builder setDetailsToAdd(Set detailsToAdd) { - this.detailsToAdd = detailsToAdd; - return this; - } - - public Builder setOnSendErrorHandlerCollection( - Collection onSendErrorHandlerCollection) { - this.onSendErrorHandlerCollection = onSendErrorHandlerCollection; - return this; - } - - public Builder setMetricMetadata(MetricMetadata metricMetadata) { - this.metricMetadata = metricMetadata; - return this; - } - - /** - * Will use the local system time, rather than zero, on sent datapoints. - * @param useLocalTime If true, use local system time - * @return this - */ - public Builder useLocalTime(boolean useLocalTime) { - this.useLocalTime = useLocalTime; - return this; - } - - /** - * Adds all dimensions to the default dimensions to be sent with every datapoint from this - * reporter. - * - * @param dimensions - * non-null map of string value pairs - * @return this - */ - public Builder addDimensions(Map dimensions) { - // loop here to get "null value" protection of addDimension - for (Map.Entry entry: dimensions.entrySet()) { - this.addDimension(entry.getKey(), entry.getValue()); - } - return this; - } - - /** - * Adds a dimension to the default dimensions to be sent with every datapoint from this - * reporter. - * - * @param name - * Name of the dimension - * @param value - * Value of the dimension. If null then the dimension is not added. - * @return this - */ - public Builder addDimension(String name, String value) { - if (value != null) { - this.defaultDimensions.put(name, value); - } - return this; - } - - /** - * If set to true, will send the metric group name as a dimension - * called "metric_group" for all metrics which have a group set. - * - * @return this - */ - public Builder sendExtraMetricDimensions(boolean sendExtraMetricDimensions) { - this.sendExtraMetricDimensions = sendExtraMetricDimensions; - return this; - } - - public SignalFxReporter build() { - AggregateMetricSender aggregateMetricSender = new AggregateMetricSender( - defaultSourceName, dataPointReceiverFactory, authToken, onSendErrorHandlerCollection); - return new SignalFxReporter(registry, name, filter, rateUnit, durationUnit, - aggregateMetricSender, detailsToAdd, metricMetadata, useLocalTime, - sendExtraMetricDimensions, defaultDimensions.build()); - } - } -} diff --git a/signalfx-yammer/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java b/signalfx-yammer/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java deleted file mode 100644 index 08d1323a..00000000 --- a/signalfx-yammer/src/test/java/com/signalfx/codahale/metrics/SignalFxReporterTest.java +++ /dev/null @@ -1,252 +0,0 @@ -package com.signalfx.codahale.metrics; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import com.yammer.metrics.core.Counter; -import com.yammer.metrics.core.Gauge; -import com.yammer.metrics.core.MetricName; -import com.yammer.metrics.core.MetricsRegistry; -import com.yammer.metrics.core.MetricPredicate; -import com.yammer.metrics.core.Histogram; -import com.google.common.collect.ImmutableSet; -import com.signalfx.codahale.reporter.MetricMetadata; -import com.signalfx.codahale.reporter.SfUtil; -import com.signalfx.codahale.reporter.SignalFxReporter; -import com.signalfx.metrics.auth.StaticAuthToken; -import com.signalfx.metrics.connection.StaticDataPointReceiverFactory; -import com.signalfx.metrics.connection.StoredDataPointReceiver; -import com.signalfx.metrics.errorhandler.MetricError; -import com.signalfx.metrics.errorhandler.OnSendErrorHandler; -import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -public class SignalFxReporterTest { - - private void testReporter(){ - StoredDataPointReceiver dbank = new StoredDataPointReceiver(); - assertEquals(0, dbank.addDataPoints.size()); - - MetricsRegistry metricRegistery = new MetricsRegistry(); - SignalFxReporter reporter = new SignalFxReporter.Builder(metricRegistery, new StaticAuthToken(""), "myserver") - .setDataPointReceiverFactory(new StaticDataPointReceiverFactory(dbank)) - .setDetailsToAdd( - ImmutableSet.of( - SignalFxReporter.MetricDetails.COUNT, - SignalFxReporter.MetricDetails.MIN, - SignalFxReporter.MetricDetails.MAX - ) - ) - .setName("testReporter") - .useLocalTime(false) - .sendExtraMetricDimensions(true) - .setOnSendErrorHandlerCollection( - Collections.singleton(new OnSendErrorHandler(){ - public void handleError(MetricError error){ - System.out.println("" + error.getMessage()); - } - }) - ) - .setFilter(MetricPredicate.ALL) - .setRateUnit(TimeUnit.SECONDS) - .build(); - - final MetricMetadata metricMetadata = reporter.getMetricMetadata(); - - - MetricName gaugeName = new MetricName("group1", "type1", "gauge1"); - metricRegistery.newGauge(gaugeName, new Gauge() { - @Override - public Integer value() { - return 1; - } - }); - - MetricName boolGaugeName = new MetricName("group1", "type1", "boolGauge1"); - metricRegistery.newGauge(boolGaugeName, new Gauge() { - @Override - public Boolean value() { - return true; - } - }); - - MetricName cntName1 = new MetricName("group1", "type1", "counter"); - Counter cnt1 = metricRegistery.newCounter(cntName1); - metricMetadata.forMetric(cnt1) - .withMetricName("newname") - .withSourceName("newsource") - .withMetricType(SignalFxProtocolBuffers.MetricType.GAUGE) - .withDimension("key", "value"); - - MetricName cntName2 = new MetricName("group1", "type1", "counter2"); - Counter cnt2 = metricRegistery.newCounter(cntName2); - metricMetadata.forMetric(cnt2) - .withMetricName("newname2"); - cnt1.inc(); - cnt1.inc(); - - MetricName timerName1 = new MetricName("group1", "type1", "atimer"); - metricRegistery.newTimer(timerName1, TimeUnit.SECONDS, TimeUnit.SECONDS).time().stop(); - - reporter.report(); - - assertEquals(7, dbank.addDataPoints.size()); - assertEquals("newname", dbank.addDataPoints.get(2).getMetric()); - assertEquals("newsource", dbank.addDataPoints.get(2).getDimensions(0).getValue()); - assertEquals("sf_source", dbank.addDataPoints.get(2).getDimensions(0).getKey()); - assertEquals(SignalFxProtocolBuffers.MetricType.GAUGE, dbank.registeredMetrics.get( - "newname")); - assertEquals(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, dbank.registeredMetrics.get( - "atimer.count")); - assertEquals(SignalFxProtocolBuffers.MetricType.GAUGE, dbank.registeredMetrics.get( - "atimer.max")); - assertEquals(2, dbank.lastValueFor("newsource", "newname").getIntValue()); - assertEquals("metric_group", dbank.addDataPoints.get(2).getDimensions(2).getKey()); - assertEquals("group1", dbank.addDataPoints.get(2).getDimensions(2).getValue()); - assertEquals("type1", dbank.addDataPoints.get(2).getDimensions(3).getValue()); - - assertNotNull(dbank.lastValueFor("myserver", "atimer.count")); - - dbank.addDataPoints.clear(); - - MetricName rawCounterName = new MetricName("group1", "type1", "raw_counter"); - Counter rawCounter = metricRegistery.newCounter(rawCounterName); - MetricName counterCallbackName = new MetricName("group1", "type1", "cumulative_counter_callback"); - metricMetadata.forMetric(rawCounter) - .withMetricType(SignalFxProtocolBuffers.MetricType.COUNTER); - SfUtil.cumulativeCounter(metricRegistery, counterCallbackName, metricMetadata, new Gauge() { - private long i = 0; - - @Override public Long value() { - return i++; - } - }); - - rawCounter.inc(10); - - reporter.report(); - assertEquals(9, dbank.addDataPoints.size()); - assertEquals(10, dbank.lastValueFor("myserver", "raw_counter").getIntValue()); - assertEquals(0, dbank.lastValueFor("myserver", "cumulative_counter_callback").getIntValue()); - - assertEquals(2, dbank.lastValueFor("newsource", "newname").getIntValue()); - - assertEquals(SignalFxProtocolBuffers.MetricType.COUNTER, dbank.registeredMetrics.get("raw_counter")); - assertEquals(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, - dbank.registeredMetrics.get("cumulative_counter_callback")); - rawCounter.inc(14); - dbank.addDataPoints.clear(); - - metricRegistery.removeMetric(cntName1); - assertEquals(true, dbank.clearValues("newsource", "newname")); - assertEquals(false, dbank.clearValues("newsource", "newname")); - reporter.report(); - - assertEquals(8, dbank.addDataPoints.size()); - assertEquals(0, dbank.valuesFor("newsource", "newname").size()); - assertEquals(24, dbank.lastValueFor("myserver", "raw_counter").getIntValue()); - assertEquals(1, dbank.lastValueFor("myserver", "cumulative_counter_callback").getIntValue()); - - - - long endTime = System.currentTimeMillis() + 1000 * 10; - reporter.start(1, TimeUnit.SECONDS); - - int size = 0; - while(endTime > System.currentTimeMillis()){ - if(size != dbank.addDataPoints.size()){ - break; - } - } - - assertTrue(endTime > System.currentTimeMillis()); - - reporter.stop(); - } - - private void testReporterWithDetails(){ - - StoredDataPointReceiver dbank = new StoredDataPointReceiver(); - assertEquals(0, dbank.addDataPoints.size()); - - Set detailsToAdd = new HashSet(); - detailsToAdd.add(SignalFxReporter.MetricDetails.STD_DEV); - detailsToAdd.add(SignalFxReporter.MetricDetails.MEAN); - - MetricsRegistry metricRegistery = new MetricsRegistry(); - SignalFxReporter reporter = new SignalFxReporter.Builder(metricRegistery, new StaticAuthToken(""), "myserver") - .setDataPointReceiverFactory(new StaticDataPointReceiverFactory(dbank)) - .setDetailsToAdd( - ImmutableSet.of( - SignalFxReporter.MetricDetails.COUNT, - SignalFxReporter.MetricDetails.MIN, - SignalFxReporter.MetricDetails.MAX - ) - ) - .setName("testReporter") - .setDefaultSourceName("defaultSource") - .useLocalTime(false) - .setOnSendErrorHandlerCollection( - Collections.singleton(new OnSendErrorHandler(){ - public void handleError(MetricError error){ - System.out.println("" + error.getMessage()); - } - }) - ) - .setFilter(MetricPredicate.ALL) - .setRateUnit(TimeUnit.SECONDS) - .setDetailsToAdd(detailsToAdd) - .build(); - - final MetricMetadata metricMetadata = reporter.getMetricMetadata(); - - MetricName histogramName = new MetricName("group1", "type1", "histogram"); - Histogram histogram = metricRegistery.newHistogram(histogramName, true); - histogram.update(10); - histogram.update(14); - histogram.update(7); - - metricMetadata.forMetric(histogram) - .withMetricName("histogram") - .withSourceName("histogram_source") - .withMetricType(SignalFxProtocolBuffers.MetricType.GAUGE) - .withDimension("key", "value"); - - reporter.report(); - - assertEquals(2, dbank.addDataPoints.size()); - - } - - @Test - public void test() throws InterruptedException { - testReporter(); - testReporterWithDetails(); - } - - @Test - public void shouldFailOnNullDefaultSourceMethod_constructor() { - try { - new SignalFxReporter.Builder(null, null, null); - fail("NPE was expected"); - } catch (NullPointerException npe) { - assertTrue(npe.getMessage().contains("defaultSourceName")); - } - } - - @Test - public void shouldFailOnNullDefaultSourceMethod_method() { - try { - new SignalFxReporter.Builder(null, "test") - .setDefaultSourceName(null); - fail("NPE was expected"); - } catch (NullPointerException npe) { - assertTrue(npe.getMessage().contains("defaultSourceName")); - } - } -}