Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper metrics path in Kubernetes annotation #15119

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.quarkus.micrometer.runtime.config.runtime.VertxConfig;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.metrics.MetricsFactory;
import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem;

public class MicrometerProcessor {
private static final DotName METER_REGISTRY = DotName.createSimple(MeterRegistry.class.getName());
Expand Down Expand Up @@ -118,9 +119,10 @@ MetricsCapabilityBuildItem metricsCapabilityBuildItem() {
}

@BuildStep(onlyIf = { MicrometerEnabled.class, PrometheusRegistryProcessor.PrometheusEnabled.class })
MetricsCapabilityBuildItem metricsCapabilityPrometheusBuildItem() {
MetricsCapabilityBuildItem metricsCapabilityPrometheusBuildItem(
NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem) {
return new MetricsCapabilityBuildItem(MetricsFactory.MICROMETER::equals,
mConfig.export.prometheus.path);
nonApplicationRootPathBuildItem.adjustPath(mConfig.export.prometheus.path));
}

@BuildStep(onlyIf = MicrometerEnabled.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ MetricsConfigurationBuildItem metricsConfigurationBuildItem() {
}

@BuildStep
MetricsCapabilityBuildItem metricsCapabilityBuildItem() {
MetricsCapabilityBuildItem metricsCapabilityBuildItem(NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem) {
if (metrics.extensionsEnabled) {
return new MetricsCapabilityBuildItem(x -> MetricsFactory.MP_METRICS.equals(x),
metrics.path);
return new MetricsCapabilityBuildItem(MetricsFactory.MP_METRICS::equals,
nonApplicationRootPathBuildItem.adjustPath(metrics.path));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void assertGeneratedResources() throws IOException {
// Annotations will have a different default prefix,
// and the scrape annotation was specifically configured
assertThat(meta.getAnnotations()).contains(entry("example.io/should_be_scraped", "true"),
entry("example.io/path", "/met"), entry("example.io/port", "9090"),
entry("example.io/path", "/q/met"), entry("example.io/port", "9090"),
entry("example.io/scheme", "http"));
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void assertGeneratedResources() throws IOException {
assertThat(t.getMetadata()).satisfies(meta -> {
// Annotations should not have been created in this configuration.
assertThat(meta.getAnnotations()).doesNotContain(entry("prometheus.io/scrape", "true"),
entry("prometheus.io/path", "/met"), entry("prometheus.io/port", "9090"),
entry("prometheus.io/path", "/q/met"), entry("prometheus.io/port", "9090"),
entry("prometheus.io/scheme", "http"));
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void assertGeneratedResources() throws IOException {
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
assertThat(t.getMetadata()).satisfies(meta -> {
assertThat(meta.getAnnotations()).contains(entry("prometheus.io/scrape", "true"),
entry("prometheus.io/path", "/met"), entry("prometheus.io/port", "9090"),
entry("prometheus.io/path", "/q/metrics"), entry("prometheus.io/port", "9090"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It changed from met to the default of metrics here because I removed quarkus.smallrye-metrics.path for this test as I also wanted to have a test that used the default path

entry("prometheus.io/scheme", "http"));
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.quarkus.it.kubernetes;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.hamcrest.Matchers.is;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.quarkus.bootstrap.model.AppArtifact;
import io.quarkus.builder.Version;
import io.quarkus.test.LogFile;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class KubernetesWithMicrometerTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(GreetingResource.class))
.setApplicationName("health")
.setApplicationVersion("0.1-SNAPSHOT")
.setRun(true)
.setLogFileName("k8s.log")
.setForcedDependencies(
Collections.singletonList(
new AppArtifact("io.quarkus", "quarkus-micrometer-registry-prometheus", Version.getVersion())));

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@LogFile
private Path logfile;

@Test
public void assertApplicationRuns() {
assertThat(logfile).isRegularFile().hasFileName("k8s.log");
TestUtil.assertLogFileContents(logfile, "kubernetes", "micrometer");

given()
.when().get("/greeting")
.then()
.statusCode(200)
.body(is("hello"));
}

@Test
public void assertGeneratedResources() throws IOException {
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
assertThat(d.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo("health");
});

assertThat(d.getSpec()).satisfies(deploymentSpec -> {
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
assertThat(t.getMetadata()).satisfies(meta -> {
assertThat(meta.getAnnotations()).contains(entry("prometheus.io/scrape", "true"),
entry("prometheus.io/path", "/q/metrics"), entry("prometheus.io/port", "8080"),
entry("prometheus.io/scheme", "http"));
});
});
});
});
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
quarkus.http.port=9090
quarkus.smallrye-metrics.path=/met