Skip to content

Commit

Permalink
helper for managing bucketed counters/timers
Browse files Browse the repository at this point in the history
  • Loading branch information
brharrington committed Jan 19, 2015
1 parent 4b5187b commit d652315
Show file tree
Hide file tree
Showing 19 changed files with 712 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ final class DefaultDistributionSummary implements DistributionSummary {
private final AtomicLong count;
private final AtomicLong totalAmount;

private final Id countId;
private final Id totalAmountId;

/** Create a new instance. */
DefaultDistributionSummary(Clock clock, Id id) {
this.clock = clock;
this.id = id;
count = new AtomicLong(0L);
totalAmount = new AtomicLong(0L);
countId = id.withTag("statistic", "count");
totalAmountId = id.withTag("statistic", "totalAmount");
}

@Override public Id id() {
Expand All @@ -58,8 +53,8 @@ final class DefaultDistributionSummary implements DistributionSummary {
@Override public Iterable<Measurement> measure() {
final long now = clock.wallTime();
final List<Measurement> ms = new ArrayList<>(2);
ms.add(new Measurement(countId, now, count.get()));
ms.add(new Measurement(totalAmountId, now, totalAmount.get()));
ms.add(new Measurement(id.withTag(Statistic.count), now, count.get()));
ms.add(new Measurement(id.withTag(Statistic.totalAmount), now, totalAmount.get()));
return ms;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,11 @@ final class DefaultLongTaskTimer implements LongTaskTimer {
private final Id id;
private final ConcurrentMap<Long, Long> tasks = new ConcurrentHashMap<>();
private final AtomicLong nextTask = new AtomicLong(0L);
private final Id activeTasksId;
private final Id durationId;

/** Create a new instance. */
DefaultLongTaskTimer(Clock clock, Id id) {
this.clock = clock;
this.id = id;

this.activeTasksId = id.withTag("statistic", "activeTasks");
this.durationId = id.withTag("statistic", "duration");
}

@Override public Id id() {
Expand Down Expand Up @@ -89,8 +84,8 @@ final class DefaultLongTaskTimer implements LongTaskTimer {
final List<Measurement> ms = new ArrayList<>(2);
final long now = clock.wallTime();
final double durationSeconds = duration() / NANOS_PER_SECOND;
ms.add(new Measurement(durationId, now, durationSeconds));
ms.add(new Measurement(activeTasksId, now, activeTasks()));
ms.add(new Measurement(id.withTag(Statistic.duration), now, durationSeconds));
ms.add(new Measurement(id.withTag(Statistic.activeTasks), now, activeTasks()));
return ms;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ final class DefaultTimer implements Timer {
private final AtomicLong count;
private final AtomicLong totalTime;

private final Id countId;
private final Id totalTimeId;

/** Create a new instance. */
DefaultTimer(Clock clock, Id id) {
this.clock = clock;
this.id = id;
count = new AtomicLong(0L);
totalTime = new AtomicLong(0L);
countId = id.withTag("statistic", "count");
totalTimeId = id.withTag("statistic", "totalTime");
}

@Override public Id id() {
Expand All @@ -61,8 +56,8 @@ final class DefaultTimer implements Timer {
@Override public Iterable<Measurement> measure() {
final long now = clock.wallTime();
final List<Measurement> ms = new ArrayList<>(2);
ms.add(new Measurement(countId, now, count.get()));
ms.add(new Measurement(totalTimeId, now, totalTime.get()));
ms.add(new Measurement(id.withTag(Statistic.count), now, count.get()));
ms.add(new Measurement(id.withTag(Statistic.totalTime), now, totalTime.get()));
return ms;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spectator.servo;

import com.netflix.spectator.api.Tag;
package com.netflix.spectator.api;

/**
* The valid set of statistics that can be reported by timers and distribution summaries.
*/
enum Statistic implements Tag {
public enum Statistic implements Tag {
/** Rate per second for calls to record. */
count,

Expand All @@ -34,7 +32,13 @@ enum Statistic implements Tag {
totalOfSquares,

/** The sum of the times recorded. */
totalTime;
totalTime,

/** Number of currently active tasks for a long task timer. */
activeTasks,

/** Duration of a running task. */
duration;

@Override public String key() {
return "statistic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public void testMeasure() {
clock.setWallTime(3712345L);
for (Measurement m : t.measure()) {
Assert.assertEquals(m.timestamp(), 3712345L);
if (m.id().equals(t.id().withTag("statistic", "count"))) {
if (m.id().equals(t.id().withTag(Statistic.count))) {
Assert.assertEquals(m.value(), 1.0, 0.1e-12);
} else if (m.id().equals(t.id().withTag("statistic", "totalAmount"))) {
} else if (m.id().equals(t.id().withTag(Statistic.totalAmount))) {
Assert.assertEquals(m.value(), 42, 0.1e-12);
} else {
Assert.fail("unexpected id: " + m.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ public void testMeasure() {
clock.setWallTime(3712345L);
for (Measurement m : t.measure()) {
Assert.assertEquals(m.timestamp(), 3712345L);
if (m.id().equals(t.id().withTag("statistic", "count"))) {
if (m.id().equals(t.id().withTag(Statistic.count))) {
Assert.assertEquals(m.value(), 1.0, 0.1e-12);
} else if (m.id().equals(t.id().withTag("statistic", "totalTime"))) {
} else if (m.id().equals(t.id().withTag(Statistic.totalTime))) {
Assert.assertEquals(m.value(), 42e6, 0.1e-12);
} else {
Assert.fail("unexpected id: " + m.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void testMeasure() {
clock.setWallTime(3712345L);
for (Measurement m : t.measure()) {
Assert.assertEquals(m.timestamp(), 3712345L);
if (m.id().equals(t.id().withTag("statistic", "count"))) {
if (m.id().equals(t.id().withTag(Statistic.count))) {
Assert.assertEquals(m.value(), 1.0, 0.1e-12);
} else if (m.id().equals(t.id().withTag("statistic", "totalAmount"))) {
} else if (m.id().equals(t.id().withTag(Statistic.totalAmount))) {
Assert.assertEquals(m.value(), 42.0, 0.1e-12);
} else {
Assert.fail("unexpected id: " + m.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void testStop() {
static void assertLongTaskTimer(Meter t, long timestamp, int activeTasks, double duration) {
for (Measurement m : t.measure()) {
Assert.assertEquals(m.timestamp(), timestamp);
if (m.id().equals(t.id().withTag("statistic", "activeTasks"))) {
if (m.id().equals(t.id().withTag(Statistic.activeTasks))) {
Assert.assertEquals(m.value(), activeTasks, 1.0e-12);
} else if (m.id().equals(t.id().withTag("statistic", "duration"))) {
} else if (m.id().equals(t.id().withTag(Statistic.duration))) {
Assert.assertEquals(m.value(), duration, 1.0e-12);
} else {
Assert.fail("unexpected id: " + m.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public void testMeasure() {
clock.setWallTime(3712345L);
for (Measurement m : t.measure()) {
Assert.assertEquals(m.timestamp(), 3712345L);
if (m.id().equals(t.id().withTag("statistic", "count"))) {
if (m.id().equals(t.id().withTag(Statistic.count))) {
Assert.assertEquals(m.value(), 1.0, 0.1e-12);
} else if (m.id().equals(t.id().withTag("statistic", "totalTime"))) {
} else if (m.id().equals(t.id().withTag(Statistic.totalTime))) {
Assert.assertEquals(m.value(), 42e6, 0.1e-12);
} else {
Assert.fail("unexpected id: " + m.id());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spectator.sandbox;

import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;

import java.util.Collections;

/** Counters that get incremented based on the bucket for recorded values. */
public final class BucketCounter implements DistributionSummary {

/**
* Creates a distribution summary object that manages a set of counters based on the bucket
* function supplied. Calling record will increment the appropriate counter.
*
* @param id
* Identifier for the metric being registered.
* @param f
* Function to map values to buckets.
* @return
* Distribution summary that manages sub-counters based on the bucket function.
*/
public static BucketCounter get(Id id, BucketFunction f) {
return get(Spectator.registry(), id, f);
}

/**
* Creates a distribution summary object that manages a set of counters based on the bucket
* function supplied. Calling record will increment the appropriate counter.
*
* @param registry
* Registry to use.
* @param id
* Identifier for the metric being registered.
* @param f
* Function to map values to buckets.
* @return
* Distribution summary that manages sub-counters based on the bucket function.
*/
public static BucketCounter get(Registry registry, Id id, BucketFunction f) {
return new BucketCounter(registry, id, f);
}

private final Registry registry;
private final Id id;
private final BucketFunction f;

/** Create a new instance. */
BucketCounter(Registry registry, Id id, BucketFunction f) {
this.registry = registry;
this.id = id;
this.f = f;
}

@Override public Id id() {
return id;
}

@Override public Iterable<Measurement> measure() {
return Collections.emptyList();
}

@Override public boolean hasExpired() {
return false;
}

@Override public void record(long amount) {
counter(f.apply(amount)).increment();
}

/**
* Return the count for a given bucket.
*/
public Counter counter(String bucket) {
return registry.counter(id.withTag("bucket", bucket));
}

@Override public long count() {
return 0L;
}

@Override public long totalAmount() {
return 0L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spectator.sandbox;

import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;

import java.util.Collections;

/** Distribution summaries that get updated based on the bucket for recorded values. */
public final class BucketDistributionSummary implements DistributionSummary {

/**
* Creates a distribution summary object that manages a set of distribution summaries based on
* the bucket function supplied. Calling record will be mapped to the record on the appropriate
* distribution summary.
*
* @param id
* Identifier for the metric being registered.
* @param f
* Function to map values to buckets.
* @return
* Distribution summary that manages sub-counters based on the bucket function.
*/
public static BucketDistributionSummary get(Id id, BucketFunction f) {
return get(Spectator.registry(), id, f);
}

/**
* Creates a distribution summary object that manages a set of distribution summaries based on
* the bucket function supplied. Calling record will be mapped to the record on the appropriate
* distribution summary.
*
* @param registry
* Registry to use.
* @param id
* Identifier for the metric being registered.
* @param f
* Function to map values to buckets.
* @return
* Distribution summary that manages sub-counters based on the bucket function.
*/
public static BucketDistributionSummary get(Registry registry, Id id, BucketFunction f) {
return new BucketDistributionSummary(registry, id, f);
}

private final Registry registry;
private final Id id;
private final BucketFunction f;

/** Create a new instance. */
BucketDistributionSummary(Registry registry, Id id, BucketFunction f) {
this.registry = registry;
this.id = id;
this.f = f;
}

@Override public Id id() {
return id;
}

@Override public Iterable<Measurement> measure() {
return Collections.emptyList();
}

@Override public boolean hasExpired() {
return false;
}

@Override public void record(long amount) {
distributionSummary(f.apply(amount)).record(amount);
}

/**
* Return the count for a given bucket.
*/
public DistributionSummary distributionSummary(String bucket) {
return registry.distributionSummary(id.withTag("bucket", bucket));
}

@Override public long count() {
return 0L;
}

@Override public long totalAmount() {
return 0L;
}
}
Loading

0 comments on commit d652315

Please sign in to comment.