Skip to content

Commit

Permalink
Initial Micrometer 1.13.x migration recipe (openrewrite#11)
Browse files Browse the repository at this point in the history
* Add Micrometer 1.13.x migration recipe

The migration from `io.prometheus.client.CollectorRegistry` to
`io.prometheus.metrics.model.registry.PrometheusRegistry` is probably
not covering _all_ cases.
But for many code bases simply changing the type might be sufficient
because `CollectorRegistry` and `PrometheusRegistry` have a very similar
(and small) public api that essentially only offers a singleton - exposed
as constant with equal names - and a constructor

Partially resolves openrewrite#8

* Update src/test/java/org/openrewrite/micrometer/UpgradeMicrometer13Test.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/test/java/org/openrewrite/micrometer/UpgradeMicrometer13Test.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Loop in 1.13.x upgrade with the existing UpgradeMicrometer

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored and app committed Aug 22, 2024
1 parent 3218cd5 commit 844e000
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 24 deletions.
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;

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

0 comments on commit 844e000

Please sign in to comment.