From ed7b9fc6b6af93e673de7f0e887ee6d99f1ae7cb Mon Sep 17 00:00:00 2001 From: Gary Graham Date: Fri, 28 Feb 2020 16:09:16 -0500 Subject: [PATCH 1/2] Initial attempt at integrating elastic APM for search performance --- hapi-fhir-jpaserver-base/pom.xml | 5 +++++ .../fhir/jpa/search/SearchCoordinatorSvcImpl.java | 13 +++++++++---- pom.xml | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-jpaserver-base/pom.xml b/hapi-fhir-jpaserver-base/pom.xml index 00fd60b7ff5..f085f2e4d45 100644 --- a/hapi-fhir-jpaserver-base/pom.xml +++ b/hapi-fhir-jpaserver-base/pom.xml @@ -48,6 +48,11 @@ commons-csv + + co.elastic.apm + apm-agent-api + + ca.uhn.hapi.fhir hapi-fhir-base diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java index 49ef70e13b6..8965e732cbd 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java @@ -56,6 +56,9 @@ import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; import ca.uhn.fhir.rest.server.util.ICachedSearchDetails; import ca.uhn.fhir.util.AsyncUtil; import ca.uhn.fhir.util.StopWatch; +import co.elastic.apm.api.ElasticApm; +import co.elastic.apm.api.Span; +import co.elastic.apm.api.Transaction; import com.google.common.annotations.VisibleForTesting; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -601,7 +604,7 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { private List myPreviouslyAddedResourcePids; private Integer myMaxResultsToFetch; private SearchRuntimeDetails mySearchRuntimeDetails; - + private Transaction myParentTransaction; /** * Constructor */ @@ -614,6 +617,7 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { mySearchRuntimeDetails = new SearchRuntimeDetails(theRequest, mySearch.getUuid()); mySearchRuntimeDetails.setQueryString(theParams.toNormalizedQueryString(theCallingDao.getContext())); myRequest = theRequest; + myParentTransaction = ElasticApm.currentTransaction(); } /** @@ -841,7 +845,8 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { @Override public Void call() { StopWatch sw = new StopWatch(); - + Span span = myParentTransaction.startSpan("db", "H2", "search"); + span.setName("search2!"); try { // Create an initial search in the DB and give it an ID saveSearch(); @@ -897,7 +902,6 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { ourLog.error("Failed during search loading after {}ms", sw.getMillis(), t); } myUnsyncedPids.clear(); - Throwable rootCause = ExceptionUtils.getRootCause(t); rootCause = defaultIfNull(rootCause, t); @@ -924,12 +928,13 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { JpaInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, myRequest, Pointcut.JPA_PERFTRACE_SEARCH_FAILED, params); saveSearch(); - + span.captureException(t); } finally { myIdToSearchTask.remove(mySearch.getUuid()); myInitialCollectionLatch.countDown(); markComplete(); + span.end(); } return null; diff --git a/pom.xml b/pom.xml index ef8d1092440..8522fbdeed9 100644 --- a/pom.xml +++ b/pom.xml @@ -679,6 +679,7 @@ UTF-8 1.0.1 + 1.13.0 5.4.1 @@ -1036,6 +1037,11 @@ httpcore ${httpcore_version} + + co.elastic.apm + apm-agent-api + ${elastic_apm_version} + org.apache.jena apache-jena-libs From 8d9119faef6e3cef7ad5828ad34f66ad15121f88 Mon Sep 17 00:00:00 2001 From: Tadgh Date: Mon, 2 Mar 2020 14:37:09 -0800 Subject: [PATCH 2/2] Rework span name --- .../java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java index 8965e732cbd..7125a415277 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java @@ -845,8 +845,8 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { @Override public Void call() { StopWatch sw = new StopWatch(); - Span span = myParentTransaction.startSpan("db", "H2", "search"); - span.setName("search2!"); + Span span = myParentTransaction.startSpan("db", "query", "search"); + span.setName("FHIR Database Search"); try { // Create an initial search in the DB and give it an ID saveSearch();