-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70407f5
commit 83c4ce6
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.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,80 @@ | ||
/* | ||
* Copyright 2023 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.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<Recipe> getRecipeList() { | ||
return super.getRecipeList(); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> 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; | ||
} | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.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
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,23 @@ | ||
# | ||
# Copyright 2023 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.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 |
65 changes: 65 additions & 0 deletions
65
src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.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,65 @@ | ||
/* | ||
* Copyright 2023 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.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); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelListTest.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