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

[19.x] [WFLY-12809] Quickstart for MP Metrics 2.3 #391

Merged
merged 1 commit into from Mar 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
987 changes: 987 additions & 0 deletions microprofile-metrics/README.adoc

Large diffs are not rendered by default.

418 changes: 418 additions & 0 deletions microprofile-metrics/grafana.json

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions microprofile-metrics/pom.xml
@@ -0,0 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wildfly.quickstarts</groupId>
<artifactId>quickstart-parent</artifactId>
<!--
Maintain separation between the artifact id and the version to help prevent
merge conflicts between commits changing the GA and those changing the V.
-->
<version>19.0.0.Beta4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>microprofile-metrics</artifactId>
<packaging>war</packaging>

<name>Quickstart: microprofile-metrics</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencies>
<!-- Import the MicroProfile Metrics API, we use provided scope as the API is included in WildFly -->
<dependency>
<groupId>org.eclipse.microprofile.metrics</groupId>
<artifactId>microprofile-metrics-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the CDI API, we use provided scope as the API is included in WildFly -->
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the Jakarta REST API, we use provided scope as the API is included in WildFly -->
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
<scope>provided</scope>
</dependency>

<!-- Needed for running tests (you may also use TestNG) -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- Arquillian allows you to test enterprise code such as EJBs and
Transactional(JTA) JPA from JUnit/TestNG -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-common</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- Set the name of the archive -->
<finalName>${project.artifactId}</finalName>
</build>
</project>
13 changes: 13 additions & 0 deletions microprofile-metrics/prometheus.yml
@@ -0,0 +1,13 @@
# prometheus.yml
global:
scrape_interval: 1s
external_labels:
monitor: 'my-monitor'

scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'prime-checker'
static_configs:
- targets: ['localhost:9990']
@@ -0,0 +1,8 @@
package org.wildfly.quickstarts.microprofile.metrics;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class JaxRsApplication extends Application {
}
@@ -0,0 +1,132 @@
package org.wildfly.quickstarts.microprofile.metrics;

import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Gauge;
import org.eclipse.microprofile.metrics.annotation.Metered;
import org.eclipse.microprofile.metrics.annotation.Metric;
import org.eclipse.microprofile.metrics.annotation.RegistryType;
import org.eclipse.microprofile.metrics.annotation.Timed;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.CountDownLatch;

@Path("/")
@ApplicationScoped
public class PrimeNumberChecker {

private static final long COUNTER_INCREMENT = 42;
private long highestPrimeNumberSoFar = 2;

@GET
@Path("/prime/{number}")
@Produces(MediaType.TEXT_PLAIN)
@Counted(name = "performedChecks", displayName="Performed Checks", description = "How many prime checks have been performed.")
@Timed(name = "checksTimer", absolute = true, description = "A measure of how long it takes to perform the primality test.", unit = MetricUnits.MILLISECONDS)
@Metered(name = "checkIfPrimeFrequency", absolute = true)
public String checkIfPrime(@PathParam("number") long number) {
if (number < 1) {
return "Only natural numbers can be prime numbers.";
}

if (number == 1) {
return "1 is not prime.";
}

if (number == 2) {
return "2 is prime.";
}

if (number % 2 == 0) {
return number + " is not prime, it is divisible by 2.";
}

for (int i = 3; i < Math.floor(Math.sqrt(number)) + 1; i = i + 2) {
if (number % i == 0) {
return number + " is not prime, is divisible by " + i + ".";
}
}

if (number > highestPrimeNumberSoFar) {
highestPrimeNumberSoFar = number;
}

return number + " is prime.";
}

@Gauge(name = "highestPrimeNumberSoFar", unit = MetricUnits.NONE, description = "Highest prime number so far.")
public Long highestPrimeNumberSoFar() {
return highestPrimeNumberSoFar;
}

private CountDownLatch countDownLatch = new CountDownLatch(1);

@GET
@Path("/parallel")
@ConcurrentGauge(name = "parallelAccess", description = "Number of parallel accesses")
public void parallelAccess() throws InterruptedException {
countDownLatch.await();
System.out.println("DONE");
}

@GET
@Path("/parallel-finish")
public void parallelFinish() {
countDownLatch.countDown();
System.out.println("Finished parallel execution");
}

@Inject
@Metric(name = "injectedCounter", absolute = true)
private Counter injectedCounter;

@GET
@Path("/injected-metric")
@Produces(MediaType.TEXT_PLAIN)
public String injectedMetric() {
injectedCounter.inc();
return "counter invoked";
}

@Inject
@RegistryType(type = MetricRegistry.Type.APPLICATION)
private MetricRegistry applicationRegistry;

@GET
@Path("/registry")
public void registry() {
// register a new application scoped metric
Counter programmaticCounter = applicationRegistry.counter(Metadata.builder()
.withName("programmaticCounter")
.withDescription("Programmatically created counter")
.build());

programmaticCounter.inc(COUNTER_INCREMENT);
}

@GET
@Path("duplicates")
@Produces(MediaType.TEXT_PLAIN)
@Counted(name = "duplicatedCounter", absolute = true, tags = {"type=original"})
public String duplicates() {
return "duplicated metrics";
}

@GET
@Path("duplicates2")
@Produces(MediaType.TEXT_PLAIN)
@Counted(name = "duplicatedCounter", absolute = true, tags = {"type=copy"})
public String duplicates2() {
return "duplicated metrics";
}
}