-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from raynigon/feature/make-metrics-configurable
Allow Metrics to be disabled if not needed
- Loading branch information
Showing
20 changed files
with
471 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...gging-async/src/main/java/com/raynigon/ecs/logging/async/service/AsyncMetricsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.raynigon.ecs.logging.async.service; | ||
|
||
import com.raynigon.ecs.logging.async.service.helper.TimerWrapper; | ||
|
||
public interface AsyncMetricsService { | ||
|
||
TimerWrapper createQueueTimer(Class<?> source); | ||
|
||
TimerWrapper createExecutionTimer(Class<?> source); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...-async/src/main/java/com/raynigon/ecs/logging/async/service/MicrometerMetricsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.raynigon.ecs.logging.async.service; | ||
|
||
import com.raynigon.ecs.logging.async.service.helper.MicrometerTimer; | ||
import com.raynigon.ecs.logging.async.service.helper.TimerWrapper; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Timer; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class MicrometerMetricsService implements AsyncMetricsService { | ||
|
||
|
||
private static final String QUEUE_TIMER_NAME = "raynigon.async.service.queue.duration"; | ||
private static final String EXECUTION_TIMER_NAME = "raynigon.async.service.execution.duration"; | ||
|
||
private final MeterRegistry meterRegistry; | ||
|
||
@Override | ||
public TimerWrapper createQueueTimer(Class<?> source) { | ||
Timer timer = meterRegistry.timer(QUEUE_TIMER_NAME, "source", formatSource(source)); | ||
return wrap(timer); | ||
} | ||
|
||
@Override | ||
public TimerWrapper createExecutionTimer(Class<?> source) { | ||
Timer timer = meterRegistry.timer(EXECUTION_TIMER_NAME, "source", formatSource(source)); | ||
return wrap(timer); | ||
} | ||
|
||
private TimerWrapper wrap(Timer timer) { | ||
return new MicrometerTimer(timer); | ||
} | ||
|
||
private String formatSource(Class<?> source) { | ||
return source.getName().split("/")[0]; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ogging-async/src/main/java/com/raynigon/ecs/logging/async/service/NoOpMetricsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.raynigon.ecs.logging.async.service; | ||
|
||
import com.raynigon.ecs.logging.async.service.helper.NoOpTimer; | ||
import com.raynigon.ecs.logging.async.service.helper.TimerWrapper; | ||
import org.springframework.stereotype.Service; | ||
|
||
public class NoOpMetricsService implements AsyncMetricsService { | ||
@Override | ||
public TimerWrapper createQueueTimer(Class<?> source) { | ||
return new NoOpTimer(); | ||
} | ||
|
||
@Override | ||
public TimerWrapper createExecutionTimer(Class<?> source) { | ||
return new NoOpTimer(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...g-async/src/main/java/com/raynigon/ecs/logging/async/service/helper/MicrometerSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.raynigon.ecs.logging.async.service.helper; | ||
|
||
import io.micrometer.core.instrument.Timer; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class MicrometerSample implements SampleWrapper { | ||
|
||
private final Timer timer; | ||
private final Timer.Sample sample; | ||
|
||
public long stop() { | ||
return sample.stop(timer); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ng-async/src/main/java/com/raynigon/ecs/logging/async/service/helper/MicrometerTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.raynigon.ecs.logging.async.service.helper; | ||
|
||
import io.micrometer.core.instrument.Timer; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
|
||
@RequiredArgsConstructor | ||
public class MicrometerTimer implements TimerWrapper{ | ||
|
||
private final Timer timer; | ||
|
||
@Override | ||
@SneakyThrows | ||
public <V> V recordCallable(Callable<V> callable) { | ||
return timer.recordCallable(callable); | ||
} | ||
|
||
@Override | ||
public <U> U record(Supplier<U> supplier) { | ||
return timer.record(supplier); | ||
} | ||
|
||
@Override | ||
public SampleWrapper start() { | ||
return wrap(timer, Timer.start()); | ||
} | ||
|
||
@Override | ||
public long stop(SampleWrapper wrapper) { | ||
MicrometerSample sample = (MicrometerSample) wrapper; | ||
return sample.stop(); | ||
} | ||
|
||
private SampleWrapper wrap(Timer timer, Timer.Sample sample) { | ||
return new MicrometerSample(timer, sample); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...logging-async/src/main/java/com/raynigon/ecs/logging/async/service/helper/NoOpSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.raynigon.ecs.logging.async.service.helper; | ||
|
||
public class NoOpSample implements SampleWrapper { | ||
} |
29 changes: 29 additions & 0 deletions
29
ecs-logging-async/src/main/java/com/raynigon/ecs/logging/async/service/helper/NoOpTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.raynigon.ecs.logging.async.service.helper; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
|
||
public class NoOpTimer implements TimerWrapper { | ||
@Override | ||
@SneakyThrows | ||
public <V> V recordCallable(Callable<V> callable) { | ||
return callable.call(); | ||
} | ||
|
||
@Override | ||
public <U> U record(Supplier<U> supplier) { | ||
return supplier.get(); | ||
} | ||
|
||
@Override | ||
public SampleWrapper start() { | ||
return new NoOpSample(); | ||
} | ||
|
||
@Override | ||
public long stop(SampleWrapper wrapper) { | ||
return -1; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ging-async/src/main/java/com/raynigon/ecs/logging/async/service/helper/SampleWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.raynigon.ecs.logging.async.service.helper; | ||
|
||
public interface SampleWrapper { | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...gging-async/src/main/java/com/raynigon/ecs/logging/async/service/helper/TimerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.raynigon.ecs.logging.async.service.helper; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
|
||
public interface TimerWrapper { | ||
<V> V recordCallable(Callable<V> callable); | ||
|
||
<U> U record(Supplier<U> supplier); | ||
|
||
SampleWrapper start(); | ||
|
||
long stop(SampleWrapper wrapper); | ||
} |
24 changes: 24 additions & 0 deletions
24
...-async/src/test/groovy/com/raynigon/ecs/logging/async/FallbackMetricsServiceITSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.raynigon.ecs.logging.async | ||
|
||
import com.raynigon.ecs.logging.async.helper.MeterRegistryProvider | ||
import com.raynigon.ecs.logging.async.service.AsyncMetricsService | ||
import com.raynigon.ecs.logging.async.service.NoOpMetricsService | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.ContextConfiguration | ||
import spock.lang.Specification | ||
|
||
@EnableAutoConfiguration | ||
@ContextConfiguration(classes = [MdcExecutorConfiguration, MeterRegistryProvider]) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = []) | ||
class FallbackMetricsServiceITSpec extends Specification { | ||
|
||
@Autowired | ||
AsyncMetricsService metricsService | ||
|
||
def "test"(){ | ||
expect: | ||
metricsService instanceof NoOpMetricsService | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...sync/src/test/groovy/com/raynigon/ecs/logging/async/MicrometerMetricsServiceITSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.raynigon.ecs.logging.async | ||
|
||
import com.raynigon.ecs.logging.async.helper.MeterRegistryProvider | ||
import com.raynigon.ecs.logging.async.service.AsyncMetricsService | ||
import com.raynigon.ecs.logging.async.service.MicrometerMetricsService | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.ContextConfiguration | ||
import spock.lang.Specification | ||
|
||
@EnableAutoConfiguration | ||
@ContextConfiguration(classes = [MdcExecutorConfiguration, MeterRegistryProvider]) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = ["raynigon.logging.async.metrics.enabled=true"]) | ||
class MicrometerMetricsServiceITSpec extends Specification { | ||
|
||
@Autowired | ||
AsyncMetricsService metricsService | ||
|
||
def "test"() { | ||
expect: | ||
metricsService instanceof MicrometerMetricsService | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...rc/test/groovy/com/raynigon/ecs/logging/async/service/MicrometerMetricsServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.raynigon.ecs.logging.async.service | ||
|
||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class MicrometerMetricsServiceSpec extends Specification { | ||
|
||
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry() | ||
|
||
@Subject | ||
MicrometerMetricsService service = new MicrometerMetricsService(meterRegistry) | ||
|
||
def "create queue timer"() { | ||
when: | ||
service.createQueueTimer(String.class).record({ null }) | ||
|
||
then: | ||
meterRegistry.find(MicrometerMetricsService.QUEUE_TIMER_NAME).timer().count() == 1 | ||
meterRegistry.find(MicrometerMetricsService.EXECUTION_TIMER_NAME).timer() == null | ||
} | ||
|
||
def "create execution timer"() { | ||
when: | ||
service.createExecutionTimer(String.class).record({ null }) | ||
|
||
then: | ||
meterRegistry.find(MicrometerMetricsService.QUEUE_TIMER_NAME).timer() == null | ||
meterRegistry.find(MicrometerMetricsService.EXECUTION_TIMER_NAME).timer().count() == 1 | ||
} | ||
} |
Oops, something went wrong.