|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 |
|
20 | 20 | import java.util.Optional;
|
21 | 21 |
|
| 22 | +import io.micrometer.observation.Observation; |
22 | 23 | import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
|
23 | 24 | import io.micrometer.observation.tck.TestObservationRegistry;
|
24 | 25 | import io.micrometer.observation.tck.TestObservationRegistryAssert;
|
|
27 | 28 | import reactor.core.publisher.Mono;
|
28 | 29 | import reactor.test.StepVerifier;
|
29 | 30 |
|
| 31 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
30 | 32 | import org.springframework.http.server.reactive.observation.ServerRequestObservationContext;
|
31 | 33 | import org.springframework.web.server.ServerWebExchange;
|
32 | 34 | import org.springframework.web.server.WebFilterChain;
|
@@ -65,7 +67,10 @@ void filterShouldAddNewObservationToReactorContext() {
|
65 | 67 | ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/test/resource"));
|
66 | 68 | exchange.getResponse().setRawStatusCode(200);
|
67 | 69 | WebFilterChain filterChain = webExchange -> Mono.deferContextual(contextView -> {
|
68 |
| - assertThat(contextView.getOrEmpty(ObservationThreadLocalAccessor.KEY)).isPresent(); |
| 70 | + Observation observation = contextView.get(ObservationThreadLocalAccessor.KEY); |
| 71 | + assertThat(observation).isNotNull(); |
| 72 | + // check that the observation was started |
| 73 | + assertThat(observation.getContext().getLowCardinalityKeyValue("outcome")).isNotNull(); |
69 | 74 | return Mono.empty();
|
70 | 75 | });
|
71 | 76 | this.filter.filter(exchange, filterChain).block();
|
@@ -99,6 +104,25 @@ void filterShouldRecordObservationWhenCancelled() {
|
99 | 104 | assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "UNKNOWN");
|
100 | 105 | }
|
101 | 106 |
|
| 107 | + @Test |
| 108 | + void filterShouldStopObservationOnResponseCommit() { |
| 109 | + ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/test/resource")); |
| 110 | + WebFilterChain filterChain = createFilterChain(filterExchange -> { |
| 111 | + throw new IllegalArgumentException("server error"); |
| 112 | + }); |
| 113 | + StepVerifier.create(this.filter.filter(exchange, filterChain).doOnError(throwable -> { |
| 114 | + ServerHttpResponse response = exchange.getResponse(); |
| 115 | + response.setRawStatusCode(500); |
| 116 | + response.setComplete().block(); |
| 117 | + })) |
| 118 | + .expectError(IllegalArgumentException.class) |
| 119 | + .verify(); |
| 120 | + Optional<ServerRequestObservationContext> observationContext = ServerHttpObservationFilter.findObservationContext(exchange); |
| 121 | + assertThat(observationContext.get().getError()).isInstanceOf(IllegalArgumentException.class); |
| 122 | + assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SERVER_ERROR"); |
| 123 | + } |
| 124 | + |
| 125 | + |
102 | 126 | private WebFilterChain createFilterChain(ThrowingConsumer<ServerWebExchange> exchangeConsumer) {
|
103 | 127 | return filterExchange -> {
|
104 | 128 | try {
|
|
0 commit comments