Skip to content

Commit

Permalink
Hibernate 6: don't record NoResultException (#12879)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Dec 12, 2024
1 parent 37211a7 commit 9da29ca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Named.named;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.trace.data.StatusData;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.LockModeType;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.hibernate.Version;
Expand Down Expand Up @@ -324,6 +329,29 @@ void testAttachesStateToQuery(Function<EntityManager, Query> queryBuildMethod) {
.get(stringKey("hibernate.session_id"))))));
}

@Test
void testNoResultExceptionIgnored() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("from Value where id = :id");
query.setParameter("id", 1000L);
assertThatThrownBy(query::getSingleResult).isInstanceOf(NoResultException.class);
entityManager.close();

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SELECT Value")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasStatus(StatusData.unset())
.hasEvents(emptyList()),
span ->
span.hasName("SELECT db1.Value")
.hasKind(SpanKind.CLIENT)
.hasParent(trace.getSpan(0))));
}

private static Stream<Arguments> provideArgumentsAttachesState() {
return Stream.of(
Arguments.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(
// not instrumenting getSingleResult as it calls list that is instrumented and
// we don't want to record the NoResultException that it throws
namedOneOf(
"list",
"getResultList",
"stream",
"getResultStream",
"uniqueResult",
"getSingleResult",
"getSingleResultOrNull",
"uniqueResultOptional",
"executeUpdate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected static void setup() {
// Pre-populate the DB, so delete/update can be tested.
Session writer = sessionFactory.openSession();
writer.beginTransaction();
prepopulated = new ArrayList<Value>();
prepopulated = new ArrayList<>();
for (int i = 0; i < 5; i++) {
prepopulated.add(new Value("Hello :) " + i));
writer.persist(prepopulated.get(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Named.named;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.LockModeType;
import jakarta.persistence.NoResultException;
import jakarta.persistence.Persistence;
import jakarta.persistence.Query;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -166,6 +171,29 @@ void testAttachesStateToQuery(Parameter parameter) {
.get(AttributeKey.stringKey("hibernate.session_id")))));
}

@Test
void testNoResultExceptionIgnored() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("from Value where id = :id");
query.setParameter("id", 1000);
assertThatThrownBy(query::getSingleResult).isInstanceOf(NoResultException.class);
entityManager.close();

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SELECT " + Value.class.getName())
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasStatus(StatusData.unset())
.hasEvents(emptyList()),
span ->
span.hasName("SELECT db1.Value")
.hasKind(SpanKind.CLIENT)
.hasParent(trace.getSpan(0))));
}

private static Stream<Arguments> provideHibernateActionParameters() {
List<BiConsumer<EntityManager, Value>> sessionMethodTests =
Arrays.asList(
Expand Down

0 comments on commit 9da29ca

Please sign in to comment.