diff --git a/src/main/resources/META-INF/rewrite/micrometer-13.yml b/src/main/resources/META-INF/rewrite/micrometer-13.yml new file mode 100644 index 0000000..c10cf9f --- /dev/null +++ b/src/main/resources/META-INF/rewrite/micrometer-13.yml @@ -0,0 +1,37 @@ +# +# Copyright 2024 the original author or authors. +#

+# 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 +#

+# https://www.apache.org/licenses/LICENSE-2.0 +#

+# 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. +# +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.micrometer.UpgradeMicrometer13 +displayName: Migrate to Micrometer 1.13 +description: > + Migrate applications to the latest Micrometer 1.13 release. This recipe will modify an + application's build files, make changes to deprecated/preferred APIs, and migrate configuration settings that have + changes between versions as described in the [Micrometer 1.13 migration guide](https://github.com/micrometer-metrics/micrometer/wiki/1.13-Migration-Guide) +tags: + - micrometer +recipeList: + - org.openrewrite.java.dependencies.UpgradeDependencyVersion: + groupId: io.micrometer + artifactId: '*' + newVersion: 1.13.x + - org.openrewrite.java.ChangePackage: + oldPackageName: io.micrometer.prometheus + newPackageName: io.micrometer.prometheusmetrics + - org.openrewrite.java.ChangeType: + oldFullyQualifiedTypeName: io.prometheus.client.CollectorRegistry + newFullyQualifiedTypeName: io.prometheus.metrics.model.registry.PrometheusRegistry + diff --git a/src/main/resources/META-INF/rewrite/micrometer.yml b/src/main/resources/META-INF/rewrite/micrometer.yml index 305feb4..34999ac 100644 --- a/src/main/resources/META-INF/rewrite/micrometer.yml +++ b/src/main/resources/META-INF/rewrite/micrometer.yml @@ -19,7 +19,4 @@ name: org.openrewrite.micrometer.UpgradeMicrometer displayName: Upgrade Micrometer description: This recipe will apply changes commonly needed when migrating Micrometer. recipeList: - - org.openrewrite.java.dependencies.UpgradeDependencyVersion: - groupId: io.micrometer - artifactId: micrometer-core - newVersion: 1.x + - org.openrewrite.micrometer.UpgradeMicrometer13 diff --git a/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer.java b/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer.java index 4e907fe..c24dabd 100644 --- a/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer.java +++ b/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer.java @@ -22,8 +22,7 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; -import java.util.regex.Pattern; - +import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.maven.Assertions.pomXml; class UpgradeMicrometer implements RewriteTest { @@ -42,8 +41,8 @@ class Dependencies { @DocumentExample void maven() { rewriteRun( - //language=xml pomXml( + //language=xml """ """, - spec -> spec.after(actual -> """ - - - 4.0.0 - com.example - demo - 0.0.1-SNAPSHOT - - - io.micrometer - micrometer-core - %s - - - - """.formatted(Pattern.compile("(1\\.1[1-9]\\.\\d+)").matcher(actual).results().findFirst().orElseThrow().group(1))))); + spec -> spec.after(actual -> { + assertThat(actual) + .as("Any version of Micrometer above 1.10.x") + .containsPattern("1\\.1[1-9]\\.\\d+"); + return actual; + }) + ) + ); } } } diff --git a/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer13Test.java b/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer13Test.java new file mode 100644 index 0000000..1f589f0 --- /dev/null +++ b/src/test/java/org/openrewrite/micrometer/UpgradeMicrometer13Test.java @@ -0,0 +1,90 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.micrometer; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class UpgradeMicrometer13Test implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResource( + "/META-INF/rewrite/micrometer-13.yml", + "org.openrewrite.micrometer.UpgradeMicrometer13"); + } + + @Test + @DocumentExample + void shouldChangePackage() { + // language=java + rewriteRun( + java( + """ + import io.micrometer.prometheus.PrometheusConfig; + import io.micrometer.prometheus.PrometheusMeterRegistry; + + class MicrometerConfig { + PrometheusMeterRegistry prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); + } + """, + """ + import io.micrometer.prometheusmetrics.PrometheusConfig; + import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; + + class MicrometerConfig { + PrometheusMeterRegistry prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); + } + """ + ) + ); + } + + @Test + void shouldMigrateToPrometheusMeterRegistry() { + // language=java + rewriteRun( + java( + """ + import io.micrometer.core.instrument.Clock; + import io.micrometer.prometheus.PrometheusConfig; + import io.micrometer.prometheus.PrometheusMeterRegistry; + import io.prometheus.client.CollectorRegistry; + + class MicrometerConfig { + PrometheusMeterRegistry prometheusMeterRegistry = + new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, CollectorRegistry.defaultRegistry, Clock.SYSTEM); + } + """, + """ + import io.micrometer.core.instrument.Clock; + import io.micrometer.prometheusmetrics.PrometheusConfig; + import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; + import io.prometheus.metrics.model.registry.PrometheusRegistry; + + class MicrometerConfig { + PrometheusMeterRegistry prometheusMeterRegistry = + new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, PrometheusRegistry.defaultRegistry, Clock.SYSTEM); + } + """ + ) + ); + } +}