Skip to content

Commit 55b8579

Browse files
Observe instantaneous events (#3247)
* Draft for signaling instantaneous events * Contextual name can be null * Add tests * Use name for contextual name as a fallback + javadoc * More javadoc * Prefix the name of the counter with the name of the observation.
1 parent a9b4b72 commit 55b8579

19 files changed

Lines changed: 195 additions & 20 deletions

File tree

micrometer-core/src/main/java/io/micrometer/core/instrument/observation/TimerObservationHandler.java renamed to micrometer-core/src/main/java/io/micrometer/core/instrument/observation/DefaultMeterObservationHandler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.stream.Collectors;
2222

2323
/**
24-
* Handler for {@link Timer.Sample}.
24+
* Handler for {@link Timer.Sample} and {@link Counter}
2525
*
2626
* WARNING: Since the {@link LongTaskTimer} needs to be created in the {@code onStart}
2727
* method, it can only contain tags that are available by that time. This means that if
@@ -36,11 +36,11 @@
3636
* @author Jonatan Ivanov
3737
* @since 1.10.0
3838
*/
39-
public class TimerObservationHandler implements MeterObservationHandler<Observation.Context> {
39+
public class DefaultMeterObservationHandler implements MeterObservationHandler<Observation.Context> {
4040

4141
private final MeterRegistry meterRegistry;
4242

43-
public TimerObservationHandler(MeterRegistry meterRegistry) {
43+
public DefaultMeterObservationHandler(MeterRegistry meterRegistry) {
4444
this.meterRegistry = meterRegistry;
4545
}
4646

@@ -65,8 +65,9 @@ public void onStop(Observation.Context context) {
6565
}
6666

6767
@Override
68-
public boolean supportsContext(Observation.Context context) {
69-
return true;
68+
public void onEvent(Observation.Event event, Observation.Context context) {
69+
Counter.builder(context.getName() + "." + event.getName()).tags(createTags(context)).register(meterRegistry)
70+
.increment();
7071
}
7172

7273
private Tags createErrorTags(Observation.Context context) {

micrometer-core/src/main/java/io/micrometer/core/instrument/observation/MeterObservationHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@
2727
*/
2828
public interface MeterObservationHandler<T extends Observation.Context> extends ObservationHandler<T> {
2929

30+
@Override
31+
default boolean supportsContext(Observation.Context context) {
32+
return true;
33+
}
34+
3035
}

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationInterceptorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.micrometer.common.KeyValues;
2222
import io.micrometer.core.instrument.MeterRegistry;
2323
import io.micrometer.core.instrument.MockClock;
24-
import io.micrometer.core.instrument.observation.TimerObservationHandler;
24+
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
2525
import io.micrometer.core.instrument.simple.SimpleConfig;
2626
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
2727
import io.micrometer.observation.Observation;
@@ -76,7 +76,7 @@ private OkHttpObservationInterceptor.Builder defaultInterceptorBuilder() {
7676
@BeforeEach
7777
void setup() {
7878
observationRegistry.observationConfig().observationHandler(testHandler);
79-
observationRegistry.observationConfig().observationHandler(new TimerObservationHandler(registry));
79+
observationRegistry.observationConfig().observationHandler(new DefaultMeterObservationHandler(registry));
8080
observationRegistry.observationConfig().observationHandler(new PropagatingHandler());
8181
}
8282

micrometer-observation-test/src/main/java/io/micrometer/observation/tck/AnyContextObservationHandlerCompatibilityKit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void handlerSupportsAnyContext() {
4141
assertThatCode(() -> handler.onStart(testContext)).doesNotThrowAnyException();
4242
assertThatCode(() -> handler.onStop(testContext)).doesNotThrowAnyException();
4343
assertThatCode(() -> handler.onError(testContext)).doesNotThrowAnyException();
44+
assertThatCode(() -> handler.onEvent(new Observation.Event("testEvent"), testContext))
45+
.doesNotThrowAnyException();
4446
assertThatCode(() -> handler.onScopeOpened(testContext)).doesNotThrowAnyException();
4547
assertThatCode(() -> handler.supportsContext(testContext)).doesNotThrowAnyException();
4648
assertThat(handler.supportsContext(testContext)).as("Handler supports any context").isTrue();

micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ConcreteContextObservationHandlerCompatibilityKit.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ void handlerSupportsConcreteContextForHandlerMethods() {
5858
assertThatCode(() -> handler.onStart(context())).doesNotThrowAnyException();
5959
assertThatCode(() -> handler.onStop(context())).doesNotThrowAnyException();
6060
assertThatCode(() -> handler.onError(context())).doesNotThrowAnyException();
61+
assertThatCode(() -> handler.onEvent(new Observation.Event("testEvent"), context())).doesNotThrowAnyException();
6162
assertThatCode(() -> handler.onScopeOpened(context())).doesNotThrowAnyException();
6263
}
6364

micrometer-observation-test/src/main/java/io/micrometer/observation/tck/NullContextObservationHandlerCompatibilityKit.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void handlerSupportsNullContext() {
5656
assertThatCode(() -> handler.onStart(null)).doesNotThrowAnyException();
5757
assertThatCode(() -> handler.onStop(null)).doesNotThrowAnyException();
5858
assertThatCode(() -> handler.onError(null)).doesNotThrowAnyException();
59+
assertThatCode(() -> handler.onEvent(null, null)).doesNotThrowAnyException();
5960
assertThatCode(() -> handler.onScopeOpened(null)).doesNotThrowAnyException();
6061
assertThatCode(() -> handler.supportsContext(null)).doesNotThrowAnyException();
6162
assertThat(handler.supportsContext(null)).as("Handler supports null context").isTrue();

micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationRegistryCompatibilityKit.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.micrometer.common.KeyValue;
1919
import io.micrometer.common.KeyValues;
20+
import io.micrometer.common.lang.Nullable;
2021
import io.micrometer.observation.Observation;
2122
import io.micrometer.observation.ObservationHandler;
2223
import io.micrometer.observation.ObservationRegistry;
@@ -80,6 +81,10 @@ void observeWithHandlers() {
8081
inOrder.verify(handler).onScopeOpened(isA(Observation.Context.class));
8182
assertThat(scope.getCurrentObservation()).isSameAs(observation);
8283

84+
Observation.Event event = new Observation.Event("testEvent", "event for testing");
85+
observation.event(event);
86+
inOrder.verify(handler).onEvent(same(event), isA(Observation.Context.class));
87+
8388
Throwable exception = new IOException("simulated");
8489
observation.error(exception);
8590
inOrder.verify(handler).onError(isA(Observation.Context.class));
@@ -455,10 +460,6 @@ public boolean supportsContext(Observation.Context context) {
455460
return context instanceof TestContext;
456461
}
457462

458-
public String getId() {
459-
return this.id;
460-
}
461-
462463
}
463464

464465
static class UnsupportedKeyValuesProvider implements Observation.GlobalKeyValuesProvider<Observation.Context> {
@@ -490,6 +491,7 @@ static class AssertingHandler implements ObservationHandler<Observation.Context>
490491

491492
private boolean stopped = false;
492493

494+
@Nullable
493495
private Observation.Context context = null;
494496

495497
@Override

micrometer-observation-test/src/test/java/io/micrometer/observation/tck/AnyContextObservationHandlerCompatibilityKitTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public void onStart(Observation.Context context) {
3131
public void onError(Observation.Context context) {
3232
}
3333

34+
@Override
35+
public void onEvent(Observation.Event event, Observation.Context context) {
36+
}
37+
3438
@Override
3539
public void onScopeOpened(Observation.Context context) {
3640
}

micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ConcreteContextObservationHandlerCompatibilityKitTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public void onStart(Observation.Context context) {
3232
public void onError(Observation.Context context) {
3333
}
3434

35+
@Override
36+
public void onEvent(Observation.Event event, Observation.Context context) {
37+
}
38+
3539
@Override
3640
public void onScopeOpened(Observation.Context context) {
3741

micrometer-observation-test/src/test/java/io/micrometer/observation/tck/NullContextObservationHandlerCompatibilityKitTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public void onStart(Observation.Context context) {
3131
public void onError(Observation.Context context) {
3232
}
3333

34+
@Override
35+
public void onEvent(Observation.Event event, Observation.Context context) {
36+
}
37+
3438
@Override
3539
public void onScopeOpened(Observation.Context context) {
3640
}

0 commit comments

Comments
 (0)