added otel span for interceptors (#5853)

This commit is contained in:
Emre Dincturk 2024-04-19 17:43:19 -04:00 committed by GitHub
parent 54d4b0fefe
commit 4125d8c9ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 1 deletions

View File

@ -138,6 +138,11 @@
<artifactId>system-stubs-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- OpenTelemetry -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -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<POINTCUT extends Enum<POINTCUT> & 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<POINTCUT extends Enum<POINTCUT> & 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 {

View File

@ -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'"

19
pom.xml
View File

@ -165,6 +165,11 @@
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- OpenTelemetry Annotations-->
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
</dependency>
</dependencies>
<developers>
@ -977,6 +982,7 @@
<maven_assembly_plugin_version>3.3.0</maven_assembly_plugin_version>
<maven_license_plugin_version>1.8</maven_license_plugin_version>
<okhttp_version>4.10.0</okhttp_version>
<otel.version>1.32.0</otel.version> <!-- BOM Version -->
<poi_version>4.1.2</poi_version>
<poi_ooxml_schemas_version>1.4</poi_ooxml_schemas_version>
<resteasy_version>6.2.5.Final</resteasy_version>
@ -2264,6 +2270,19 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- OpenTelemetry -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>${otel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
<version>${otel.version}</version>
</dependency>
</dependencies>
</dependencyManagement>