OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用

文章目录

  • 准备Quarkus客户端环境
  • 创建Quarkus测试应用
  • 安装项目级Prometheus运行环境
  • 用Prometheus监控Quarkus应用
  • 用Grafana监控Quarkus应用

准备Quarkus客户端环境

  1. 依次执行以下命令,下载JDK、GraavlVM、Maven,然后配置环境。
$ curl -LO https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u242-b08/OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz
$ tar -xvf jdk_x64_linux_hotspot_8u242b08.tar.gz
$ mv jdk8u242-b08/ ~/jdk8
$ export JAVA_HOME=~/jdk8
 
$ curl -LO http://www.trieuvan.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
$ tar -xvf apache-maven-3.6.3-bin.tar.gz
$ mv apache-maven-3.6.3 ~/maven
$ export PATH=~/maven/bin:$PATH
 
$ curl -LO https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-19.3.1/graalvm-ce-java8-linux-amd64-19.3.1.tar.gz
$ tar -xvf graalvm-ce-java8-linux-amd64-19.3.1.tar.gz 
$ mv graalvm-ce-java8-19.3.1 ~/graalvm
$ export PATH=~/graalvm/bin:$PATH
  
$ gu install native-image
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image
Downloading: Component native-image: Native Image  from github.com
Installing new component: Native Image (org.graalvm.native-image, version 19.3.1)
 
$ gu list
ComponentId              Version             Component name      Origin
--------------------------------------------------------------------------------
graalvm                  19.3.1              GraalVM Core
native-image             19.3.1              Native Image        github.com

创建Quarkus测试应用

  1. 在第一个窗口创建带有“metrics”扩展的Quarkus测试应用,并运行。注意:为了能监控应用级别metrics,我们使用了Quarkus的“metrics”扩展。
$ mvn io.quarkus:quarkus-maven-plugin:1.2.1.Final:create  \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=getting-started \
    -DclassName="org.acme.quickstart.GreetingResource" \
    -Dextensions="metrics" \
    -Dpath="/hello"
 
$ cd getting-started
$ mvn compile quarkus:dev
  1. 在第二个窗口执行以下命令测试应用,并查看应用级别的缺省metrics。
$ curl http://localhost:8080/hello
hello
$ curl http://localhost:8080/metrics
。。。
# HELP vendor_memory_usedNonHeap_bytes Displays the amount of used non-heap memory in bytes.
# TYPE vendor_memory_usedNonHeap_bytes gauge
vendor_memory_usedNonHeap_bytes 3.2114336E7
  1. 在第二个窗口编辑“getting-started/src/main/java/org/acme/quickstart/GreetingResource.java”文件,增加标有“Add line”的代码行。
package org.acme.quickstart;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.metrics.annotation.Counted;  //Add line.
 
@Path("/hello")
public class GreetingResource {
 
    @GET
    @Counted(name = "greetings", description = "How many greetings we've given.")    //Add line.
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
}
  1. 在第二个窗口执行命令,确认已经有应用级别metrics:“application_org_acme_quickstart_GreetingResource_greetings_total”。
$ curl http://localhost:8080/metrics/application
# HELP application_org_acme_quickstart_GreetingResource_greetings_total How many greetings we've given.
# TYPE application_org_acme_quickstart_GreetingResource_greetings_total counter
application_org_acme_quickstart_GreetingResource_greetings_total 0.0
  1. 在第二个窗口创建“getting-started/src/main/java/org/acme/quickstart/PrimeNumberChecker.java”文件,内容如下。其中定义了3个定制应用级metrics:“performedChecks”、“checksTimer”、“highestPrimeNumberSoFar”。
package org.acme.quickstart;
 
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Gauge;
import org.eclipse.microprofile.metrics.annotation.Timed;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
 
@Path("/prime")
public class PrimeNumberChecker {
 
    private long highestPrimeNumberSoFar = 2;
 
