You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/10/22 13:11:08 UTC

[camel-quarkus] branch master updated: Add observability example (#311)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/master by this push:
     new 0541c85  Add observability example (#311)
0541c85 is described below

commit 0541c8509a52d61148f809ad15de92a03b218974
Author: James Netherton <ja...@users.noreply.github.com>
AuthorDate: Tue Oct 22 14:10:58 2019 +0100

    Add observability example (#311)
    
    fixes #297
    
    WIP
---
 examples/observability/README.adoc                 |  77 +++++++++++
 examples/observability/pom.xml                     | 144 +++++++++++++++++++++
 .../main/java/org/acme/observability/Routes.java   |  34 +++++
 .../health/camel/CustomLivenessCheck.java          |  50 +++++++
 .../health/camel/CustomReadinessCheck.java         |  38 ++++++
 .../microprofile/CamelUptimeHealthCheck.java       |  50 +++++++
 .../src/main/resources/application.properties      |  23 ++++
 .../org/acme/observability/ObservabilityIT.java    |   7 +
 .../org/acme/observability/ObservabilityTest.java  |  52 ++++++++
 examples/pom.xml                                   |   1 +
 10 files changed, 476 insertions(+)

diff --git a/examples/observability/README.adoc b/examples/observability/README.adoc
new file mode 100644
index 0000000..277fbbb
--- /dev/null
+++ b/examples/observability/README.adoc
@@ -0,0 +1,77 @@
+= Camel Quarkus Observability
+
+This example project demonstrates how to add support for metrics, health checks and distributed tracing.
+
+=== Running
+
+[source]
+----
+$ mvn clean compile quarkus:dev -DnoDeps
+----
+
+This compiles the project and starts the Quarkus tooling in https://quarkus.io/guides/maven-tooling#development-mode[development mode].
+
+To build a https://quarkus.io/guides/maven-tooling#building-a-native-executable[native executable] run the following (Docker required):
+
+[source]
+----
+$ mvn clean package -Dnative
+----
+
+You can find the native executable in the `target` directory.
+
+==== Metrics endpoint
+
+Metrics are exposed on an HTTP endpoint at `/metrics`. You can also browse application specific metrics from the `/metrics/application` endpoint.
+
+To view all Camel metrics do:
+
+[source]
+----
+$ curl localhost:8080/metrics/application
+----
+
+To pick out specific metrics you can either use `grep` or the `https://stedolan.github.io/jq/[jq]` library :
+
+[source]
+----
+$ curl -s -H"Accept: application/json" localhost:8080/metrics/application | jq '.["camel.context.exchanges.completed.total;camelContext=camel-quarkus-observability"]'
+----
+
+==== Health endpoint
+
+Camel provides some out of the box liveness and readiness checks. To see this working, interrogate the `/health/live` and `/health/ready` endpoints:
+
+[source]
+----
+$ curl -s localhost:8080/health/live
+----
+
+[source]
+----
+$ curl -s localhost:8080/health/ready
+----
+
+The JSON output will contain a check named 'camel' for verifying whether the `CamelContext` is in the 'Started' state and another check to verify whether each individual route is in the 'Started' state.
+
+This example project contains a custom liveness check class `CustomLivenessCheck` and custom readiness check class `CustomReadinessCheck` which leverage the Camel health API.
+You'll see these listed in the health JSON as 'custom-liveness-check' and 'custom-readiness-check'. On every 5th invocation of these checks, the health status will be reported as DOWN.
+
+You can also directly leverage MicroProfile Metrics APIs to create checks. Class `CamelUptimeHealthCheck` demonstrates how to register a rediness check.
+
+==== Tracing
+
+The tracing configuration for the application can be found within `application.properties`.
+
+To view tracing events, start a Jaeger tracing server. A simple way of doing this is with Docker:
+
+[source]
+----
+$ docker run -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p 5775:5775/udp -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778 -p 16686:16686 -p 14268:14268 -p 9411:9411 jaegertracing/all-in-one:latest
+----
+
+With the server running, browse to http://localhost:16686. Then choose 'greetings-service' from the 'Service' drop down and click the 'Find Traces' button.
+
+The netty-http consumer route introduces a random delay to simulate latency, hence the overall time of each trace should be different. When viewing a trace, you should see
+a hierarchy of 3 spans showing the progression of the message exchange through each endpoint.
+
diff --git a/examples/observability/pom.xml b/examples/observability/pom.xml
new file mode 100644
index 0000000..accc6b1
--- /dev/null
+++ b/examples/observability/pom.xml
@@ -0,0 +1,144 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<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/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>org.apache.camel.quarkus</groupId>
+        <artifactId>camel-quarkus-bom</artifactId>
+        <version>0.2.1-SNAPSHOT</version>
+        <relativePath>../../poms/bom/pom.xml</relativePath>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>camel-quarkus-examples-observability</artifactId>
+    <name>Camel Quarkus :: Examples :: Observability</name>
+    <description>Camel Quarkus Example :: Observability</description>
+
+    <properties>
+        <native-image.docker-build>true</native-image.docker-build>
+        <native-image.container-runtime>docker</native-image.container-runtime>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-timer</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-netty-http</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-microprofile-health</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-microprofile-metrics</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-opentracing</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-resteasy</artifactId>
+        </dependency>
+
+        <!-- test dependencies -->
+        <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-junit5</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>io.rest-assured</groupId>
+            <artifactId>rest-assured</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>io.quarkus</groupId>
+                <artifactId>quarkus-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>build</id>
+                        <goals>
+                            <goal>build</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+    <profiles>
+        <profile>
+            <id>native-image</id>
+            <activation>
+                <property>
+                    <name>native</name>
+                </property>
+            </activation>
+            <build>
+                <plugins>
+                    <plugin>
+                        <groupId>org.apache.maven.plugins</groupId>
+                        <artifactId>maven-failsafe-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <goals>
+                                    <goal>integration-test</goal>
+                                    <goal>verify</goal>
+                                </goals>
+                                <configuration>
+                                    <systemProperties>
+                                        <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
+                                    </systemProperties>
+                                </configuration>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <groupId>io.quarkus</groupId>
+                        <artifactId>quarkus-maven-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>native-image</id>
+                                <goals>
+                                    <goal>native-image</goal>
+                                </goals>
+                                <configuration>
+                                    <enableServer>false</enableServer>
+                                    <cleanupServer>true</cleanupServer>
+                                    <enableHttpUrlHandler>true</enableHttpUrlHandler>
+                                </configuration>
+                            </execution>
+                        </executions>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+    </profiles>
+
+</project>
diff --git a/examples/observability/src/main/java/org/acme/observability/Routes.java b/examples/observability/src/main/java/org/acme/observability/Routes.java
new file mode 100644
index 0000000..3b7a902
--- /dev/null
+++ b/examples/observability/src/main/java/org/acme/observability/Routes.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.acme.observability;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class Routes extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        // Invokes a simple greeting endpoint every 10 seconds
+        from("timer:greeting?period=10s")
+            .to("netty-http:http://localhost:8099/greeting");
+
+        from("netty-http:0.0.0.0:8099/greeting")
+            // Random delay to simulate latency
+            .delay(simple("${random(1000, 5000)}"))
+            .setBody(constant("Hello From Camel Quarkus!"));
+    }
+}
diff --git a/examples/observability/src/main/java/org/acme/observability/health/camel/CustomLivenessCheck.java b/examples/observability/src/main/java/org/acme/observability/health/camel/CustomLivenessCheck.java
new file mode 100644
index 0000000..5313c8e
--- /dev/null
+++ b/examples/observability/src/main/java/org/acme/observability/health/camel/CustomLivenessCheck.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.acme.observability.health.camel;
+
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.microprofile.health.AbstractCamelMicroProfileLivenessCheck;
+
+/**
+ * A simple custom liveness check which utilizes the Camel Health API.
+ *
+ * The check status is recorded as DOWN on every 5th invocation.
+ */
+public class CustomLivenessCheck extends AbstractCamelMicroProfileLivenessCheck {
+
+    AtomicInteger hitCount = new AtomicInteger();
+
+    public CustomLivenessCheck() {
+        super("custom-liveness-check");
+        getConfiguration().setEnabled(true);
+    }
+
+    @Override
+    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {
+        int hits = hitCount.incrementAndGet();
+
+        // Flag the check as DOWN on every 5th invocation, else it is UP
+        if (hits %5 == 0) {
+            builder.down();
+        } else {
+            builder.up();
+        }
+    }
+}
diff --git a/examples/observability/src/main/java/org/acme/observability/health/camel/CustomReadinessCheck.java b/examples/observability/src/main/java/org/acme/observability/health/camel/CustomReadinessCheck.java
new file mode 100644
index 0000000..9ebe412
--- /dev/null
+++ b/examples/observability/src/main/java/org/acme/observability/health/camel/CustomReadinessCheck.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.acme.observability.health.camel;
+
+import java.util.Map;
+
+import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.microprofile.health.AbstractCamelMicroProfileReadinessCheck;
+
+/**
+ * A simple custom liveness check which utilizes the Camel Health API.
+ */
+public class CustomReadinessCheck extends AbstractCamelMicroProfileReadinessCheck {
+
+    public CustomReadinessCheck() {
+        super("custom-readiness-check");
+        getConfiguration().setEnabled(true);
+    }
+
+    @Override
+    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {
+        builder.up();
+    }
+}
diff --git a/examples/observability/src/main/java/org/acme/observability/health/microprofile/CamelUptimeHealthCheck.java b/examples/observability/src/main/java/org/acme/observability/health/microprofile/CamelUptimeHealthCheck.java
new file mode 100644
index 0000000..68fc396
--- /dev/null
+++ b/examples/observability/src/main/java/org/acme/observability/health/microprofile/CamelUptimeHealthCheck.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.acme.observability.health.microprofile;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+import org.apache.camel.CamelContext;
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
+import org.eclipse.microprofile.health.Readiness;
+
+/**
+ * A simple CamelContext uptime readiness check which implements the MicroProfile Health API.
+ */
+@Readiness
+@ApplicationScoped
+public class CamelUptimeHealthCheck implements HealthCheck {
+
+    @Inject
+    CamelContext camelContext;
+
+    @Override
+    public HealthCheckResponse call() {
+        HealthCheckResponseBuilder builder = HealthCheckResponse.named("Uptime readiness check");
+
+        if (camelContext.getUptimeMillis() > 0) {
+            builder.up();
+        } else {
+            builder.down();
+        }
+
+        return builder.build();
+    }
+}
diff --git a/examples/observability/src/main/resources/application.properties b/examples/observability/src/main/resources/application.properties
new file mode 100644
index 0000000..5768559
--- /dev/null
+++ b/examples/observability/src/main/resources/application.properties
@@ -0,0 +1,23 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+camel.context.name = camel-quarkus-observability
+
+# Configure Quarkus Jaeger OpenTracing support
+quarkus.jaeger.service-name = greetings-service
+quarkus.jaeger.sampler-type = const
+quarkus.jaeger.sampler-param = 1
+quarkus.jaeger.endpoint = http://localhost:14268/api/traces
diff --git a/examples/observability/src/test/java/org/acme/observability/ObservabilityIT.java b/examples/observability/src/test/java/org/acme/observability/ObservabilityIT.java
new file mode 100644
index 0000000..086e67e
--- /dev/null
+++ b/examples/observability/src/test/java/org/acme/observability/ObservabilityIT.java
@@ -0,0 +1,7 @@
+package org.acme.observability;
+
+import io.quarkus.test.junit.SubstrateTest;
+
+@SubstrateTest
+public class ObservabilityIT extends ObservabilityTest {
+}
diff --git a/examples/observability/src/test/java/org/acme/observability/ObservabilityTest.java b/examples/observability/src/test/java/org/acme/observability/ObservabilityTest.java
new file mode 100644
index 0000000..e9d0ade
--- /dev/null
+++ b/examples/observability/src/test/java/org/acme/observability/ObservabilityTest.java
@@ -0,0 +1,52 @@
+package org.acme.observability;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.http.ContentType;
+
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+@QuarkusTest
+public class ObservabilityTest {
+
+    @Test
+    public void metrics() {
+        // Verify a expected Camel metric is available
+        given()
+            .when().accept(ContentType.JSON)
+            .get("/metrics/application")
+            .then()
+            .statusCode(200)
+            .body(
+                "'camel.context.status;camelContext=camel-quarkus-observability'", is(1)
+            );
+    }
+
+    @Test
+    public void health() {
+        // Verify liveness
+        given()
+            .when().accept(ContentType.JSON)
+            .get("/health/live")
+            .then()
+            .statusCode(200)
+            .body("status", Matchers.is("UP"),
+                  "checks.name", containsInAnyOrder("camel-liveness-checks", "camel"),
+                  "checks.data.custom-liveness-check", containsInAnyOrder(null, "UP")
+            );
+
+        // Verify readiness
+        given()
+            .when().accept(ContentType.JSON)
+            .get("/health/ready")
+            .then()
+            .statusCode(200)
+            .body("status", Matchers.is("UP"),
+                    "checks.name", containsInAnyOrder("camel-readiness-checks", "camel", "Uptime readiness check"),
+                    "checks.data.custom-readiness-check", containsInAnyOrder(null, "UP")
+            );
+    }
+}
diff --git a/examples/pom.xml b/examples/pom.xml
index 2c7780b..dac1d04 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -32,6 +32,7 @@
     <name>Camel Quarkus :: Examples</name>
 
     <modules>
+        <module>observability</module>
         <module>timer-log</module>
         <module>rest-json</module>
     </modules>