From 83c4ce67c5c9898dd4ea41018d8a55f307d127e4 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Tue, 3 Oct 2023 03:42:52 -0700 Subject: [PATCH] Start MigrateEmptyLabelMiskCounter --- .../misk/MigrateEmptyLabelMiskCounter.java | 80 +++++++++++++++++++ .../misk/NoExplicitEmptyLabelList.java | 15 ++++ src/main/resources/META-INF/rewrite/misk.yml | 23 ++++++ .../MigrateEmptyLabelMiskCounterTest.java | 65 +++++++++++++++ .../misk/NoExplicitEmptyLabelListTest.java | 15 ++++ 5 files changed, 198 insertions(+) create mode 100644 src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java create mode 100644 src/main/resources/META-INF/rewrite/misk.yml create mode 100644 src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java diff --git a/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java b/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java new file mode 100644 index 0000000..c216193 --- /dev/null +++ b/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java @@ -0,0 +1,80 @@ +/* + * Copyright 2023 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.misk; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.Metrics; +import org.openrewrite.*; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.template.Semantics; +import org.openrewrite.java.tree.J; + +import java.util.List; + +import static kotlin.collections.CollectionsKt.listOf; +import static org.openrewrite.java.template.Semantics.expression; + +public class MigrateEmptyLabelMiskCounter extends Recipe { + + @Override + public String getDisplayName() { + return "Migrate Misk counter to Micrometer"; + } + + @Override + public String getDescription() { + return "Convert a Misk (Prometheus) counter to a Micrometer counter."; + } + + @Override + public List getRecipeList() { + return super.getRecipeList(); + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesType<>("misk.metrics.v2.Metrics", true), new JavaIsoVisitor<>() { + final MethodMatcher miskCounter = new MethodMatcher("misk.metrics.v2.Metrics counter(..)"); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation m = super.visitMethodInvocation(method, ctx); + if (miskCounter.matches(method)) { + boolean emptyLabel = method.getArguments().size() == 2; + if (method.getArguments().size() == 3) { + emptyLabel = expression(this, "listOf", () -> listOf()).build() + .matches(new Cursor(getCursor(), method.getArguments().get(2))); + } + if (!emptyLabel) { + return m; + } + m = expression( + this, "micrometerCounter", + (String name, String help) -> Counter.builder(name).description(help).register(Metrics.globalRegistry) + ).build().apply(getCursor(), + method.getCoordinates().replace(), + method.getArguments().get(0), + method.getArguments().get(1)); + + maybeRemoveImport("misk.metrics.v2.Metrics"); + } + return m; + } + }); + } +} diff --git a/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java b/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java index d66ec77..6da71de 100644 --- a/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java +++ b/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java @@ -1,3 +1,18 @@ +/* + * Copyright 2023 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.misk; import misk.metrics.v2.Metrics; diff --git a/src/main/resources/META-INF/rewrite/misk.yml b/src/main/resources/META-INF/rewrite/misk.yml new file mode 100644 index 0000000..e980530 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/misk.yml @@ -0,0 +1,23 @@ +# +# Copyright 2023 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.misk.MigrateMiskToMicrometer +displayName: Migrate Misk metrics to Micrometer +description: This recipe will move Misk metrics to Micrometer, where that is possible to do without a loss of fidelity. +recipeList: + - org.openrewrite.micrometer.misk.NoExplicitEmptyLabelList + - org.openrewrite.micrometer.misk.MigrateEmptyLabelMiskCounter diff --git a/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java b/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java new file mode 100644 index 0000000..8ad0286 --- /dev/null +++ b/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2023 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.misk; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +public class MigrateEmptyLabelMiskCounterTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new MigrateEmptyLabelMiskCounter()) + .parser(JavaParser.fromJavaVersion() + .classpath("misk-metrics", "kotlin-reflect", "kotlin-stdlib")); + } + + @Disabled + @Test + void migrateEmptyLabel() { + //language=java + rewriteRun( + java( + """ + import misk.metrics.v2.Metrics; + import static kotlin.collections.CollectionsKt.listOf; + + class Test { + void test(Metrics metrics) { + metrics.counter("counter", "description", listOf()); + } + } + """, + """ + import io.micrometer.core.instrument.Counter; + + import static kotlin.collections.CollectionsKt.listOf; + + class Test { + void test(Metrics metrics) { + Counter.builder("counter").description("description").register(Metrics.globalRegistry); + } + } + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelListTest.java b/src/test/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelListTest.java index 812004e..e7f5ce7 100644 --- a/src/test/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelListTest.java +++ b/src/test/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelListTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2023 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.misk; import org.junit.jupiter.api.Disabled;