Skip to content

Commit

Permalink
NH-93561: add test for hibernate v4 and update v6 test
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverchuk committed Oct 28, 2024
1 parent 076e015 commit 590f48a
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 55 deletions.
3 changes: 3 additions & 0 deletions gradle/instrumentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class JavaagentProvider implements CommandLineArgumentProvider {
"-Dsw.apm.sql.tag.prepared=true",
// prevent sporadic gradle deadlocks, see SafeLogger for more details
"-Dotel.javaagent.testing.transform-safe-logging.enabled=true",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.lang.invoke=ALL-UNNAMED",
"-XX:+IgnoreUnrecognizedVMOptions",
"-Dotel.javaagent.debug=true"]
}
}
5 changes: 5 additions & 0 deletions instrumentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ subprojects {
if (subProj.getPlugins().hasPlugin('java')) {
// Make it so all instrumentation subproject tests can be run with a single command.
instr_project.tasks.test.dependsOn(subProj.tasks.test)
subProj.tasks.test.configure {
testLogging {
events "passed", "skipped", "failed"
}
}

instr_project.dependencies {
implementation(project(subProj.getPath()))
Expand Down
3 changes: 3 additions & 0 deletions instrumentation/hibernate/hibernate-4.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ dependencies {
compileOnly("org.hibernate:hibernate-core:4.0.0.Final")
compileOnly "io.opentelemetry:opentelemetry-sdk-trace:${versions.opentelemetry}"
compileOnly "io.opentelemetry.semconv:opentelemetry-semconv:${versions.opentelemetrySemconv}"

testImplementation("org.hibernate:hibernate-core:4.0.0.Final")
testImplementation("com.h2database:h2:2.3.232")
}

compileJava {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.solarwinds.opentelemetry.instrumentation.hibernate.v4_0;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;

@Entity(name = "devs")
public class Dev {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.solarwinds.opentelemetry.instrumentation.hibernate.v4_0;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class LoaderInstrumentationTest {
@RegisterExtension
public static AgentInstrumentationExtension testing = AgentInstrumentationExtension.create();

private SessionFactory sessionFactory;

@BeforeEach
public void setUp() {
Configuration configuration = new Configuration();
configuration
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.setProperty("hibernate.connection.driver_class", "org.h2.Driver")
.setProperty("hibernate.connection.url", "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "create")
.setProperty("hibernate.show_sql", "true");

sessionFactory = configuration.addAnnotatedClass(Dev.class).buildSessionFactory();

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

Dev dev = new Dev();
dev.setName("cleverchuk");

session.persist(dev);
transaction.commit();
session.close();
}

@AfterEach
public void tearDown() {
sessionFactory.close();
}

@Test
void verifyDbContextInjectionSpanIsCreated() {
testing.runWithSpan(
"root",
() -> {
Session session = sessionFactory.openSession();
session.createQuery("SELECT name FROM devs").list();
session.close();
});
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("root").hasKind(SpanKind.INTERNAL),
span -> span.hasName("sw.hibernate.context").hasKind(SpanKind.INTERNAL)));
}
}
3 changes: 3 additions & 0 deletions instrumentation/hibernate/hibernate-6.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ dependencies {
implementation project(":instrumentation:instrumentation-shared")

compileOnly "org.hibernate:hibernate-core:6.0.0.Final"
compileOnly "com.solarwinds.joboe:logging:${versions.joboe}"

compileOnly "io.opentelemetry:opentelemetry-sdk-trace:${versions.opentelemetry}"
compileOnly "io.opentelemetry.semconv:opentelemetry-semconv:${versions.opentelemetrySemconv}"

testImplementation "org.hibernate:hibernate-core:6.0.0.Final"
testImplementation("com.h2database:h2:2.3.232")
}

compileJava {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.solarwinds.joboe.logging.LoggerFactory;
import com.solarwinds.opentelemetry.instrumentation.hibernate.FunctionWrapper;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
Expand Down Expand Up @@ -100,8 +101,8 @@ public static void startMethod(
scope = context.makeCurrent();
swoSql = sql;

} catch (Throwable ignore) {
// ignore because we can't do jack here
} catch (Throwable throwable) {
LoggerFactory.getLogger().error("[Hibernate - v6] Error injecting context", throwable);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.solarwinds.opentelemetry.instrumentation.hibernate.v6_0;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.annotations.GenericGenerator;

@Entity(name = "devs")
public class Dev {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Loading

0 comments on commit 590f48a

Please sign in to comment.