Skip to content

Commit

Permalink
add test for preserving custom statistic (#1004)
Browse files Browse the repository at this point in the history
Adds a test case for AtlasCounter to ensure that it
will preserve the custom statistic if one is used.
  • Loading branch information
brharrington authored Nov 11, 2022
1 parent 4bef43c commit b0bc86a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.netflix.spectator.api.Clock;
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Statistic;
import com.netflix.spectator.impl.StepDouble;

Expand All @@ -33,12 +32,12 @@ class AtlasCounter extends AtlasMeter implements Counter {
private final Id stat;

/** Create a new instance. */
AtlasCounter(Registry registry, Id id, Clock clock, long ttl, long step) {
AtlasCounter(Id id, Clock clock, long ttl, long step) {
super(id, clock, ttl);
this.value = new StepDouble(0.0, clock, step);
// Add the statistic for typing. Re-adding the tags from the id is to retain
// the statistic from the id if it was already set
this.stat = registry.createId(id.name())
this.stat = Id.create(id.name())
.withTag(Statistic.count)
.withTag(DsType.rate)
.withTags(id.tags());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ synchronized List<RollupPolicy.Result> getBatches(long t) {
}

@Override protected Counter newCounter(Id id) {
return new AtlasCounter(this, id, clock(), meterTTL, lwcStepMillis);
return new AtlasCounter(id, clock(), meterTTL, lwcStepMillis);
}

@Override protected DistributionSummary newDistributionSummary(Id id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package com.netflix.spectator.atlas;

import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.ManualClock;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Statistic;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -27,10 +26,8 @@
public class AtlasCounterTest {

private final ManualClock clock = new ManualClock();
private final Registry registry = new DefaultRegistry();
private final long step = 10000L;
private final AtlasCounter counter = new AtlasCounter(registry,
registry.createId("test"), clock, step, step);
private final AtlasCounter counter = new AtlasCounter(Id.create("test"), clock, step, step);

private void checkValue(double expected) {
int count = 0;
Expand Down Expand Up @@ -119,4 +116,12 @@ public void expiration() {
counter.increment(42L);
Assertions.assertFalse(counter.hasExpired());
}

@Test
public void preferStatisticFromTags() {
Id id = Id.create("test").withTag(Statistic.percentile);
AtlasCounter c = new AtlasCounter(id, clock, step, step);
Id actual = c.measure().iterator().next().id();
Assertions.assertEquals(id.withTag(DsType.rate), actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

import java.util.Arrays;

import com.netflix.spectator.api.DefaultRegistry;
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.Utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit b0bc86a

Please sign in to comment.