Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vertx openTelemetry scenario #166

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions 300-quarkus-vertx-webClient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<artifactId>quarkus-opentelemetry</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
<artifactId>quarkus-opentelemetry-exporter-jaeger</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -32,13 +32,25 @@
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-web-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package io.quarkus.qe.vertx.webclient;
package io.quarkus.qe.vertx.webclient.handler;

import static io.quarkus.vertx.web.Route.HttpMethod;

import java.net.HttpURLConnection;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.qe.vertx.webclient.config.ChuckEndpointValue;
import io.quarkus.qe.vertx.webclient.config.VertxWebClientConfig;
import io.quarkus.qe.vertx.webclient.model.Joke;
import io.quarkus.vertx.web.Route;
import io.quarkus.vertx.web.RouteBase;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.Json;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import io.vertx.mutiny.ext.web.client.WebClient;
import io.vertx.mutiny.ext.web.client.predicate.ResponsePredicate;
import io.vertx.mutiny.ext.web.codec.BodyCodec;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;

@Path("/chuck")
@RouteBase(path = "/chuck")
public class ChuckNorrisResource {

@Inject
Expand All @@ -41,51 +44,46 @@ void initialize() {
this.client = WebClient.create(vertx);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/")
public Uni<Response> getRandomJoke() {
@Route(methods = HttpMethod.GET, path = "/")
public Uni<Joke> getRandomJoke() {
return getChuckQuoteAsJoke()
.map(resp -> Response.ok(resp).build())
.ifNoItem().after(Duration.ofSeconds(httpClientConf.timeout)).fail()
.onFailure().retry().atMost(httpClientConf.retries);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/bodyCodec")
public Uni<Response> getRandomJokeWithBodyCodec() {
return client.getAbs(chuckNorrisQuote.getValue())
@Route(methods = HttpMethod.GET, path = "/bodyCodec", produces = "application/json")
public Uni<Joke> getRandomJokeWithBodyCodec() {
return client.getAbs(chuckNorrisQuote.getValue())
.as(BodyCodec.json(Joke.class))
.putHeader("Accept", "application/json")
.expect(ResponsePredicate.status(Response.Status.OK.getStatusCode()))
.expect(ResponsePredicate.status(HttpURLConnection.HTTP_OK))
.send()
.map(resp -> Response.ok(resp.body()).build())
.map(HttpResponse::body)
.ifNoItem().after(Duration.ofSeconds(httpClientConf.timeout)).fail()
.onFailure().retry().atMost(httpClientConf.retries);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/combine")
public Uni<Response> getTwoRandomJokes() {
@Route(methods = HttpMethod.GET, path = "/combine", produces = "application/json")
public Uni<List<Joke>> getTwoRandomJokes() {
Uni<Joke> jokeOne = getChuckQuoteAsJoke();
Uni<Joke> jokeTwo = getChuckQuoteAsJoke();

return Uni.combine()
.all()
.unis(jokeOne, jokeTwo)
.combinedWith((BiFunction<Joke, Joke, List<Joke>>) Arrays::asList)
.map(resp -> Response.ok(Json.encode(resp)).build());
.combinedWith((BiFunction<Joke, Joke, List<Joke>>) Arrays::asList);
}

@Route(methods = HttpMethod.GET, path = "/pong", produces = "application/json")
public Uni<String> ping() {
return Uni.createFrom().item("pong");
}

private Uni<Joke> getChuckQuoteAsJoke() {
return client.getAbs(chuckNorrisQuote.getValue())
.putHeader("Accept", "application/json")
.expect(ResponsePredicate.status(Response.Status.OK.getStatusCode()))
.expect(ResponsePredicate.status(HttpURLConnection.HTTP_OK))
.send()
.map(resp -> resp.bodyAsJsonObject().mapTo(Joke.class));
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.qe.vertx.webclient.handler;

import io.quarkus.vertx.web.Route;
import io.smallrye.mutiny.TimeoutException;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import java.net.HttpURLConnection;
import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class FailureHandler {
@Route(path = "/*", type = Route.HandlerType.FAILURE, produces = "application/json")
void runtimeFailures(RuntimeException e, HttpServerResponse response) {
if(e instanceof TimeoutException){
response.setStatusCode(HttpURLConnection.HTTP_CLIENT_TIMEOUT).end(Json.encode(new JsonObject().put("msg", e.getMessage())));
}else{
response.setStatusCode(HttpURLConnection.HTTP_INTERNAL_ERROR).end(Json.encode(new JsonObject().put("msg", e.getMessage())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.qe.vertx.webclient.handler;

import javax.inject.Inject;

import io.quarkus.qe.vertx.webclient.service.PongService;
import io.quarkus.vertx.web.Route;
import io.quarkus.vertx.web.RouteBase;
import io.smallrye.mutiny.Uni;

@RouteBase(path = "/trace")
public class TracingExampleResource {

@Inject
PongService pongService;

@Route(methods = Route.HttpMethod.GET, path = "/ping")
Uni<String> pingRequest() {
return pongService.pong().onItem().transform(pong -> "ping-" + pong);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.qe.vertx.webclient;
package io.quarkus.qe.vertx.webclient.model;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.qe.vertx.webclient.service;

import java.net.HttpURLConnection;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import io.vertx.mutiny.ext.web.client.WebClient;
import io.vertx.mutiny.ext.web.client.predicate.ResponsePredicate;

@ApplicationScoped
public class PongService {

@Inject
Vertx vertx;

@ConfigProperty(name = "quarkus.http.port")
public int port;

private WebClient client;
private String basePath;

@PostConstruct
void initialize() {
this.client = WebClient.create(vertx);
this.basePath = "http://localhost:" + port;
}

public Uni<String> pong() {
return client.getAbs(basePath + "/chuck/pong")
.putHeader("Accept", "application/json")
.expect(ResponsePredicate.status(HttpURLConnection.HTTP_OK))
.send()
.map(HttpResponse::bodyAsString);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Configuration file
quarkus.http.port=8081
chucknorris.api.domain=https://api.chucknorris.io
vertx.webclient.timeout-sec=2
vertx.webclient.retries=3
vertx.webclient.retries=3

# Jaeger
quarkus.opentelemetry.tracer.exporter.jaeger.endpoint=http://localhost:14250/api/traces
Loading