Skip to content

Commit

Permalink
Refactor to have system tests that nominally exercises most common fu…
Browse files Browse the repository at this point in the history
…nctionality.

There are a bunch of todos with unanswered questions or polish. Coverage is primarily based on invocation analysis and usage patterns of an internal codebase. Does not cover fields.

Not covered:
io.prometheus.client.Collector.MetricFamilySamples.<constructor>
io.prometheus.client.Collector.MetricFamilySamples.Sample.<constructor>
io.prometheus.client.Collector.register
io.prometheus.client.CollectorRegistry.clear
io.prometheus.client.CollectorRegistry.filteredMetricFamilySamples
io.prometheus.client.CollectorRegistry.getSampleValue
io.prometheus.client.CollectorRegistry.metricFamilySamples
io.prometheus.client.CollectorRegistry.register
io.prometheus.client.CollectorRegistry.unregister
io.prometheus.client.hotspot.DefaultExports.register
io.prometheus.client.jetty.JettyStatisticsCollector.<constructor>
io.prometheus.client.jetty.QueuedThreadPoolStatisticsCollector.<constructor>
io.prometheus.client.jetty.QueuedThreadPoolStatisticsCollector.register
  • Loading branch information
escardin committed Oct 18, 2023
1 parent 6dd7790 commit f5baaf0
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 207 deletions.
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("org.openrewrite.build.recipe-library") version "latest.release"
kotlin("jvm") version "1.9.10"
}

group = "org.openrewrite.recipe"
Expand All @@ -22,8 +23,8 @@ dependencies {
exclude("com.google.auto.service", "auto-service-annotations")
}

compileOnly("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.0")
compileOnly("org.jetbrains.kotlin:kotlin-reflect:1.9.0")
implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.10")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.10")
compileOnly("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143")
compileOnly("io.micrometer:micrometer-core:latest.release")
compileOnly("io.prometheus:simpleclient:latest.release")
Expand All @@ -40,7 +41,6 @@ dependencies {
testImplementation("io.micrometer:micrometer-registry-prometheus:latest.release")
testImplementation("com.google.guava:guava:latest.release")

testRuntimeOnly("org.jetbrains.kotlin:kotlin-stdlib-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
Expand Up @@ -15,13 +15,16 @@
*/
package org.openrewrite.micrometer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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;
import static org.openrewrite.kotlin.Assertions.kotlin;

class PrometheusMicrometerMigrationTests implements RewriteTest {

Expand All @@ -33,210 +36,14 @@ public void defaults(RecipeSpec spec) {

@Disabled
@Test
void counterSimpleTest() {
void migrationSystemTest() throws IOException {
rewriteRun(
//language=java
java(
"""
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
class Test {
private CollectorRegistry registry;
void test() {
Counter.build("gets", "-").create();
Counter counter = Counter.build("gets", "-")
.register(registry);
counter.inc();
counter.inc(5);
counter.get();
counter.collect();
}
}
""",
"""
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
class Test {
private MeterRegistry registry;
void test() {
Counter.build("gets", "-").create();
Counter counter= Counter.builder("gets")
.description("-")
.register(registry);
counter.increment();
counter.increment(5);
counter.count();
counter.measure();
}
}
"""
kotlin(
Files.readString(
Path.of("src/test/kotlin/org/openrewrite/micrometer/PrometheusSystemTest.kt")),
Files.readString(
Path.of("src/test/kotlin/org/openrewrite/micrometer/MicrometerSystemTest.kt"))
)
);
}

@Test
void counterWithLabels() {
rewriteRun(
//language=java
java(
"""
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
class TestProducer {
private CollectorRegistry registry;
public Counter requestsCounter = Counter.build("requests", "-")
.labelNames("name1","name2")
.register(registry);
}
""",
"""
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
class TestProducer {
private MeterRegistry registry;
public Counter requestsCounter(String name1Value, String name2Value){
return Counter.builder("requests")
.description("-")
.tags("name1",labels[0],"name2",labels[1])
.register(producer.registry);
}
}
"""
),
//language=java
java(
"""
class TestConsumer {
private TestProducer producer;
void test() {
producer.requestsCounter.labels("value1","value2").inc();
producer.requestsCounter.labels("value1","value2").get();
producer.requestsCounter.labels("value3","value4").inc();
producer.requestsCounter.labels("value3","value4").get();
}
}
""",
"""
class TestConsumer {
private TestProducer producer;
void test() {
producer.requestsCounter("value1","value2").increment();
producer.requestsCounter("value1","value2").measure();
producer.requestsCounter("value3","value4").increment();
producer.requestsCounter("value3","value4").measure();
}
}
"""
)
);
}

@Disabled
@Test
void summaryTest() {
rewriteRun(
//language=java
java(
"""
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Summary;
class Test {
private CollectorRegistry registry;
void test() {
Summary summary = Summary.build("call_times", "-")
.quantile(0.5, 0.05)
.quantile(0.75, 0.02)
.quantile(0.95, 0.01)
.quantile(0.99, 0.001)
.quantile(0.999, 0.0001)
.register(registry);
summary.observe(100.0);
summary.get();
}
}
""",
"""
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
class Test {
private MeterRegistry registry;
void test() {
DistributionSummary summary = DistributionSummary.builder("call_times")
.description("-")
.publishPercentiles(0.5, 0.75, 0.95, 0.99, 0.999)
.percentilePrecision(4)
.distributionStatisticExpiry(Duration.ofMinutes(10))
.distributionStatisticBufferLength(5)
.register(registry);
summary.record(100.0);
summary.takeSnapshot();
}
}
"""
)
);
}

@Disabled
@Test
void histogramTest() {
rewriteRun(
//language=java
java(
"""
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Histogram;
class Test {
private CollectorRegistry registry;
void test() {
Histogram histogram = Histogram.build("histogram", "-")
.buckets(1.0, 2.0)
.register(registry);
histogram.observe(1.0);
}
}
""",
"""
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
class Test {
private MeterRegistry registry;
void test() {
DistributionSummary histogram = DistributionSummary.builder("histogram")
.description("-")
.serviceLevelObjectives(1.0, 2.0)
.register(registry);
histogram.record(1.0);
}
}
"""
)
);
}

}
Loading

0 comments on commit f5baaf0

Please sign in to comment.