Skip to content

Commit 07108d8

Browse files
authored
multiple span per java method (#212)
1 parent f00f82a commit 07108d8

File tree

5 files changed

+48
-33
lines changed

5 files changed

+48
-33
lines changed

java/src/main/java/org/digma/intellij/plugin/idea/psi/java/Constants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public interface Constants {
55
String OPENTELEMETY_FQN = "io.opentelemetry.api.OpenTelemetry";
66
String TRACER_BUILDER_FQN = "io.opentelemetry.api.trace.TracerBuilder";
77
String WITH_SPAN_FQN = "io.opentelemetry.instrumentation.annotations.WithSpan";
8-
String WITH_SPAN_INST_LIBRARY = "io.opentelemetry.spring-boot-autoconfigure";
8+
String WITH_SPAN_INST_LIBRARY_1 = "io.opentelemetry.spring-boot-autoconfigure";
9+
String WITH_SPAN_INST_LIBRARY_2 = "io.opentelemetry.opentelemetry-instrumentation-annotations-1.16";
10+
String WITH_SPAN_INST_LIBRARY_3 = "io.quarkus.opentelemetry";
911
String SPAN_BUILDER_FQN = "io.opentelemetry.api.trace.SpanBuilder";
1012
String TRACER_FQN = "io.opentelemetry.api.trace.Tracer";
1113
}

java/src/main/java/org/digma/intellij/plugin/idea/psi/java/JavaLanguageService.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
import static org.digma.intellij.plugin.idea.psi.java.Constants.SPAN_BUILDER_FQN;
4242
import static org.digma.intellij.plugin.idea.psi.java.JavaLanguageUtils.createJavaMethodCodeObjectId;
43-
import static org.digma.intellij.plugin.idea.psi.java.SpanDiscoveryUtils.filterNonRelevantMethodsForSpanDiscovery;
44-
import static org.digma.intellij.plugin.idea.psi.java.SpanDiscoveryUtils.filterNonRelevantReferencesForSpanDiscovery;
43+
import static org.digma.intellij.plugin.idea.psi.java.JavaSpanDiscoveryUtils.filterNonRelevantMethodsForSpanDiscovery;
44+
import static org.digma.intellij.plugin.idea.psi.java.JavaSpanDiscoveryUtils.filterNonRelevantReferencesForSpanDiscovery;
4545

4646
@SuppressWarnings("UnstableApiUsage")
4747
public class JavaLanguageService implements LanguageService {
@@ -440,7 +440,7 @@ private void startSpanMethodCallSpanDiscovery(@NotNull PsiFile psiFile, @NotNull
440440
startSpanReferences = filterNonRelevantReferencesForSpanDiscovery(startSpanReferences);
441441

442442
startSpanReferences.forEach(psiReference -> {
443-
SpanInfo spanInfo = SpanDiscoveryUtils.getSpanInfoFromStartSpanMethodReference(project, psiReference);
443+
SpanInfo spanInfo = JavaSpanDiscoveryUtils.getSpanInfoFromStartSpanMethodReference(project, psiReference);
444444
if (spanInfo != null) {
445445
Log.log(LOGGER::debug, "Found span info {} for method {}",spanInfo.getId(),spanInfo.getContainingMethod());
446446
MethodInfo methodInfo = documentInfo.getMethods().get(spanInfo.getContainingMethod());
@@ -460,13 +460,15 @@ private void withSpanAnnotationSpanDiscovery(@NotNull PsiFile psiFile, @NotNull
460460
Query<PsiMethod> psiMethods = AnnotatedElementsSearch.searchPsiMethods(withSpanClass, GlobalSearchScope.fileScope(psiFile));
461461
psiMethods = filterNonRelevantMethodsForSpanDiscovery(psiMethods);
462462
psiMethods.forEach(psiMethod -> {
463-
SpanInfo spanInfo = SpanDiscoveryUtils.getSpanInfoFromWithSpanAnnotatedMethod(psiMethod);
464-
if (spanInfo != null) {
465-
Log.log(LOGGER::debug, "Found span info {} for method {}",spanInfo.getId(),spanInfo.getContainingMethod());
466-
MethodInfo methodInfo = documentInfo.getMethods().get(spanInfo.getContainingMethod());
467-
//this method must exist in the document info
468-
Objects.requireNonNull(methodInfo, "method info " + spanInfo.getContainingMethod() + " must exist in DocumentInfo for " + documentInfo.getFileUri());
469-
methodInfo.getSpans().add(spanInfo);
463+
List<SpanInfo> spanInfos = JavaSpanDiscoveryUtils.getSpanInfoFromWithSpanAnnotatedMethod(psiMethod);
464+
if (spanInfos != null) {
465+
spanInfos.forEach(spanInfo -> {
466+
Log.log(LOGGER::debug, "Found span info {} for method {}",spanInfo.getId(),spanInfo.getContainingMethod());
467+
MethodInfo methodInfo = documentInfo.getMethods().get(spanInfo.getContainingMethod());
468+
//this method must exist in the document info
469+
Objects.requireNonNull(methodInfo, "method info " + spanInfo.getContainingMethod() + " must exist in DocumentInfo for " + documentInfo.getFileUri());
470+
methodInfo.getSpans().add(spanInfo);
471+
});
470472
}
471473
});
472474
}

java/src/main/java/org/digma/intellij/plugin/idea/psi/java/JavaLanguageUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import java.util.function.Predicate;
1616

17-
import static org.digma.intellij.plugin.idea.psi.java.Constants.WITH_SPAN_INST_LIBRARY;
1817

1918
/**
2019
* A collection of reusable utility methods for the java language, mostly PSI related.
@@ -63,9 +62,9 @@ public static String createSpanNameForWithSpanAnnotation(@NotNull PsiMethod psiM
6362

6463

6564
@NotNull
66-
public static String createSpanIdForWithSpanAnnotation(@NotNull PsiMethod psiMethod, @NotNull PsiAnnotation withSpanAnnotation, @NotNull PsiClass containingClass) {
65+
public static String createSpanIdForWithSpanAnnotation(@NotNull PsiMethod psiMethod, @NotNull PsiAnnotation withSpanAnnotation, @NotNull PsiClass containingClass, @NotNull String instLibrary) {
6766
var spanName = createSpanNameForWithSpanAnnotation(psiMethod, withSpanAnnotation, containingClass);
68-
return WITH_SPAN_INST_LIBRARY + "$_$" + spanName;
67+
return instLibrary + "$_$" + spanName;
6968
}
7069

7170
@NotNull

java/src/main/java/org/digma/intellij/plugin/idea/psi/java/SpanDiscoveryUtils.java renamed to java/src/main/java/org/digma/intellij/plugin/idea/psi/java/JavaSpanDiscoveryUtils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.jetbrains.annotations.NotNull;
1313
import org.jetbrains.annotations.Nullable;
1414

15+
import java.util.ArrayList;
16+
import java.util.List;
1517
import java.util.Objects;
1618

1719
import static org.digma.intellij.plugin.idea.psi.java.Constants.*;
@@ -22,8 +24,8 @@
2224
* all of those methods must be called in the scope of a ReadAction.
2325
*/
2426
@SuppressWarnings("UnstableApiUsage")
25-
public class SpanDiscoveryUtils {
26-
private SpanDiscoveryUtils() {
27+
public class JavaSpanDiscoveryUtils {
28+
private JavaSpanDiscoveryUtils() {
2729
}
2830

2931
/*
@@ -39,7 +41,7 @@ private SpanDiscoveryUtils() {
3941
* will return null.
4042
*/
4143
@Nullable
42-
public static SpanInfo getSpanInfoFromWithSpanAnnotatedMethod(@NotNull PsiMethod psiMethod) {
44+
public static List<SpanInfo> getSpanInfoFromWithSpanAnnotatedMethod(@NotNull PsiMethod psiMethod) {
4345

4446
var withSpanAnnotation = psiMethod.getAnnotation(Constants.WITH_SPAN_FQN);
4547
var containingClass = psiMethod.getContainingClass();
@@ -56,9 +58,12 @@ public static SpanInfo getSpanInfoFromWithSpanAnnotatedMethod(@NotNull PsiMethod
5658
Objects.requireNonNull(containingFileUri, "containingFileUri must not be null here");
5759

5860
var spanName = createSpanNameForWithSpanAnnotation(psiMethod, withSpanAnnotation, containingClass);
59-
var spanId = createSpanIdForWithSpanAnnotation(psiMethod, withSpanAnnotation, containingClass);
6061

61-
return new SpanInfo(spanId, spanName, methodId, containingFileUri);
62+
List<SpanInfo> spanInfos = new ArrayList<>();
63+
spanInfos.add(new SpanInfo(createSpanIdForWithSpanAnnotation(psiMethod, withSpanAnnotation, containingClass,WITH_SPAN_INST_LIBRARY_1), spanName, methodId, containingFileUri));
64+
spanInfos.add(new SpanInfo(createSpanIdForWithSpanAnnotation(psiMethod, withSpanAnnotation, containingClass,WITH_SPAN_INST_LIBRARY_2), spanName, methodId, containingFileUri));
65+
spanInfos.add(new SpanInfo(createSpanIdForWithSpanAnnotation(psiMethod, withSpanAnnotation, containingClass,WITH_SPAN_INST_LIBRARY_3), spanName, methodId, containingFileUri));
66+
return spanInfos;
6267
}
6368

6469
//if here then we couldn't completely discover the span

java/src/main/java/org/digma/intellij/plugin/idea/psi/java/JavaSpanNavigationProvider.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import java.util.stream.Collectors;
3131

3232
import static org.digma.intellij.plugin.idea.psi.java.Constants.SPAN_BUILDER_FQN;
33-
import static org.digma.intellij.plugin.idea.psi.java.SpanDiscoveryUtils.filterNonRelevantMethodsForSpanDiscovery;
34-
import static org.digma.intellij.plugin.idea.psi.java.SpanDiscoveryUtils.filterNonRelevantReferencesForSpanDiscovery;
33+
import static org.digma.intellij.plugin.idea.psi.java.JavaSpanDiscoveryUtils.filterNonRelevantMethodsForSpanDiscovery;
34+
import static org.digma.intellij.plugin.idea.psi.java.JavaSpanDiscoveryUtils.filterNonRelevantReferencesForSpanDiscovery;
3535

3636
@SuppressWarnings("UnstableApiUsage")
3737
public class JavaSpanNavigationProvider implements Disposable {
@@ -111,7 +111,7 @@ private void buildStartSpanMethodCall() {
111111
startSpanReferences = filterNonRelevantReferencesForSpanDiscovery(startSpanReferences);
112112

113113
startSpanReferences.forEach(psiReference -> {
114-
SpanInfo spanInfo = SpanDiscoveryUtils.getSpanInfoFromStartSpanMethodReference(project, psiReference);
114+
SpanInfo spanInfo = JavaSpanDiscoveryUtils.getSpanInfoFromStartSpanMethodReference(project, psiReference);
115115
if (spanInfo != null) {
116116
int lineNumber = psiReference.getElement().getTextOffset();
117117
var location = new SpanLocation(spanInfo.getContainingFileUri(), lineNumber);
@@ -129,11 +129,14 @@ private void buildWithSpanAnnotation() {
129129
Query<PsiMethod> psiMethods = AnnotatedElementsSearch.searchPsiMethods(withSpanClass, GlobalSearchScope.projectScope(project));
130130
psiMethods = filterNonRelevantMethodsForSpanDiscovery(psiMethods);
131131
psiMethods.forEach(psiMethod -> {
132-
SpanInfo spanInfo = SpanDiscoveryUtils.getSpanInfoFromWithSpanAnnotatedMethod(psiMethod);
133-
if (spanInfo != null) {
134-
int offset = psiMethod.getTextOffset();
135-
var location = new SpanLocation(spanInfo.getContainingFileUri(), offset);
136-
spanLocations.put(spanInfo.getId(), location);
132+
List<SpanInfo> spanInfos = JavaSpanDiscoveryUtils.getSpanInfoFromWithSpanAnnotatedMethod(psiMethod);
133+
if (spanInfos != null) {
134+
spanInfos.forEach(spanInfo -> {
135+
Log.log(LOGGER::debug, "Found span info {} for method {}",spanInfo.getId(),spanInfo.getContainingMethod());
136+
int offset = psiMethod.getTextOffset();
137+
var location = new SpanLocation(spanInfo.getContainingFileUri(), offset);
138+
spanLocations.put(spanInfo.getId(), location);
139+
});
137140
}
138141
});
139142
}
@@ -209,8 +212,9 @@ private void buildStartSpanMethodCall(VirtualFile virtualFile) {
209212
startSpanReferences = filterNonRelevantReferencesForSpanDiscovery(startSpanReferences);
210213

211214
startSpanReferences.forEach(psiReference -> {
212-
SpanInfo spanInfo = SpanDiscoveryUtils.getSpanInfoFromStartSpanMethodReference(project, psiReference);
215+
SpanInfo spanInfo = JavaSpanDiscoveryUtils.getSpanInfoFromStartSpanMethodReference(project, psiReference);
213216
if (spanInfo != null) {
217+
Log.log(LOGGER::debug, "Found span info {} in method {}",spanInfo.getId(),spanInfo.getContainingMethod());
214218
int lineNumber = psiReference.getElement().getTextOffset();
215219
var location = new SpanLocation(spanInfo.getContainingFileUri(), lineNumber);
216220
spanLocations.put(spanInfo.getId(), location);
@@ -228,11 +232,14 @@ private void buildWithSpanAnnotation(VirtualFile virtualFile) {
228232
Query<PsiMethod> psiMethods = AnnotatedElementsSearch.searchPsiMethods(withSpanClass, GlobalSearchScope.fileScope(project, virtualFile));
229233
psiMethods = filterNonRelevantMethodsForSpanDiscovery(psiMethods);
230234
psiMethods.forEach(psiMethod -> {
231-
SpanInfo spanInfo = SpanDiscoveryUtils.getSpanInfoFromWithSpanAnnotatedMethod(psiMethod);
232-
if (spanInfo != null) {
233-
int offset = psiMethod.getTextOffset();
234-
var location = new SpanLocation(spanInfo.getContainingFileUri(), offset);
235-
spanLocations.put(spanInfo.getId(), location);
235+
List<SpanInfo> spanInfos = JavaSpanDiscoveryUtils.getSpanInfoFromWithSpanAnnotatedMethod(psiMethod);
236+
if (spanInfos != null) {
237+
spanInfos.forEach(spanInfo -> {
238+
Log.log(LOGGER::debug, "Found span info {} for method {}",spanInfo.getId(),spanInfo.getContainingMethod());
239+
int offset = psiMethod.getTextOffset();
240+
var location = new SpanLocation(spanInfo.getContainingFileUri(), offset);
241+
spanLocations.put(spanInfo.getId(), location);
242+
});
236243
}
237244
});
238245
}

0 commit comments

Comments
 (0)