    @GET
    @Path("/{number}")
    @Produces("text/plain")
    @Counted(name = "performedChecks", description = "How many primality checks have been performed.")
    @Timed(name = "checksTimer", description = "A measure of how long it takes to perform the primality test.", unit = MetricUnits.MILLISECONDS)
    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;
    }
}
  1. 在第二个窗口执行命令,测试本地Quarkus应用。
$ curl http://localhost:8080/prime/5
5 is prime.
$ curl http://localhost:8080/prime/350
351 is not prime, is divisible by 3.
$ curl http://localhost:8080/prime/359
359 is prime.
  1. 在第二个窗口执行命令查看包含“performedChecks”、“highestPrimeNumberSoFar”、“checksTimer”应用metrics监控值。
$ curl http://localhost:8080/metrics/application | grep performedChecks
# HELP application_org_acme_quickstart_PrimeNumberChecker_performedChecks_total How many primality checks have been performed.
# TYPE application_org_acme_quickstart_PrimeNumberChecker_performedChecks_total counter
application_org_acme_quickstart_PrimeNumberChecker_performedChecks_total 3.0 
 
$ curl http://localhost:8080/metrics/application | grep highestPrimeNumberSoFar
# HELP application_org_acme_quickstart_PrimeNumberChecker_highestPrimeNumberSoFar Highest prime number so far.
# TYPE application_org_acme_quickstart_PrimeNumberChecker_highestPrimeNumberSoFar gauge
application_org_acme_quickstart_PrimeNumberChecker_highestPrimeNumberSoFar 359.0 
 
$ curl http://localhost:8080/metrics/application | grep checksTimer
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_rate_per_second gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_rate_per_second 0.009651120871431985
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_one_min_rate_per_second gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_one_min_rate_per_second 0.0015203946095816126
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_five_min_rate_per_second gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_five_min_rate_per_second 0.07491811314937041
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_fifteen_min_rate_per_second gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_fifteen_min_rate_per_second 0.14412698663875181
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_min_seconds gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_min_seconds 1.07824E-4
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_max_seconds gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_max_seconds 8.55019E-4
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_mean_seconds gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_mean_seconds 4.0862461940924336E-4
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_stddev_seconds gauge
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_stddev_seconds 3.0210634938071025E-4
# HELP application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds A measure of how long it takes to perform the primality test.
# TYPE application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds summary
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds_count 3.0
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds{quantile="0.5"} 4.36168E-4
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds{quantile="0.75"} 8.55019E-4
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds{quantile="0.95"} 8.55019E-4
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds{quantile="0.98"} 8.55019E-4
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds{quantile="0.99"} 8.55019E-4
application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds{quantile="0.999"} 8.55019E-4
  1. 中断第一个窗口运行的应用,然后执行命令打包应用。
$ mvn clean package -DskipTests
  1. 以binary的方式向OpenShift部署Quarkus应用。
