From 4125d8c9adbfc125aa8a36696489e595a3bce4d4 Mon Sep 17 00:00:00 2001
From: Emre Dincturk <74370953+mrdnctrk@users.noreply.github.com>
Date: Fri, 19 Apr 2024 17:43:19 -0400
Subject: [PATCH] added otel span for interceptors (#5853)
---
hapi-fhir-base/pom.xml | 5 +++++
.../executor/BaseInterceptorService.java | 17 ++++++++++++++++-
...-opentelemetry-spans-for-interceptors.yaml | 6 ++++++
pom.xml | 19 +++++++++++++++++++
4 files changed, 46 insertions(+), 1 deletion(-)
create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5855-opentelemetry-spans-for-interceptors.yaml
diff --git a/hapi-fhir-base/pom.xml b/hapi-fhir-base/pom.xml
index 1db4476b4d3..c5ec6eee1f1 100644
--- a/hapi-fhir-base/pom.xml
+++ b/hapi-fhir-base/pom.xml
@@ -138,6 +138,11 @@
system-stubs-jupiter
test
+
+
+ io.opentelemetry
+ opentelemetry-api
+
diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/executor/BaseInterceptorService.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/executor/BaseInterceptorService.java
index d0ba36e44c3..22a196850ea 100644
--- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/executor/BaseInterceptorService.java
+++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/executor/BaseInterceptorService.java
@@ -31,6 +31,8 @@ import ca.uhn.fhir.util.ReflectionUtil;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.instrumentation.annotations.WithSpan;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.apache.commons.lang3.Validate;
@@ -547,7 +549,7 @@ public abstract class BaseInterceptorService & I
// Invoke the method
try {
- return myMethod.invoke(getInterceptor(), args);
+ return invokeMethod(args);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if (myPointcut.isShouldLogAndSwallowException(targetException)) {
@@ -566,6 +568,19 @@ public abstract class BaseInterceptorService & I
throw new InternalErrorException(Msg.code(1911) + e);
}
}
+
+ @WithSpan("hapifhir.interceptor")
+ private Object invokeMethod(Object[] args) throws InvocationTargetException, IllegalAccessException {
+ // Add attributes to the opentelemetry span
+ Span currentSpan = Span.current();
+ currentSpan.setAttribute("hapifhir.interceptor.pointcut_name", myPointcut.name());
+ currentSpan.setAttribute(
+ "hapifhir.interceptor.class_name",
+ myMethod.getDeclaringClass().getName());
+ currentSpan.setAttribute("hapifhir.interceptor.method_name", myMethod.getName());
+
+ return myMethod.invoke(getInterceptor(), args);
+ }
}
protected class HookDescriptor {
diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5855-opentelemetry-spans-for-interceptors.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5855-opentelemetry-spans-for-interceptors.yaml
new file mode 100644
index 00000000000..52e71439b05
--- /dev/null
+++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5855-opentelemetry-spans-for-interceptors.yaml
@@ -0,0 +1,6 @@
+---
+type: add
+issue: 5855
+title: "If using an OpenTelemetry agent, a span is now generated for each interceptor method call. The span is named
+as 'hapifhir.interceptor' and it has the following attributes about the interceptor:
+'hapifhir.interceptor.pointcut_name','hapifhir.interceptor.class_name', 'hapifhir.interceptor.method_name'"
diff --git a/pom.xml b/pom.xml
index c6ef616a573..0ffabdf9385 100644
--- a/pom.xml
+++ b/pom.xml
@@ -165,6 +165,11 @@
mockito-junit-jupiter
test
+
+
+ io.opentelemetry.instrumentation
+ opentelemetry-instrumentation-annotations
+
@@ -977,6 +982,7 @@
3.3.0
1.8
4.10.0
+ 1.32.0
4.1.2
1.4
6.2.5.Final
@@ -2264,6 +2270,19 @@
provided
true
+
+
+ io.opentelemetry
+ opentelemetry-bom
+ ${otel.version}
+ pom
+ import
+
+
+ io.opentelemetry.instrumentation
+ opentelemetry-instrumentation-annotations
+ ${otel.version}
+