Skip to content

Commit

Permalink
Update with latest rewrite-templating
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Oct 3, 2023
1 parent 45b458b commit 70407f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
16 changes: 10 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ dependencies {
implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion")

annotationProcessor("org.openrewrite:rewrite-templating:latest.integration")
implementation("org.openrewrite:rewrite-templating:latest.integration")
compileOnly("com.google.errorprone:error_prone_core:2.19.1:with-dependencies") {
exclude("com.google.auto.service", "auto-service-annotations")
}

// TODO compileOnly?
implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.0")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.0")
implementation("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143")
implementation("io.micrometer:micrometer-core:latest.release")
implementation("io.prometheus:simpleclient:latest.release")
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.0")
compileOnly("org.jetbrains.kotlin:kotlin-reflect:1.9.0")
compileOnly("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143")
compileOnly("io.micrometer:micrometer-core:latest.release")
compileOnly("io.prometheus:simpleclient:latest.release")

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-test")
Expand All @@ -39,4 +39,8 @@ dependencies {

testImplementation("io.micrometer:micrometer-registry-prometheus:latest.release")
testImplementation("com.google.guava:guava:latest.release")

testRuntimeOnly("org.jetbrains.kotlin:kotlin-stlib-common:1.9.0")
testRuntimeOnly("org.jetbrains.kotlin:kotlin-reflect:1.9.0")
testRuntimeOnly("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openrewrite.micrometer.misk;

import io.prometheus.client.SimpleCollector;
import misk.metrics.v2.Metrics;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
Expand All @@ -13,13 +12,9 @@

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static kotlin.collections.CollectionsKt.listOf;
import static org.openrewrite.java.JavaTemplate.compile;
import static org.openrewrite.java.template.Semantics.expression;

public class NoExplicitEmptyLabelList extends Recipe {

Expand All @@ -36,26 +31,18 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {


return new JavaIsoVisitor<>() {
final List<JavaTemplate> hasEmptyLabel = Arrays.asList(
compile(this, "emptyLabelCounter", (JavaTemplate.F3<? extends SimpleCollector<?>, Metrics, String, String>)
(m, s, s1) -> m.counter(s, s1, listOf())).build(),
compile(this, "emptyLabelGauge", (JavaTemplate.F3<? extends SimpleCollector<?>, Metrics, String, String>)
(m, s, s1) -> m.gauge(s, s1, listOf())).build(),
compile(this, "emptyLabelPeakGauge", (JavaTemplate.F3<? extends SimpleCollector<?>, Metrics, String, String>)
(m, s, s1) -> m.peakGauge(s, s1, listOf())).build()
expression(this, "emptyLabelCounter",
(Metrics m, String s, String s1) -> m.counter(s, s1, listOf())).build(),
expression(this, "emptyLabelGauge",
(Metrics m, String s, String s1) -> m.gauge(s, s1, listOf())).build(),
expression(this, "emptyLabelPeakGauge",
(Metrics m, String s, String s1) -> m.peakGauge(s, s1, listOf())).build()
);

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
AtomicInteger i = new AtomicInteger();
Stream.<JavaTemplate.F3<? extends SimpleCollector<?>, Metrics, String, String>>of(
(m, s, s1) -> m.counter(s, s1, listOf()),
(m, s, s1) -> m.gauge(s, s1, listOf()),
(m, s, s1) -> m.peakGauge(s, s1, listOf())
).map(f -> compile(this, "emptyLabel" + i.incrementAndGet(), f).build()).collect(toList());

if (method.getSelect() != null &&
TypeUtils.isOfClassType(method.getSelect().getType(), "misk.metrics.v2.Metrics") &&
hasEmptyLabel.stream().anyMatch(tmpl -> tmpl.matches(getCursor()))) {
Expand Down

0 comments on commit 70407f5

Please sign in to comment.