Skip to content

Commit

Permalink
Add helper for double distribution summary.
Browse files Browse the repository at this point in the history
Simplifies getting a common registered instance
of a DoubleDistributionSummary. Will update some
more in the future to make it step based and
see if we can create a better mechanism in the
registry to support this type of extension.
  • Loading branch information
brharrington committed Oct 22, 2014
1 parent bb7f4c7 commit dc31d68
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@


import com.netflix.spectator.api.Clock;
import com.netflix.spectator.api.ExtendedRegistry;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Meter;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand All @@ -31,6 +35,48 @@
*/
public class DoubleDistributionSummary implements Meter {

private static final ConcurrentHashMap<Id, DoubleDistributionSummary> INSTANCES =
new ConcurrentHashMap<>();

// https://github.com/Netflix/spectator/issues/43
private static final long RESET_FREQ = 60000L;

/**
* Get or create a double distribution summary with the specified id.
*
* @param id
* Identifier for the metric being registered.
* @return
* Distribution summary corresponding to the id.
*/
public static DoubleDistributionSummary get(Id id) {
return get(Spectator.registry(), id);
}

/**
* Get or create a double distribution summary with the specified id.
*
* @param registry
* Registry to use.
* @param id
* Identifier for the metric being registered.
* @return
* Distribution summary corresponding to the id.
*/
static DoubleDistributionSummary get(Registry registry, Id id) {
DoubleDistributionSummary instance = INSTANCES.get(id);
if (instance == null) {
final Clock c = registry.clock();
DoubleDistributionSummary tmp = new DoubleDistributionSummary(c, id, RESET_FREQ);
instance = INSTANCES.putIfAbsent(id, tmp);
if (instance == null) {
instance = tmp;
registry.register(tmp);
}
}
return instance;
}

private static final long ZERO = Double.doubleToLongBits(0.0);

private final Clock clock;
Expand All @@ -49,7 +95,12 @@ public class DoubleDistributionSummary implements Meter {
private final Id totalOfSquaresId;
private final Id maxId;

/** Create a new instance. */
/**
* Create a new instance.
*
* @deprecated Use DoubleDistributionSummary.get(id). This will be made package private in 0.15.
*/
@Deprecated
public DoubleDistributionSummary(Clock clock, Id id, long resetFreq) {
this.clock = clock;
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,12 @@ public void testRegister() {
}
}

@Test
public void staticGet() {
Id id = registry.createId("foo");
DoubleDistributionSummary t = DoubleDistributionSummary.get(registry, id);
Assert.assertSame(t, DoubleDistributionSummary.get(registry, id));
Assert.assertNotNull(registry.get(id));
}

}

0 comments on commit dc31d68

Please sign in to comment.