$ oc new-build registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:1.5 --binary --name=primes -l app=primes
$ rm -rf target/binary && mkdir -p target/binary && cp -r target/*-runner.jar target/lib target/binary
$ oc new-app primes
$ oc expose service primes
$ oc rollout status -w dc/primes
  1. 完成部署后测试OpenShift上运行的Quarkus应用。
$ curl http://$(oc get route primes |awk 'NR==2 {print $2}')/prime/5
5 is prime.
$ curl http://$(oc get route primes |awk 'NR==2 {print $2}')/prime/351
351 is not prime, is divisible by 3.
$ curl http://$(oc get route primes |awk 'NR==2 {print $2}')/prime/359
359 is prime.

安装项目级Prometheus运行环境

  1. 执行命令,在quarkus-prometheus项目中部署Prometheus运行环境。
$ oc new-project quarkus-prometheus --display-name="Prometheus Monitoring Quarkus App"
  1. 创建以下内容的prometheus.yml文件,它是Prometheus的运行配置文件,定义了两个监控job,其中“localhost:9090”是Prometheus自身metrics地址,“primes:8080”是我们要运行的Quarkus应用的metrics地址。
global:
  scrape_interval:     15s
  evaluation_interval: 15s
alerting:
  alertmanagers:
  - static_configs:
    - targets:
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'hello-app'
    static_configs:
    - targets: ['primes:8080']
  1. 根据prometheus.yml创建Prometheus的运行配置ConfigMap对象。
$ oc create configmap prom --from-file=prometheus.yml=prometheus.yml
configmap/prom created
  1. 创建并部署Prometheus及其相关资源,包括Service、PVC等。
$ oc new-app prom/prometheus
$ oc expose svc/prometheus
$ oc set volume dc/prometheus --add -t configmap --configmap-name=prom -m /etc/prometheus/prometheus.yml --sub-path=prometheus.yml
info: Generated volume name: volume-2vbzq
deploymentconfig.apps.openshift.io/prometheus volume updated
 
$ oc rollout status -w dc/prometheus
Waiting for rollout to finish: 0 out of 1 new replicas have been updated...
Waiting for rollout to finish: 0 out of 1 new replicas have been updated...
Waiting for rollout to finish: 0 of 1 updated replicas are available...
Waiting for latest deployment config spec to be observed by the controller loop...
replication controller "prometheus-2" successfully rolled out

用Prometheus监控Quarkus应用

  1. 持续访问Quarkus应用,产生监控数据。
PRIME_URL=http://$(oc get route primes |awk 'NR==2 {print $2}')
while [ true ] ; do
        BITS=$(( ( RANDOM % 60 ) + 1 ))
        NUM=$(openssl prime -generate -bits $BITS)
        curl ${PRIME_URL}/prime/${NUM}  && echo
        sleep 2
done
  1. 通过名为prometheus的Route访问控制台。将“application_org_acme_quickstart_PrimeNumberChecker_performedChecks_total”填入查询框,然后点击Execute,此时会显示监控到的指标值。
$ oc get route prometheus

OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第1张图片
3. 点击Graph,然后将时间调整到5m(分钟)。此时将会以图形化方式显示监控结果。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第2张图片
4. 将监控指标改为“base_memory_usedHeap_bytes”,然后再Execute,可以查看监控到的内存指标情况。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第3张图片

用Grafana监控Quarkus应用

  1. 执行命令部署Grafana。
$ oc new-app grafana/grafana && oc expose svc/grafana
$ oc rollout status -w dc/grafana
  1. 通过名为grafana的Route访问控制台,用admin/admin登录,然后跳过修改密码进入Grafana控制台。
$ oc get route grafana

OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第4张图片
3. 在控制台初始化页面中点击“Add data source”。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第5张图片
4. 设置HTTP的URL为本地Prometheus的metrics地址“http://prometheus:9090”,然后点击“Save & Test”。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第6张图片
5. 在Grafana控制台中点击“+”,然后进入Dashboard菜单。在New dashborad界面中进入Add Query。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第7张图片
6. 在设置Query步骤中将Metrics设为“application_org_acme_quickstart_PrimeNumberChecker_performedChecks_total”。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第8张图片
7. 在设置General步骤中将Title设为“Prime Checks”。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第9张图片
8. 此时界面已经显示监控的指标图。点击右上方的Save Dashboard图标,并指定名称为“Quarkus Primes”,然后Save。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第10张图片
9. 按照以上的操作方式再新建以下的Dashboard。
指标名1:application_org_acme_quickstart_PrimeNumberChecker_checksTimer_seconds
保存名1:Primes Performance
指标名2:application_org_acme_quickstart_PrimeNumberChecker_highestPrimeNumberSoFar
图样式2:Singlestat
保存名2:Highest So Far
指标名3:base_memory_usedHeap_bytes
图样式3:Gauge
显示单位:Field Units to bytes
保存名3:Memory
10. 最后可以在Grafana的Dashboard显示如下图的监控控制台。
OpenShift 4.3 之 Quarkus(3)用独立的Prometheus监控Quarkus应用_第11张图片

你可能感兴趣的:(OpenShift,4,prometheus,Quarkus)