Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Sep 5, 2024
1 parent 1270277 commit 52d92f3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@
*/
package org.openrewrite.java.spring.boot3;

import org.openrewrite.NlsRewrite;
import org.openrewrite.Recipe;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

import java.util.Collections;

import static java.util.Collections.singletonList;

public class MigrateWebMvcTagsToObservationConvention extends Recipe {

private static final String WEBMVCTAGSPROVIDER_FQ = "org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider";
private static final String DEFAULTSERVERREQUESTOBSERVATIONCONVENTION = "DefaultServerRequestObservationConvention";
private static final String DEFAULTSERVERREQUESTOBSERVATIONCONVENTION_FQ = "org.springframework.http.server.observation.DefaultServerRequestObservationConvention";
private static final String KEYVALUES_FQ = "io.micrometer.common.KeyValues";

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Migrate `WebMvcTagsProvider` to `DefaultServerRequestObservationConvention`";
Expand All @@ -28,4 +43,62 @@ public class MigrateWebMvcTagsToObservationConvention extends Recipe {
public @NlsRewrite.Description String getDescription() {
return "Migrate `WebMvcTagsProvider` to `DefaultServerRequestObservationConvention` as part of Spring Boot 3.2 removals.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>(WEBMVCTAGSPROVIDER_FQ, true), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration c = classDecl;
if (classDecl.getImplements() != null) {
for (TypeTree type : classDecl.getImplements()) {
if (TypeUtils.isOfClassType(type.getType(), WEBMVCTAGSPROVIDER_FQ)) {
maybeRemoveImport(WEBMVCTAGSPROVIDER_FQ);
maybeAddImport(DEFAULTSERVERREQUESTOBSERVATIONCONVENTION_FQ);
c = classDecl.withImplements(null)
.withExtends(TypeTree.build(DEFAULTSERVERREQUESTOBSERVATIONCONVENTION)
.withType(JavaType.buildType(DEFAULTSERVERREQUESTOBSERVATIONCONVENTION_FQ))
.withPrefix(Space.SINGLE_SPACE));
c = super.visitClassDeclaration(c, executionContext);
}
}
}
return maybeAutoFormat(classDecl, c, executionContext, getCursor());
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
J.MethodDeclaration m = method;
for (J.Annotation anno : m.getLeadingAnnotations()) {
if (TypeUtils.isOfType(anno.getType(), JavaType.buildType("java.lang.Override"))) {
if (method.getName().getSimpleName().equals("getTags")) {

J.VariableDeclarations methodArg = JavaTemplate.builder("ServerRequestObservationContext context")
.contextSensitive()
.imports("org.springframework.http.server.observation.ServerRequestObservationContext")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(executionContext, "spring-web-6.+"))
.build()
.<J.MethodDeclaration>apply(getCursor(), method.getCoordinates().replaceParameters())
.getParameters().get(0).withPrefix(Space.EMPTY);

Statement keyValuesInitializer = JavaTemplate.builder("KeyValues values = super.getLowCardinalityKeyValues(context);")
.contextSensitive()
.imports(KEYVALUES_FQ)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(executionContext, "micrometer-commons-1.11.+"))
.build()
.<J.MethodDeclaration>apply(getCursor(), method.getBody().getCoordinates().firstStatement())
.getBody().getStatements().get(0);

maybeAddImport(KEYVALUES_FQ);
m = m.withName(m.getName().withSimpleName("getLowCardinalityKeyValues"))
.withReturnTypeExpression(TypeTree.build("KeyValues").withType(JavaType.buildType(KEYVALUES_FQ)))
.withParameters(singletonList(methodArg))
.withBody(m.getBody().withStatements(ListUtils.insert(m.getBody().getStatements(), keyValuesInitializer, 0)));
}
}
}
return super.visitMethodDeclaration(m, executionContext);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse res
import org.springframework.stereotype.Component;
@Component
class ServerRequestObservationConvention extends DefaultServerRequestObservationConvention {
class CustomWebMvcTagsProvider extends DefaultServerRequestObservationConvention {
@Override
public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext context) {
Expand All @@ -81,7 +81,7 @@ public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext cont
values.and(KeyValue.of("custom.header", customHeader));
}
return super.getLowCardinalityKeyValues(context).and(values);
return values;
}
}
"""));
Expand Down

0 comments on commit 52d92f3

Please sign in to comment.