From a9905a22cab4e3a38291993bfda839c588d4987f Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 31 Mar 2023 15:39:02 +0300 Subject: [PATCH] Fix hibernate 6 latest dep test (#8189) --- .../hibernate/v6_0/SessionFactoryInstrumentation.java | 7 ++++++- .../hibernate/v6_0/SessionInstrumentation.java | 10 ++++++++-- .../javaagent/src/test/groovy/ProcedureCallTest.groovy | 2 +- .../hibernate-6.0/spring-testing/build.gradle.kts | 6 ++++++ .../src/test/groovy/SpringJpaTest.groovy | 6 +++--- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionFactoryInstrumentation.java b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionFactoryInstrumentation.java index 116c926baa31..ae5807c5b8a9 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionFactoryInstrumentation.java +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionFactoryInstrumentation.java @@ -40,7 +40,12 @@ public void transform(TypeTransformer transformer) { isMethod() .and(namedOneOf("openSession", "openStatelessSession")) .and(takesArguments(0)) - .and(returns(namedOneOf("org.hibernate.Session", "org.hibernate.StatelessSession"))), + .and( + returns( + namedOneOf( + "org.hibernate.Session", + "org.hibernate.StatelessSession", + "org.hibernate.internal.SessionImpl"))), SessionFactoryInstrumentation.class.getName() + "$SessionFactoryAdvice"); } diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java index 46bafd78fbc1..fde18f963d42 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v6_0/SessionInstrumentation.java @@ -85,7 +85,9 @@ public void transform(TypeTransformer transformer) { transformer.applyAdviceToMethod( isMethod() - .and(returns(implementsInterface(named("org.hibernate.query.CommonQueryContract")))), + .and( + returns(implementsInterface(named("org.hibernate.query.CommonQueryContract"))) + .or(named("org.hibernate.query.spi.QueryImplementor"))), SessionInstrumentation.class.getName() + "$GetQueryAdvice"); } @@ -150,7 +152,11 @@ public static class GetQueryAdvice { @Advice.OnMethodExit(suppress = Throwable.class) public static void getQuery( - @Advice.This SharedSessionContract session, @Advice.Return CommonQueryContract query) { + @Advice.This SharedSessionContract session, @Advice.Return Object queryObject) { + if (!(queryObject instanceof CommonQueryContract)) { + return; + } + CommonQueryContract query = (CommonQueryContract) queryObject; VirtualField sessionVirtualField = VirtualField.find(SharedSessionContract.class, SessionInfo.class); diff --git a/instrumentation/hibernate/hibernate-6.0/javaagent/src/test/groovy/ProcedureCallTest.groovy b/instrumentation/hibernate/hibernate-6.0/javaagent/src/test/groovy/ProcedureCallTest.groovy index 6c0c0a56de35..f7827670d250 100644 --- a/instrumentation/hibernate/hibernate-6.0/javaagent/src/test/groovy/ProcedureCallTest.groovy +++ b/instrumentation/hibernate/hibernate-6.0/javaagent/src/test/groovy/ProcedureCallTest.groovy @@ -153,7 +153,7 @@ class ProcedureCallTest extends AgentInstrumentationSpecification { kind INTERNAL childOf span(0) status ERROR - errorEvent(SQLGrammarException, "could not prepare statement") + errorEvent(SQLGrammarException, ~/could not prepare statement/) attributes { "hibernate.session_id" { sessionId = it diff --git a/instrumentation/hibernate/hibernate-6.0/spring-testing/build.gradle.kts b/instrumentation/hibernate/hibernate-6.0/spring-testing/build.gradle.kts index d081da37d580..7b0c7f9cd272 100644 --- a/instrumentation/hibernate/hibernate-6.0/spring-testing/build.gradle.kts +++ b/instrumentation/hibernate/hibernate-6.0/spring-testing/build.gradle.kts @@ -2,6 +2,8 @@ plugins { id("otel.javaagent-testing") } +val springAgent by configurations.creating + dependencies { library("org.hibernate:hibernate-core:6.0.0.Final") @@ -14,6 +16,8 @@ dependencies { testImplementation("org.hsqldb:hsqldb:2.0.0") testImplementation("org.springframework.data:spring-data-jpa:3.0.0") + + springAgent("org.springframework:spring-instrument:6.0.7") } otelJava { @@ -21,6 +25,8 @@ otelJava { } tasks.withType().configureEach { + jvmArgs("-javaagent:" + springAgent.singleFile.absolutePath) + // TODO run tests both with and without experimental span attributes jvmArgs("-Dotel.instrumentation.hibernate.experimental-span-attributes=true") } diff --git a/instrumentation/hibernate/hibernate-6.0/spring-testing/src/test/groovy/SpringJpaTest.groovy b/instrumentation/hibernate/hibernate-6.0/spring-testing/src/test/groovy/SpringJpaTest.groovy index 7eda918d7ee7..5797d279a716 100644 --- a/instrumentation/hibernate/hibernate-6.0/spring-testing/src/test/groovy/SpringJpaTest.groovy +++ b/instrumentation/hibernate/hibernate-6.0/spring-testing/src/test/groovy/SpringJpaTest.groovy @@ -141,7 +141,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_NAME" "test" "$SemanticAttributes.DB_USER" "sa" "$SemanticAttributes.DB_CONNECTION_STRING" "hsqldb:mem:" - "$SemanticAttributes.DB_STATEMENT" ~/insert into Customer \(.*\) values \(.*, \?, \?\)/ + "$SemanticAttributes.DB_STATEMENT" ~/insert into Customer \(.*\) values \(.*\)/ "$SemanticAttributes.DB_OPERATION" "INSERT" "$SemanticAttributes.DB_SQL_TABLE" "Customer" } @@ -156,7 +156,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_NAME" "test" "$SemanticAttributes.DB_USER" "sa" "$SemanticAttributes.DB_CONNECTION_STRING" "hsqldb:mem:" - "$SemanticAttributes.DB_STATEMENT" ~/insert into Customer \(.*\) values \(.*, \?, \?\)/ + "$SemanticAttributes.DB_STATEMENT" ~/insert into Customer \(.*\) values \(.*\)/ "$SemanticAttributes.DB_OPERATION" "INSERT" "$SemanticAttributes.DB_SQL_TABLE" "Customer" } @@ -232,7 +232,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification { "$SemanticAttributes.DB_NAME" "test" "$SemanticAttributes.DB_USER" "sa" "$SemanticAttributes.DB_CONNECTION_STRING" "hsqldb:mem:" - "$SemanticAttributes.DB_STATEMENT" "update Customer set firstName=?, lastName=? where id=?" + "$SemanticAttributes.DB_STATEMENT" ~/update Customer set firstName=\?,(.*)lastName=\? where id=\?/ "$SemanticAttributes.DB_OPERATION" "UPDATE" "$SemanticAttributes.DB_SQL_TABLE" "Customer" }