Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Micrometer 1.13.x migration recipe #11

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/resources/META-INF/rewrite/micrometer-13.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# 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.
#
---
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

5 changes: 1 addition & 4 deletions src/main/resources/META-INF/rewrite/micrometer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 10 additions & 20 deletions src/test/java/org/openrewrite/micrometer/UpgradeMicrometer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,8 +41,8 @@ class Dependencies {
@DocumentExample
void maven() {
rewriteRun(
//language=xml
pomXml(
//language=xml
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -61,23 +60,14 @@ void maven() {
</dependencies>
</project>
""",
spec -> spec.after(actual -> """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>%s</version>
</dependency>
</dependencies>
</project>
""".formatted(Pattern.compile("<version>(1\\.1[1-9]\\.\\d+)</version>").matcher(actual).results().findFirst().orElseThrow().group(1)))));
spec -> spec.after(actual -> {
assertThat(actual)
.as("Any version of Micrometer above 1.10.x")
.containsPattern("<version>1\\.1[1-9]\\.\\d+</version>");
return actual;
})
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.micrometer;
jjank marked this conversation as resolved.
Show resolved Hide resolved

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);
}
"""
)
);
}
}