-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45683 from ozangunalp/vertx_event_bus_consumer_co…
…ntext Dispatch Vert.x event bus consumer events in correct context
- Loading branch information
Showing
10 changed files
with
380 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...opentelemetry-vertx/src/main/java/io/quarkus/it/opentelemetry/vertx/EventBusConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.it.opentelemetry.vertx; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.instrumentation.annotations.WithSpan; | ||
import io.quarkus.logging.Log; | ||
import io.quarkus.vertx.ConsumeEvent; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.smallrye.common.annotation.RunOnVirtualThread; | ||
import io.vertx.core.MultiMap; | ||
|
||
@ApplicationScoped | ||
public class EventBusConsumer { | ||
|
||
@ConsumeEvent("pets") | ||
// non-blocking | ||
public String sayHi(Pet pet) { | ||
Log.infov("Received a pet: {0} {1}", pet, Span.current()); | ||
process(); | ||
return "Hello " + pet.getName() + " (" + pet.getKind() + ")"; | ||
} | ||
|
||
@ConsumeEvent("persons") | ||
@Blocking | ||
public String name(String name) { | ||
Log.infov("Received a pet: {0} {1}", name, Span.current()); | ||
process(); | ||
return "Hello " + name; | ||
} | ||
|
||
@ConsumeEvent("person-headers") | ||
@RunOnVirtualThread | ||
public String personWithHeader(MultiMap headers, Person person) { | ||
Log.infov("Received a person: {0} {1}", person, Span.current()); | ||
process(); | ||
String s = "Hello " + person.getFirstName() + " " + person.getLastName() + ", " + headers; | ||
return s; | ||
} | ||
|
||
@WithSpan | ||
public void process() { | ||
|
||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...s/opentelemetry-vertx/src/main/java/io/quarkus/it/opentelemetry/vertx/EventBusSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.it.opentelemetry.vertx; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.core.eventbus.DeliveryOptions; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.mutiny.core.eventbus.EventBus; | ||
import io.vertx.mutiny.core.eventbus.Message; | ||
|
||
@Path("/event-bus") | ||
public class EventBusSender { | ||
|
||
@Inject | ||
EventBus bus; | ||
|
||
@POST | ||
@Path("/person") | ||
public Uni<String> helloToPerson(JsonObject json) { | ||
return bus.<String> request("persons", json.getString("name")) | ||
.onItem().transform(Message::body); | ||
} | ||
|
||
@POST | ||
@Path("/person2") | ||
@Produces("text/plain") | ||
public Uni<String> helloToPersonWithHeaders(JsonObject json) { | ||
return bus.<String> request( | ||
"person-headers", | ||
new Person(json.getString("firstName"), json.getString("lastName")), | ||
new DeliveryOptions().addHeader("header", "headerValue")) | ||
.onItem().transform(Message::body); | ||
} | ||
|
||
@POST | ||
@Path("/pet") | ||
public Uni<String> helloToPet(JsonObject json) { | ||
return bus.<String> request("pets", new Pet(json.getString("name"), json.getString("kind"))) | ||
.onItem().transform(Message::body); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...ion-tests/opentelemetry-vertx/src/main/java/io/quarkus/it/opentelemetry/vertx/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkus.it.opentelemetry.vertx; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
public class Person { | ||
|
||
private String firstName; | ||
private String lastName; | ||
|
||
public Person(String firstName, String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
public Person() { | ||
// Used by reflection. | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public Person setFirstName(String firstName) { | ||
this.firstName = firstName; | ||
return this; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public Person setLastName(String lastName) { | ||
this.lastName = lastName; | ||
return this; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ration-tests/opentelemetry-vertx/src/main/java/io/quarkus/it/opentelemetry/vertx/Pet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.it.opentelemetry.vertx; | ||
|
||
/** | ||
* Simple pojo. | ||
* The test using this pojo will use the generic codec facility. | ||
*/ | ||
public class Pet { | ||
|
||
private final String name; | ||
|
||
private final String kind; | ||
|
||
public Pet(String name, String kind) { | ||
this.name = name; | ||
this.kind = kind; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getKind() { | ||
return kind; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...tests/opentelemetry-vertx/src/test/java/io/quarkus/it/opentelemetry/vertx/EventBusIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.it.opentelemetry.vertx; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
public class EventBusIT extends EventBusTest { | ||
} |
117 changes: 117 additions & 0 deletions
117
...sts/opentelemetry-vertx/src/test/java/io/quarkus/it/opentelemetry/vertx/EventBusTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package io.quarkus.it.opentelemetry.vertx; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static java.net.HttpURLConnection.HTTP_OK; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.opentelemetry.api.trace.SpanId; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.http.ContentType; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
@QuarkusTest | ||
public class EventBusTest extends SpanExporterBaseTest { | ||
|
||
@BeforeEach | ||
void reset() { | ||
given().get("/reset").then().statusCode(HTTP_OK); | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> getSpans().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testEventBusWithString() { | ||
String body = new JsonObject().put("name", "Bob Morane").toString(); | ||
given().contentType(ContentType.JSON).body(body) | ||
.post("/event-bus/person") | ||
.then().statusCode(200).body(equalTo("Hello Bob Morane")); | ||
|
||
await().atMost(5, TimeUnit.SECONDS).until(() -> getSpans().size() >= 3); | ||
List<Map<String, Object>> spans = getSpans(); | ||
|
||
Map<String, Object> serverCall = getSpanByKindAndParentId(spans, SpanKind.SERVER, SpanId.getInvalid()); | ||
String spanId = getSpanId(serverCall); | ||
assertNotEquals(SpanId.getInvalid(), spanId); | ||
|
||
Map<String, Object> producerSpan = getSpanByKindAndParentId(spans, SpanKind.PRODUCER, spanId); | ||
String producerSpanId = getSpanId(producerSpan); | ||
assertNotEquals(SpanId.getInvalid(), producerSpanId); | ||
|
||
Map<String, Object> consumerSpan = getSpanByKindAndParentId(spans, SpanKind.CONSUMER, producerSpanId); | ||
String consumerSpanId = getSpanId(consumerSpan); | ||
assertNotEquals(SpanId.getInvalid(), consumerSpanId); | ||
|
||
Map<String, Object> methodCallSpan = getSpanByKindAndParentId(spans, SpanKind.INTERNAL, consumerSpanId); | ||
String methodCallSpanId = getSpanId(methodCallSpan); | ||
assertNotEquals(SpanId.getInvalid(), methodCallSpanId); | ||
} | ||
|
||
@Test | ||
public void testEventBusWithObjectAndHeader() { | ||
String body = new JsonObject() | ||
.put("firstName", "Bob") | ||
.put("lastName", "Morane") | ||
.toString(); | ||
given().contentType(ContentType.JSON).body(body) | ||
.post("/event-bus/person2") | ||
.then().statusCode(200) | ||
// For some reason Multimap.toString() has \n at the end. | ||
.body(CoreMatchers.startsWith("Hello Bob Morane, header=headerValue\n")); | ||
|
||
await().atMost(5, TimeUnit.SECONDS).until(() -> getSpans().size() >= 3); | ||
List<Map<String, Object>> spans = getSpans(); | ||
|
||
Map<String, Object> serverCall = getSpanByKindAndParentId(spans, SpanKind.SERVER, SpanId.getInvalid()); | ||
String spanId = getSpanId(serverCall); | ||
assertNotEquals(SpanId.getInvalid(), spanId); | ||
|
||
Map<String, Object> producerSpan = getSpanByKindAndParentId(spans, SpanKind.PRODUCER, spanId); | ||
String producerSpanId = getSpanId(producerSpan); | ||
assertNotEquals(SpanId.getInvalid(), producerSpanId); | ||
|
||
Map<String, Object> consumerSpan = getSpanByKindAndParentId(spans, SpanKind.CONSUMER, producerSpanId); | ||
String consumerSpanId = getSpanId(consumerSpan); | ||
assertNotEquals(SpanId.getInvalid(), consumerSpanId); | ||
|
||
Map<String, Object> methodCallSpan = getSpanByKindAndParentId(spans, SpanKind.INTERNAL, consumerSpanId); | ||
String methodCallSpanId = getSpanId(methodCallSpan); | ||
assertNotEquals(SpanId.getInvalid(), methodCallSpanId); | ||
} | ||
|
||
@Test | ||
public void testEventBusWithPet() { | ||
String body = new JsonObject().put("name", "Neo").put("kind", "rabbit").toString(); | ||
given().contentType(ContentType.JSON).body(body) | ||
.post("/event-bus/pet") | ||
.then().statusCode(200).body(equalTo("Hello Neo (rabbit)")); | ||
|
||
await().atMost(5, TimeUnit.SECONDS).until(() -> getSpans().size() >= 3); | ||
List<Map<String, Object>> spans = getSpans(); | ||
|
||
Map<String, Object> serverCall = getSpanByKindAndParentId(spans, SpanKind.SERVER, SpanId.getInvalid()); | ||
String spanId = getSpanId(serverCall); | ||
assertNotEquals(SpanId.getInvalid(), spanId); | ||
|
||
Map<String, Object> producerSpan = getSpanByKindAndParentId(spans, SpanKind.PRODUCER, spanId); | ||
String producerSpanId = getSpanId(producerSpan); | ||
assertNotEquals(SpanId.getInvalid(), producerSpanId); | ||
|
||
Map<String, Object> consumerSpan = getSpanByKindAndParentId(spans, SpanKind.CONSUMER, producerSpanId); | ||
String consumerSpanId = getSpanId(consumerSpan); | ||
assertNotEquals(SpanId.getInvalid(), consumerSpanId); | ||
|
||
Map<String, Object> methodCallSpan = getSpanByKindAndParentId(spans, SpanKind.INTERNAL, consumerSpanId); | ||
String methodCallSpanId = getSpanId(methodCallSpan); | ||
assertNotEquals(SpanId.getInvalid(), methodCallSpanId); | ||
} | ||
} |
Oops, something went wrong.