Skip to content

Commit

Permalink
port 8080 was blocked (#11375)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger authored May 17, 2024
1 parent 974b28e commit bbfe5a6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

package io.opentelemetry.spring.smoketest;

import static org.assertj.core.api.Assertions.assertThat;

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.web.server.LocalServerPort;
import org.springframework.web.client.RestClient;

@SpringBootTest(
classes = {
Expand All @@ -21,8 +26,21 @@
})
class OtelSpringStarterSmokeTest extends AbstractOtelSpringStarterSmokeTest {

@Autowired RestClient.Builder restClientBuilder;
@LocalServerPort private int port;

@Test
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
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.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
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()},
Expand All @@ -55,6 +58,8 @@ class AbstractOtelSpringStarterSmokeTest extends AbstractSpringStarterSmokeTest
@Autowired private PropagationProperties propagationProperties;
@Autowired private OtelResourceProperties otelResourceProperties;
@Autowired private OtlpExporterProperties otlpExporterProperties;
@Autowired private RestTemplateBuilder restTemplateBuilder;
@LocalServerPort private int port;

@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
Expand Down Expand Up @@ -168,26 +173,17 @@ void shouldSendTelemetry() {

@Test
void restTemplate() {
assertClient(OtelSpringStarterSmokeTestController.REST_TEMPLATE);
}

protected void assertClient(String url) {
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(
traceAssert ->
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
.hasKind(SpanKind.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.LongHistogram;
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.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OtelSpringStarterSmokeTestController {
Expand All @@ -24,33 +20,15 @@ public class OtelSpringStarterSmokeTestController {
public static final String TEST_HISTOGRAM = "histogram-test-otel-spring-starter";
public static final String METER_SCOPE_NAME = "scope";
private final LongHistogram histogram;
private final Optional<RestTemplate> restTemplate;
private final Optional<ServletWebServerApplicationContext> server;

public OtelSpringStarterSmokeTestController(
OpenTelemetry openTelemetry,
RestTemplateBuilder restTemplateBuilder,
Optional<ServletWebServerApplicationContext> server) {
this.server = server;
public OtelSpringStarterSmokeTestController(OpenTelemetry openTelemetry) {
Meter meter = openTelemetry.getMeter(METER_SCOPE_NAME);
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)
public String ping() {
histogram.record(10);
return "pong";
}

@GetMapping(REST_TEMPLATE)
public String restTemplate() {
return restTemplate
.map(t -> t.getForObject(PING, String.class))
.orElseThrow(() -> new IllegalStateException("RestTemplate not available"));
}
}

0 comments on commit bbfe5a6

Please sign in to comment.