Skip to content

Commit

Permalink
adding a test case for MigrateWebMvcTagsToObservationConvention for t… (
Browse files Browse the repository at this point in the history
#607)

* adding a test case for MigrateWebMvcTagsToObservationConvention for the case where the iterable passed to `Tags.and(...)` is *not* an identifier, and partially fixing it, BUT this commit still has a strange test failure with cyclically duplicating http request/response variables

* adjusting precondition of MigrateWebMvcTagsToObservationConvention per @Laurens-W suggestion
  • Loading branch information
nmck257 authored Oct 18, 2024
1 parent cea3ccf commit d0bc9dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.search.FindImplementations;
import org.openrewrite.java.tree.*;

import java.util.ArrayList;
Expand Down Expand Up @@ -68,7 +68,7 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>(WEBMVCTAGSPROVIDER_FQ, true), new JavaVisitor<ExecutionContext>() {
return Preconditions.check(new FindImplementations(WEBMVCTAGSPROVIDER_FQ), new JavaVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.MethodDeclaration getTagsMethod = null;
Expand Down Expand Up @@ -251,7 +251,7 @@ private Statement refactorTagsUsage(ExecutionContext ctx, org.openrewrite.java.t
}
return getMultiKeyValueStatement(ctx, coords, args, returnIdentifier);
} else if (TAGS_AND_TAG_ITERABLE.matches(init) || TAGS_OF_TAG_ITERABLE.matches(init)) {
J.Identifier iterable = (J.Identifier) init.getArguments().get(0);
Expression iterable = init.getArguments().get(0);
String template = "for (Tag tag : #{any()}) {\n" +
" #{any()}.and(KeyValue.of(tag.getKey(), tag.getValue()));\n" +
"}\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,13 @@ public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse res
tags = tags.and("a", "b", "c", "d");
tags = Tags.and(Tag.of("a", "b"), staticTag);
tags = tags.and(staticTags);
tags = tags.and(methodTags());
return tags;
}
private Tags methodTags() {
return staticTags;
}
}
""",
"""
Expand Down Expand Up @@ -219,8 +224,15 @@ public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext con
for (Tag tag : staticTags) {
values.and(KeyValue.of(tag.getKey(), tag.getValue()));
}
for (Tag tag : methodTags()) {
values.and(KeyValue.of(tag.getKey(), tag.getValue()));
}
return values;
}
private Tags methodTags() {
return staticTags;
}
}
"""
)
Expand Down

0 comments on commit d0bc9dc

Please sign in to comment.