-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial test case * WIP * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * WIP * Apply suggestions from code review * WIP * WIP * WIP * WIP * WIP * Suggestions * WIP * Working setup * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Format and apply suggestion * More progress * Naming and imports * Adopt classpathFromResources & add tomcat-embedded-core * Fix most warnings and issues * Fix type issues and warnings * More edge cases * More edge cases, extra test coverage * Remove jetbrains notnull * Reduce document example test case and add additional test cases for edge cases * Apply review feedback * Apply formatter to tests * Reduce imports and classpath Explicitly add micrometer-observation jar * Use method matcher * By default use high cardinality keys as low might cause issues with ingesting metrics/traces * Consistently use the same version suffix for classpath resources * Demonstrate failure on any empty method * Fix class cast issue --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0d5e2d8
commit 9d3a3f0
Showing
8 changed files
with
618 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
296 changes: 296 additions & 0 deletions
296
...main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+389 KB
src/main/resources/META-INF/rewrite/classpath/jakarta.servlet-api-6.1.0.jar
Binary file not shown.
Binary file added
BIN
+46.4 KB
src/main/resources/META-INF/rewrite/classpath/micrometer-commons-1.11.12.jar
Binary file not shown.
Binary file added
BIN
+851 KB
src/main/resources/META-INF/rewrite/classpath/micrometer-core-1.11.12.jar
Binary file not shown.
Binary file added
BIN
+69.7 KB
src/main/resources/META-INF/rewrite/classpath/micrometer-observation-1.11.12.jar
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
314 changes: 314 additions & 0 deletions
314
.../java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,314 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.spring.boot3; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class MigrateWebMvcTagsToObservationConventionTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new MigrateWebMvcTagsToObservationConvention()) | ||
.parser(JavaParser.fromJavaVersion().classpath( | ||
"micrometer-core", | ||
"spring-boot", | ||
"spring-context", | ||
"spring-beans", | ||
"spring-web", | ||
"jakarta.servlet-api")); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void shouldMigrateWebMvcTagsProviderToDefaultServerRequestObservationConvention() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTags; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider; | ||
class CustomWebMvcTagsProvider implements WebMvcTagsProvider { | ||
@Override | ||
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) { | ||
Tags tags = Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response), WebMvcTags.status(response), WebMvcTags.outcome(response)); | ||
String customHeader = request.getHeader("X-Custom-Header"); | ||
if (customHeader != null) { | ||
tags = tags.and("custom.header", customHeader); | ||
} | ||
return tags; | ||
} | ||
} | ||
""", | ||
""" | ||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; | ||
import org.springframework.http.server.observation.ServerRequestObservationContext; | ||
class CustomWebMvcTagsProvider extends DefaultServerRequestObservationConvention { | ||
@Override | ||
public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext context) { | ||
HttpServletRequest request = context.getCarrier(); | ||
KeyValues values = super.getHighCardinalityKeyValues(context); | ||
String customHeader = request.getHeader("X-Custom-Header"); | ||
if (customHeader != null) { | ||
values.and(KeyValue.of("custom.header", customHeader)); | ||
} | ||
return values; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void shouldMigrateTags_Of() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTags; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider; | ||
class CustomWebMvcTagsProvider implements WebMvcTagsProvider { | ||
Tags staticTags = Tags.of("a", "b", "c", "d"); | ||
Tag staticTag = Tag.of("a", "b"); | ||
@Override | ||
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) { | ||
Tags tags = Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response), WebMvcTags.status(response), WebMvcTags.outcome(response)); | ||
tags = Tags.of("a", "b"); | ||
tags = Tags.of("a", "b", "c", "d"); | ||
tags = Tags.of(Tag.of("a", "b"), staticTag); | ||
tags = Tags.of(staticTags); | ||
return tags; | ||
} | ||
} | ||
""", | ||
""" | ||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; | ||
import org.springframework.http.server.observation.ServerRequestObservationContext; | ||
class CustomWebMvcTagsProvider extends DefaultServerRequestObservationConvention { | ||
Tags staticTags = Tags.of("a", "b", "c", "d"); | ||
Tag staticTag = Tag.of("a", "b"); | ||
@Override | ||
public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext context) { | ||
KeyValues values = super.getHighCardinalityKeyValues(context); | ||
values.and(KeyValue.of("a", "b")); | ||
values.and(KeyValue.of("a", "b"), KeyValue.of("c", "d")); | ||
values.and(KeyValue.of("a", "b"), KeyValue.of(staticTag.getKey(), staticTag.getValue())); | ||
for (Tag tag : staticTags) { | ||
values.and(KeyValue.of(tag.getKey(), tag.getValue())); | ||
} | ||
return values; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void shouldMigrateTags_And() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTags; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider; | ||
class CustomWebMvcTagsProvider implements WebMvcTagsProvider { | ||
Tags staticTags = Tags.of("a", "b", "c", "d"); | ||
Tag staticTag = Tag.of("a", "b"); | ||
@Override | ||
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) { | ||
Tags tags = Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response), WebMvcTags.status(response), WebMvcTags.outcome(response)); | ||
String customHeader = request.getHeader("X-Custom-Header"); | ||
if (customHeader != null) { | ||
tags = tags.and("custom.header", customHeader); | ||
} | ||
if (response.getStatus() >= 400) { | ||
tags = tags.and("error", "true"); | ||
} | ||
tags = tags.and("a", "b", "c", "d"); | ||
tags = Tags.and(Tag.of("a", "b"), staticTag); | ||
tags = tags.and(staticTags); | ||
return tags; | ||
} | ||
} | ||
""", | ||
""" | ||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; | ||
import org.springframework.http.server.observation.ServerRequestObservationContext; | ||
class CustomWebMvcTagsProvider extends DefaultServerRequestObservationConvention { | ||
Tags staticTags = Tags.of("a", "b", "c", "d"); | ||
Tag staticTag = Tag.of("a", "b"); | ||
@Override | ||
public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext context) { | ||
HttpServletRequest request = context.getCarrier(); | ||
HttpServletResponse response = context.getResponse(); | ||
KeyValues values = super.getHighCardinalityKeyValues(context); | ||
String customHeader = request.getHeader("X-Custom-Header"); | ||
if (customHeader != null) { | ||
values.and(KeyValue.of("custom.header", customHeader)); | ||
} | ||
if (response.getStatus() >= 400) { | ||
values.and(KeyValue.of("error", "true")); | ||
} | ||
values.and(KeyValue.of("a", "b"), KeyValue.of("c", "d")); | ||
values.and(KeyValue.of("a", "b"), KeyValue.of(staticTag.getKey(), staticTag.getValue())); | ||
for (Tag tag : staticTags) { | ||
values.and(KeyValue.of(tag.getKey(), tag.getValue())); | ||
} | ||
return values; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void shouldMigrateReturnTags_Of() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider; | ||
class CustomWebMvcTagsProvider implements WebMvcTagsProvider { | ||
@Override | ||
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) { | ||
return Tags.of(Tag.of("a", "b")); | ||
} | ||
} | ||
""", | ||
""" | ||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; | ||
import org.springframework.http.server.observation.ServerRequestObservationContext; | ||
class CustomWebMvcTagsProvider extends DefaultServerRequestObservationConvention { | ||
@Override | ||
public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext context) { | ||
KeyValues values = super.getHighCardinalityKeyValues(context); | ||
values.and(KeyValue.of("a", "b")); | ||
return values; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void shouldNotFailOnEmptyMethod() { | ||
//language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import io.micrometer.core.instrument.Tag; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider; | ||
import java.util.Collections; | ||
class CustomWebMvcTagsProvider implements WebMvcTagsProvider { | ||
@Override | ||
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) { | ||
return Collections.emptyList(); | ||
} | ||
void shouldNotFailOnEmptyMethod() {} | ||
} | ||
""", | ||
""" | ||
import io.micrometer.common.KeyValues; | ||
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; | ||
import org.springframework.http.server.observation.ServerRequestObservationContext; | ||
import java.util.Collections; | ||
class CustomWebMvcTagsProvider extends DefaultServerRequestObservationConvention { | ||
@Override | ||
public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext context) { | ||
KeyValues values = super.getHighCardinalityKeyValues(context); | ||
return values; | ||
} | ||
void shouldNotFailOnEmptyMethod() {} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |