Skip to content

Commit

Permalink
Start MigrateEmptyLabelMiskCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Oct 3, 2023
1 parent 70407f5 commit 83c4ce6
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
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;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 misk.metrics.v2.Metrics;
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/META-INF/rewrite/misk.yml
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
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);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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;
Expand Down

0 comments on commit 83c4ce6

Please sign in to comment.