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

Improve hibernate reactive instrumentation #9486

Merged
merged 1 commit into from
Sep 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ testing {
implementation("org.hibernate.reactive:hibernate-reactive-core:2.0.0.Final")
implementation("io.vertx:vertx-pg-client:4.4.2")
}
compileOnly("io.vertx:vertx-codegen:4.4.2")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,156 @@ void testStage() throws Exception {
testing.runWithSpan(
"parent",
() ->
Vertx.vertx()
.getOrCreateContext()
.runOnContext(
event ->
stageSessionFactory
.withSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.find(Value.class, 1L)
.thenAccept(
value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
runWithVertx(
() ->
stageSessionFactory
.withSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.find(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageWithStatelessSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.withStatelessSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageSessionWithTransaction() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.withSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.withTransaction(transaction -> session.find(Value.class, 1L))
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageStatelessSessionWithTransaction() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.withStatelessSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.withTransaction(transaction -> session.get(Value.class, 1L))
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageOpenSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.openSession()
.thenApply(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.find(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageOpenStatelessSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.openStatelessSession()
.thenApply(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

private static void runWithVertx(Runnable runnable) {
Vertx.vertx().getOrCreateContext().runOnContext(event -> runnable.run());
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
private static void assertTrace() {
testing.waitAndAssertTraces(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.vertx.core.Vertx;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.hibernate.reactive.mutiny.Mutiny;
import org.hibernate.reactive.stage.Stage;
Expand Down Expand Up @@ -138,6 +140,131 @@ void testStage() throws Exception {
assertTrace();
}

@Test
void testStageWithStatelessSession() throws Exception {
testing
.runWithSpan(
"parent",
() ->
stageSessionFactory
.withStatelessSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.toCompletableFuture())
.get(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageSessionWithTransaction() throws Exception {
testing
.runWithSpan(
"parent",
() ->
stageSessionFactory
.withSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.withTransaction(transaction -> session.find(Value.class, 1L))
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.toCompletableFuture())
.get(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageStatelessSessionWithTransaction() throws Exception {
testing
.runWithSpan(
"parent",
() ->
stageSessionFactory
.withStatelessSession(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.withTransaction(transaction -> session.get(Value.class, 1L))
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.toCompletableFuture())
.get(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageOpenSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.openSession()
.thenApply(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.find(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

@Test
void testStageOpenStatelessSession() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
testing.runWithSpan(
"parent",
() ->
runWithVertx(
() ->
stageSessionFactory
.openStatelessSession()
.thenApply(
session -> {
if (!Span.current().getSpanContext().isValid()) {
throw new IllegalStateException("missing parent span");
}

return session
.get(Value.class, 1L)
.thenAccept(value -> testing.runWithSpan("callback", () -> {}));
})
.thenAccept(unused -> latch.countDown())));
latch.await(30, TimeUnit.SECONDS);

assertTrace();
}

private static void runWithVertx(Runnable runnable) {
Vertx.vertx().getOrCreateContext().runOnContext(event -> runnable.run());
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
private static void assertTrace() {
testing.waitAndAssertTraces(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.hibernate.reactive.v1_0;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public final class CompletionStageWrapper {

private CompletionStageWrapper() {}

public static <T> CompletionStage<T> wrap(CompletionStage<T> future) {
Context context = Context.current();
if (context != Context.root()) {
return wrap(future, context);
}
return future;
}

private static <T> CompletionStage<T> wrap(CompletionStage<T> completionStage, Context context) {
CompletableFuture<T> result = new CompletableFuture<>();
completionStage.whenComplete(
(T value, Throwable throwable) -> {
try (Scope ignored = context.makeCurrent()) {
if (throwable != null) {
result.completeExceptionally(throwable);
} else {
result.complete(value);
}
}
});

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public HibernateReactiveInstrumentationModule() {
@Override
public List<TypeInstrumentation> typeInstrumentations() {
return asList(
new StageSessionFactoryInstrumentation(), new MutinySessionFactoryInstrumentation());
new StageSessionFactoryInstrumentation(),
new StageSessionImplInstrumentation(),
new MutinySessionFactoryInstrumentation());
}
}
Loading
Loading