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

Use repository interface name in spring data operation name #5352

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -81,7 +81,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
assertTraces(1) {
trace(0, 2) {
span(0) {
name "CrudRepository.findAll"
name "DocRepository.findAll"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -117,7 +117,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
assertTraces(1) {
trace(0, 3) {
span(0) {
name "ElasticsearchRepository.index"
name "DocRepository.index"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -166,7 +166,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
assertTraces(1) {
trace(0, 2) {
span(0) {
name "CrudRepository.findById"
name "DocRepository.findById"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -201,7 +201,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
assertTraces(2) {
trace(0, 3) {
span(0) {
name "ElasticsearchRepository.index"
name "DocRepository.index"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -242,7 +242,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
}
trace(1, 2) {
span(0) {
name "CrudRepository.findById"
name "DocRepository.findById"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -276,7 +276,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
assertTraces(2) {
trace(0, 3) {
span(0) {
name "CrudRepository.deleteById"
name "DocRepository.deleteById"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -317,7 +317,7 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat

trace(1, 2) {
span(0) {
name "CrudRepository.findAll"
name "DocRepository.findAll"
kind INTERNAL
attributes {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.util.ClassAndMethod;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand All @@ -25,7 +26,6 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;
Expand Down Expand Up @@ -82,33 +82,40 @@ public static final class InterceptingRepositoryProxyPostProcessor

@Override
public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
factory.addAdvice(0, RepositoryInterceptor.INSTANCE);
factory.addAdvice(
0, new RepositoryInterceptor(repositoryInformation.getRepositoryInterface()));
}
}

static final class RepositoryInterceptor implements MethodInterceptor {
public static final MethodInterceptor INSTANCE = new RepositoryInterceptor();
private final Class<?> repositoryInterface;

private RepositoryInterceptor() {}
RepositoryInterceptor(Class<?> repositoryInterface) {
this.repositoryInterface = repositoryInterface;
}

@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Context parentContext = currentContext();
Method method = methodInvocation.getMethod();
// Since this interceptor is the outer most interceptor, non-Repository methods
// Since this interceptor is the outermost interceptor, non-Repository methods
// including Object methods will also flow through here. Don't create spans for those.
boolean isRepositoryOp = Repository.class.isAssignableFrom(method.getDeclaringClass());
if (!isRepositoryOp || !instrumenter().shouldStart(parentContext, method)) {
boolean isRepositoryOp =
!Object.class.equals(
method
.getDeclaringClass()); // Repository.class.isAssignableFrom(method.getDeclaringClass());
laurit marked this conversation as resolved.
Show resolved Hide resolved
ClassAndMethod classAndMethod = ClassAndMethod.create(repositoryInterface, method.getName());
if (!isRepositoryOp || !instrumenter().shouldStart(parentContext, classAndMethod)) {
return methodInvocation.proceed();
}

Context context = instrumenter().start(parentContext, method);
Context context = instrumenter().start(parentContext, classAndMethod);
try (Scope ignored = context.makeCurrent()) {
Object result = methodInvocation.proceed();
instrumenter().end(context, method, null, null);
instrumenter().end(context, classAndMethod, null, null);
return result;
} catch (Throwable t) {
instrumenter().end(context, method, null, t);
instrumenter().end(context, classAndMethod, null, t);
throw t;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNames;
import java.lang.reflect.Method;
import io.opentelemetry.instrumentation.api.util.ClassAndMethod;

public final class SpringDataSingletons {

private static final Instrumenter<Method, Void> INSTRUMENTER =
Instrumenter.<Method, Void>builder(
private static final Instrumenter<ClassAndMethod, Void> INSTRUMENTER =
Instrumenter.<ClassAndMethod, Void>builder(
GlobalOpenTelemetry.get(), "io.opentelemetry.spring-data-1.8", SpanNames::fromMethod)
.newInstrumenter();

public static Instrumenter<Method, Void> instrumenter() {
public static Instrumenter<ClassAndMethod, Void> instrumenter() {
return INSTRUMENTER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 2) {
span(0) {
name "JpaRepository.findAll"
name "JpaCustomerRepository.findAll"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -92,7 +92,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 2 + (isHibernate4 ? 0 : 1)) {
span(0) {
name "CrudRepository.save"
name "JpaCustomerRepository.save"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -141,7 +141,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 3) {
span(0) {
name "CrudRepository.save"
name "JpaCustomerRepository.save"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -215,7 +215,7 @@ class SpringJpaTest extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 3) {
span(0) {
name "CrudRepository.delete"
name "JpaCustomerRepository.delete"
kind INTERNAL
attributes {
}
Expand Down Expand Up @@ -251,4 +251,43 @@ class SpringJpaTest extends AgentInstrumentationSpecification {
}
}
}

def "test custom repository method"() {
def context = new AnnotationConfigApplicationContext(JpaPersistenceConfig)
def repo = context.getBean(JpaCustomerRepository)

// when Spring JPA sets up, it issues metadata queries -- clear those traces
clearExportedData()

setup:
def customers = repo.findSpecialCustomers()

expect:
customers.isEmpty()

assertTraces(1) {
trace(0, 2) {
span(0) {
name "JpaCustomerRepository.findSpecialCustomers"
kind INTERNAL
attributes {
}
}
span(1) { // select
name "SELECT test.JpaCustomer"
kind CLIENT
childOf span(0)
attributes {
"$SemanticAttributes.DB_SYSTEM" "hsqldb"
"$SemanticAttributes.DB_NAME" "test"
"$SemanticAttributes.DB_USER" "sa"
"$SemanticAttributes.DB_CONNECTION_STRING" "hsqldb:mem:"
"$SemanticAttributes.DB_STATEMENT" ~/^select /
"$SemanticAttributes.DB_OPERATION" "SELECT"
"$SemanticAttributes.DB_SQL_TABLE" "JpaCustomer"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface JpaCustomerRepository extends JpaRepository<JpaCustomer, Long> {
public interface JpaCustomerRepository
extends JpaRepository<JpaCustomer, Long>, JpaCustomerRepositoryCustom {
List<JpaCustomer> findByLastName(String lastName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package spring.jpa;

import java.util.List;

public interface JpaCustomerRepositoryCustom {
List<JpaCustomer> findSpecialCustomers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package spring.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;

public class JpaCustomerRepositoryImpl implements JpaCustomerRepositoryCustom {
@Autowired private EntityManager entityManager;

@Override
public List<JpaCustomer> findSpecialCustomers() {
return entityManager.createQuery("from JpaCustomer", JpaCustomer.class).getResultList();
}
}