-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Prometheus annotations when smallrye metrics in enabled
Fixes: #10301
- Loading branch information
Showing
7 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
...rnetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/Annotations.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,19 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
final class Annotations { | ||
|
||
private Annotations() { | ||
} | ||
|
||
static final class Prometheus { | ||
|
||
private Prometheus() { | ||
} | ||
|
||
private static final String PREFIX = "prometheus.io/"; | ||
|
||
static final String SCRAPE = PREFIX + "scrape"; | ||
static final String PATH = PREFIX + "path"; | ||
static final String PORT = PREFIX + "port"; | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
...c/main/java/io/quarkus/smallrye/metrics/deployment/spi/MetricsConfigurationBuildItem.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,19 @@ | ||
package io.quarkus.smallrye.metrics.deployment.spi; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
|
||
/** | ||
* A build item that can be used by build steps that need to know the metrics configuration | ||
*/ | ||
public final class MetricsConfigurationBuildItem extends SimpleBuildItem { | ||
|
||
private final String path; | ||
|
||
public MetricsConfigurationBuildItem(String path) { | ||
this.path = path; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...uarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithMetricsTest.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,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 KubernetesWithMetricsTest { | ||
|
||
@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") | ||
.withConfigurationResource("kubernetes-with-metrics.properties") | ||
.setForcedDependencies( | ||
Collections.singletonList( | ||
new AppArtifact("io.quarkus", "quarkus-smallrye-metrics", Version.getVersion()))); | ||
|
||
@ProdBuildResults | ||
private ProdModeTestResults prodModeTestResults; | ||
|
||
@LogFile | ||
private Path logfile; | ||
|
||
@Test | ||
public void assertApplicationRuns() { | ||
assertThat(logfile).isRegularFile().hasFileName("k8s.log"); | ||
TestUtil.assertLogFileContents(logfile, "kubernetes", "metrics"); | ||
|
||
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", "/met"), entry("prometheus.io/port", "9090")); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
...sts/kubernetes/quarkus-standard-way/src/test/resources/kubernetes-with-metrics.properties
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,2 @@ | ||
quarkus.http.port=9090 | ||
quarkus.smallrye-metrics.path=/met |