port 8080 was blocked (#11375)

This commit is contained in:
Gregor Zeitlinger 2024-05-17 16:21:36 +02:00 committed by GitHub
parent 974b28e192
commit bbfe5a6800
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 73 deletions

View File

@ -1,35 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.spring.smoketest;
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;
@RestController
public class RestClientSmokeTestController {
public static final String REST_CLIENT = "/rest-client";
private final Optional<RestClient> restClient;
public RestClientSmokeTestController(
RestClient.Builder restClientBuilder, OtelSpringStarterSmokeTestController controller) {
restClient = controller.getRootUri().map(uri -> restClientBuilder.baseUrl(uri).build());
}
@GetMapping(REST_CLIENT)
public String restClient() {
return restClient
.map(
c ->
c.get()
.uri(OtelSpringStarterSmokeTestController.PING)
.retrieve()
.body(String.class))
.orElseThrow(() -> new IllegalStateException("RestClient not available"));
}
}

View File

@ -5,8 +5,13 @@
package io.opentelemetry.spring.smoketest; package io.opentelemetry.spring.smoketest;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.web.client.RestClient;
@SpringBootTest( @SpringBootTest(
classes = { classes = {
@ -21,8 +26,21 @@ import org.springframework.boot.test.context.SpringBootTest;
}) })
class OtelSpringStarterSmokeTest extends AbstractOtelSpringStarterSmokeTest { class OtelSpringStarterSmokeTest extends AbstractOtelSpringStarterSmokeTest {
@Autowired RestClient.Builder restClientBuilder;
@LocalServerPort private int port;
@Test @Test
void restClient() { void restClient() {
assertClient(OtelSpringStarterSmokeTestController.REST_CLIENT); testing.clearAllExportedData();
RestClient client = restClientBuilder.baseUrl("http://localhost:" + port).build();
assertThat(
client
.get()
.uri(OtelSpringStarterSmokeTestController.PING)
.retrieve()
.body(String.class))
.isEqualTo("pong");
assertClient();
} }
} }

View File

@ -35,12 +35,15 @@ import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestTemplate;
/** /**
* This test class enforces the order of the tests to make sure that {@link #shouldSendTelemetry()}, * This test class enforces the order of the tests to make sure that {@link #shouldSendTelemetry()},
@ -55,6 +58,8 @@ class AbstractOtelSpringStarterSmokeTest extends AbstractSpringStarterSmokeTest
@Autowired private PropagationProperties propagationProperties; @Autowired private PropagationProperties propagationProperties;
@Autowired private OtelResourceProperties otelResourceProperties; @Autowired private OtelResourceProperties otelResourceProperties;
@Autowired private OtlpExporterProperties otlpExporterProperties; @Autowired private OtlpExporterProperties otlpExporterProperties;
@Autowired private RestTemplateBuilder restTemplateBuilder;
@LocalServerPort private int port;
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class TestConfiguration { static class TestConfiguration {
@ -168,26 +173,17 @@ class AbstractOtelSpringStarterSmokeTest extends AbstractSpringStarterSmokeTest
@Test @Test
void restTemplate() { void restTemplate() {
assertClient(OtelSpringStarterSmokeTestController.REST_TEMPLATE);
}
protected void assertClient(String url) {
testing.clearAllExportedData(); testing.clearAllExportedData();
testRestTemplate.getForObject(url, String.class); RestTemplate restTemplate = restTemplateBuilder.rootUri("http://localhost:" + port).build();
restTemplate.getForObject(OtelSpringStarterSmokeTestController.PING, String.class);
assertClient();
}
protected void assertClient() {
testing.waitAndAssertTraces( testing.waitAndAssertTraces(
traceAssert -> traceAssert ->
traceAssert.hasSpansSatisfyingExactly( traceAssert.hasSpansSatisfyingExactly(
clientSpan ->
clientSpan
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfying(
a -> assertThat(a.get(UrlAttributes.URL_FULL)).endsWith(url)),
serverSpan ->
serverSpan
.hasKind(SpanKind.SERVER)
.hasAttribute(HttpAttributes.HTTP_ROUTE, url),
nestedClientSpan -> nestedClientSpan ->
nestedClientSpan nestedClientSpan
.hasKind(SpanKind.CLIENT) .hasKind(SpanKind.CLIENT)

View File

@ -8,12 +8,8 @@ package io.opentelemetry.spring.smoketest;
import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.LongHistogram; import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.Meter;
import java.util.Optional;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController @RestController
public class OtelSpringStarterSmokeTestController { public class OtelSpringStarterSmokeTestController {
@ -24,21 +20,10 @@ public class OtelSpringStarterSmokeTestController {
public static final String TEST_HISTOGRAM = "histogram-test-otel-spring-starter"; public static final String TEST_HISTOGRAM = "histogram-test-otel-spring-starter";
public static final String METER_SCOPE_NAME = "scope"; public static final String METER_SCOPE_NAME = "scope";
private final LongHistogram histogram; private final LongHistogram histogram;
private final Optional<RestTemplate> restTemplate;
private final Optional<ServletWebServerApplicationContext> server;
public OtelSpringStarterSmokeTestController( public OtelSpringStarterSmokeTestController(OpenTelemetry openTelemetry) {
OpenTelemetry openTelemetry,
RestTemplateBuilder restTemplateBuilder,
Optional<ServletWebServerApplicationContext> server) {
this.server = server;
Meter meter = openTelemetry.getMeter(METER_SCOPE_NAME); Meter meter = openTelemetry.getMeter(METER_SCOPE_NAME);
histogram = meter.histogramBuilder(TEST_HISTOGRAM).ofLongs().build(); histogram = meter.histogramBuilder(TEST_HISTOGRAM).ofLongs().build();
restTemplate = getRootUri().map(uri -> restTemplateBuilder.rootUri(uri).build());
}
public Optional<String> getRootUri() {
return server.map(s -> "http://localhost:" + s.getWebServer().getPort());
} }
@GetMapping(PING) @GetMapping(PING)
@ -46,11 +31,4 @@ public class OtelSpringStarterSmokeTestController {
histogram.record(10); histogram.record(10);
return "pong"; return "pong";
} }
@GetMapping(REST_TEMPLATE)
public String restTemplate() {
return restTemplate
.map(t -> t.getForObject(PING, String.class))
.orElseThrow(() -> new IllegalStateException("RestTemplate not available"));
}
} }