|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.spring.webflux.v5_3; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest.controller; |
| 9 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; |
| 10 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR; |
| 11 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION; |
| 12 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD; |
| 13 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.PATH_PARAM; |
| 14 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM; |
| 15 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; |
| 16 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; |
| 17 | +import static java.util.Collections.singletonList; |
| 18 | + |
| 19 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 20 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; |
| 21 | +import java.net.URI; |
| 22 | +import java.util.Properties; |
| 23 | +import org.springframework.boot.SpringApplication; |
| 24 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 25 | +import org.springframework.context.ConfigurableApplicationContext; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.http.HttpStatus; |
| 28 | +import org.springframework.http.ResponseEntity; |
| 29 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 30 | +import org.springframework.stereotype.Controller; |
| 31 | +import org.springframework.web.bind.annotation.PathVariable; |
| 32 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 33 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 34 | +import org.springframework.web.bind.annotation.RequestParam; |
| 35 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 36 | +import org.springframework.web.server.WebFilter; |
| 37 | +import reactor.core.publisher.Flux; |
| 38 | +import reactor.core.publisher.Mono; |
| 39 | + |
| 40 | +@SpringBootApplication |
| 41 | +class TestWebfluxSpringBootApp { |
| 42 | + |
| 43 | + static ConfigurableApplicationContext start(int port, String contextPath) { |
| 44 | + Properties props = new Properties(); |
| 45 | + props.put("server.port", port); |
| 46 | + props.put("spring.webflux.base-path", contextPath); |
| 47 | + |
| 48 | + SpringApplication app = new SpringApplication(TestWebfluxSpringBootApp.class); |
| 49 | + app.setDefaultProperties(props); |
| 50 | + return app.run(); |
| 51 | + } |
| 52 | + |
| 53 | + @Bean |
| 54 | + WebFilter telemetryFilter() { |
| 55 | + return SpringWebfluxServerTelemetry.builder(GlobalOpenTelemetry.get()) |
| 56 | + .setCapturedRequestHeaders(singletonList(AbstractHttpServerTest.TEST_REQUEST_HEADER)) |
| 57 | + .setCapturedResponseHeaders(singletonList(AbstractHttpServerTest.TEST_RESPONSE_HEADER)) |
| 58 | + .build() |
| 59 | + .createWebFilterAndRegisterReactorHook(); |
| 60 | + } |
| 61 | + |
| 62 | + @Controller |
| 63 | + static class TestController { |
| 64 | + |
| 65 | + @RequestMapping("/success") |
| 66 | + @ResponseBody |
| 67 | + Flux<String> success() { |
| 68 | + return Flux.defer(() -> Flux.just(controller(SUCCESS, SUCCESS::getBody))); |
| 69 | + } |
| 70 | + |
| 71 | + @RequestMapping("/no-mono") |
| 72 | + @ResponseBody |
| 73 | + String noMono() { |
| 74 | + return controller(SUCCESS, SUCCESS::getBody); |
| 75 | + } |
| 76 | + |
| 77 | + @RequestMapping("/query") |
| 78 | + @ResponseBody |
| 79 | + Mono<String> query_param(@RequestParam("some") String param) { |
| 80 | + return Mono.just(controller(QUERY_PARAM, () -> "some=" + param)); |
| 81 | + } |
| 82 | + |
| 83 | + @RequestMapping("/redirect") |
| 84 | + @ResponseBody |
| 85 | + Mono<Void> redirect(ServerHttpResponse response) { |
| 86 | + response.setStatusCode(HttpStatus.FOUND); |
| 87 | + response.getHeaders().setLocation(URI.create("/redirected")); |
| 88 | + return controller(REDIRECT, response::setComplete); |
| 89 | + } |
| 90 | + |
| 91 | + @RequestMapping("/error-status") |
| 92 | + Mono<ResponseEntity<String>> error() { |
| 93 | + return Mono.just( |
| 94 | + controller( |
| 95 | + ERROR, |
| 96 | + () -> new ResponseEntity<>(ERROR.getBody(), HttpStatus.valueOf(ERROR.getStatus())))); |
| 97 | + } |
| 98 | + |
| 99 | + @RequestMapping("/exception") |
| 100 | + Flux<ResponseEntity<String>> exception() throws Exception { |
| 101 | + return Flux.just( |
| 102 | + controller( |
| 103 | + EXCEPTION, |
| 104 | + () -> { |
| 105 | + throw new RuntimeException(EXCEPTION.getBody()); |
| 106 | + })); |
| 107 | + } |
| 108 | + |
| 109 | + @RequestMapping("/captureHeaders") |
| 110 | + Mono<ResponseEntity<String>> capture_headers( |
| 111 | + @RequestHeader("X-Test-Request") String testRequestHeader) { |
| 112 | + return Mono.just( |
| 113 | + controller( |
| 114 | + CAPTURE_HEADERS, |
| 115 | + () -> |
| 116 | + ResponseEntity.ok() |
| 117 | + .header("X-Test-Response", testRequestHeader) |
| 118 | + .body(CAPTURE_HEADERS.getBody()))); |
| 119 | + } |
| 120 | + |
| 121 | + @RequestMapping("/path/{id}/param") |
| 122 | + @ResponseBody |
| 123 | + Mono<String> path_param(@PathVariable("id") int id) { |
| 124 | + return Mono.just(controller(PATH_PARAM, () -> String.valueOf(id))); |
| 125 | + } |
| 126 | + |
| 127 | + @RequestMapping("/child") |
| 128 | + @ResponseBody |
| 129 | + Mono<String> indexed_child(@RequestParam("id") String id) { |
| 130 | + return Mono.just( |
| 131 | + controller( |
| 132 | + INDEXED_CHILD, |
| 133 | + () -> { |
| 134 | + INDEXED_CHILD.collectSpanAttributes(name -> name.equals("id") ? id : null); |
| 135 | + return INDEXED_CHILD.getBody(); |
| 136 | + })); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments