Skip to content

Added increment/decrement by delta to CounterService and implementations #10107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.actuate.metrics;

import org.springframework.util.Assert;

/**
* A service that can be used to increment, decrement and reset a named counter value.
*
Expand All @@ -29,12 +31,36 @@ public interface CounterService {
*/
void increment(String metricName);

/**
* Increment the specified counter by delta.
* @param metricName the name of the counter
* @param delta the amount to increment by
*/
default void increment(String metricName, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
for (int i = 0; i < delta; i++) {
increment(metricName);
}
}

/**
* Decrement the specified counter by 1.
* @param metricName the name of the counter
*/
void decrement(String metricName);

/**
* Decrement the specified counter by delta.
* @param metricName the name of the counter
* @param delta the amount to decrement by
*/
default void decrement(String metricName, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
for (int i = 0; i < delta; i++) {
decrement(metricName);
}
}

/**
* Reset the specified counter.
* @param metricName the name of the counter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.util.Assert;

/**
* Fast implementation of {@link CounterService} using {@link CounterBuffers}.
Expand All @@ -45,11 +46,23 @@ public void increment(String metricName) {
this.buffers.increment(wrap(metricName), 1L);
}

@Override
public void increment(String metricName, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
this.buffers.increment(wrap(metricName), delta);
}

@Override
public void decrement(String metricName) {
this.buffers.increment(wrap(metricName), -1L);
}

@Override
public void decrement(String metricName, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
this.buffers.increment(wrap(metricName), -delta);
}

@Override
public void reset(String metricName) {
this.buffers.reset(wrap(metricName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,23 @@ public void increment(String name) {
incrementInternal(name, 1L);
}

@Override
public void increment(String name, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
incrementInternal(name, delta);
}

@Override
public void decrement(String name) {
incrementInternal(name, -1L);
}

@Override
public void decrement(String name, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
incrementInternal(name, -delta);
}

private void incrementInternal(String name, long value) {
if (name.startsWith("meter")) {
Meter meter = this.registry.meter(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.util.Assert;

/**
* Default implementation of {@link CounterService}.
Expand All @@ -44,11 +45,23 @@ public void increment(String metricName) {
this.writer.increment(new Delta<>(wrap(metricName), 1L));
}

@Override
public void increment(String metricName, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
this.writer.increment(new Delta<>(wrap(metricName), delta));
}

@Override
public void decrement(String metricName) {
this.writer.increment(new Delta<>(wrap(metricName), -1L));
}

@Override
public void decrement(String metricName, long delta) {
Assert.state(delta > 0, "Delta should be greater than 0");
this.writer.increment(new Delta<>(wrap(metricName), -delta));
}

@Override
public void reset(String metricName) {
this.writer.reset(wrap(metricName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public void incrementCounter() {
assertThat(this.registry.counter("counter.foo").getCount()).isEqualTo(3);
}

@Test
public void incrementCounterWithDelta() {
this.writer.increment("foo", 3L);
assertThat(this.registry.counter("counter.foo").getCount()).isEqualTo(3);
}

@Test
public void updatePredefinedMeter() {
this.writer.increment("meter.foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public void decrementPrependsCounter() {
assertThat(this.captor.getValue().getValue()).isEqualTo(-1L);
}

@Test
public void incrementCounterWithDelta() {
this.service.increment("foo", 42L);
verify(this.repository).increment(this.captor.capture());
assertThat(this.captor.getValue().getName()).isEqualTo("counter.foo");
assertThat(this.captor.getValue().getValue()).isEqualTo(42L);
}

@Test
public void decrementCounterWithDelta() {
this.service.decrement("foo", 42L);
verify(this.repository).increment(this.captor.capture());
assertThat(this.captor.getValue().getName()).isEqualTo("counter.foo");
assertThat(this.captor.getValue().getValue()).isEqualTo(-42L);
}

@Test
public void resetResetsCounter() throws Exception {
this.service.reset("foo");
Expand Down