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

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new f0909bfec tracing trait usage example
f0909bfec is described below

commit f0909bfeca26fdfe1db0d65f2dc0a0dec15e48b4
Author: SubhasmitaSw <[email protected]>
AuthorDate: Mon Apr 11 15:47:18 2022 +0530

    tracing trait usage example
---
 examples/traits/tracing/InventoryService.java      | 115 ++++++++++++++++
 examples/traits/tracing/OrderService.java          | 153 +++++++++++++++++++++
 examples/traits/tracing/README.md                  |  50 +++++++
 examples/traits/tracing/application.properties     |   1 +
 .../tracing/customizers/OpentracingCustomizer.java |  60 ++++++++
 examples/traits/tracing/instance.yaml              |   4 +
 .../traits/tracing/interface/jaegerInterface.png   | Bin 0 -> 361969 bytes
 examples/traits/tracing/quarkus.properties         |   4 +
 8 files changed, 387 insertions(+)

diff --git a/examples/traits/tracing/InventoryService.java 
b/examples/traits/tracing/InventoryService.java
new file mode 100644
index 000000000..8afbc7076
--- /dev/null
+++ b/examples/traits/tracing/InventoryService.java
@@ -0,0 +1,115 @@
+/*
+ * 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.
+ */
+
+
+/* 
+
+kamel run InventoryService.java --name inventory -d camel-opentracing -d 
mvn:io.jaegertracing:jaeger-client:1.2.0 -d rest-api -d camel-jackson 
--property-file application.properties
+
+*/
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.apache.camel.component.jackson.JacksonDataFormat;
+import java.text.SimpleDateFormat;
+import org.apache.camel.Exchange;
+import java.util.Date;
+import java.util.Map;
+
+
+public class InventoryService extends RouteBuilder {
+
+    
+    @Override
+    public void configure() throws Exception {
+        
+        rest()
+            .post("/notify/order")
+                .to("direct:notify");
+
+        
+        JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
+        jacksonDataFormat.setUnmarshalType(Map.class);
+        JacksonDataFormat invDataFormat = new JacksonDataFormat();
+        invDataFormat.setUnmarshalType(InventoryNotification.class);
+
+        
+        from("direct:notify")
+            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
+            .unmarshal(jacksonDataFormat)
+            .log("Inventory Notified ${body}")
+            .bean(InventoryNotification.class, 
"getInventoryNotification(${body['orderId']},${body['itemId']},${body['quantity']}
 )")
+            .marshal(invDataFormat)
+            .convertBodyTo(String.class)
+        ;
+
+    }
+
+
+    private static class InventoryNotification {
+        private Integer orderId;
+        private Integer itemId;
+        private Integer quantity;
+        private String department;
+        private Date datetime;
+
+        public static InventoryNotification getInventoryNotification(Integer 
orderId, Integer itemId, Integer quantity ){
+            InventoryNotification invenNotification  = new 
InventoryNotification();
+            invenNotification.setOrderId(orderId);
+            invenNotification.setItemId(itemId);
+            invenNotification.setQuantity(quantity);
+            invenNotification.setDepartment("inventory");
+            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' 
HH:mm:ss z");
+            invenNotification.setDatetime(new 
Date(System.currentTimeMillis()));
+            return invenNotification;
+        }
+
+
+        public void setOrderId(Integer orderId){
+            this.orderId=orderId;
+        }
+        public void setItemId(Integer itemId){
+            this.itemId=itemId;
+        }
+        public void setQuantity(Integer quantity){
+            this.quantity=quantity;
+        }
+        public Integer getOrderId(){
+            return this.orderId;
+        }
+        public Integer getItemId(){
+            return this.itemId;
+        }
+        public Integer getQuantity(){
+            return this.quantity;
+        }
+        public String getDepartment() {
+            return department;
+        }
+        public void setDepartment(String department) {
+            this.department = department;
+        }
+        public Date getDatetime() {
+            return datetime;
+        }
+    
+        public void setDatetime(Date datetime) {
+            this.datetime = datetime;
+        }
+    }
+    
+}
\ No newline at end of file
diff --git a/examples/traits/tracing/OrderService.java 
b/examples/traits/tracing/OrderService.java
new file mode 100644
index 000000000..2e3fa5e6c
--- /dev/null
+++ b/examples/traits/tracing/OrderService.java
@@ -0,0 +1,153 @@
+/*
+ * 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.
+ */
+
+
+/*
+
+kamel run --name=order-service-api -d camel-swagger-java -d camel-jackson -d 
camel-undertow  OrderService.java --dev
+
+*/
+
+import java.util.HashMap;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.jackson.JacksonDataFormat;
+import org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy;
+
+public class OrderService extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        
+        restConfiguration()
+            .component("undertow")
+            .apiContextPath("/api-doc")
+            .apiProperty("cors", "true")
+            .apiProperty("api.title", "Order API")
+            .apiProperty("api.version", "1.0")
+            .enableCORS(true)
+            .port("8080")
+            .bindingMode(RestBindingMode.json);
+
+            rest()
+            .post("/place")
+                .to("direct:placeorder");
+
+        JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
+        jacksonDataFormat.setUnmarshalType(Order.class);
+        
+        
+
+        from("direct:placeorder")
+            .log("placeorder--> ${body}")
+            .marshal(jacksonDataFormat)
+            //.unmarshal(jacksonDataFormat)
+            .setHeader("myinputBody",simple("${body}"))
+            .log("inputBody 1 --> ${headers.myinputBody}")
+            .removeHeader("CamelHttp*").setHeader(Exchange.HTTP_METHOD, 
constant(org.apache.camel.component.http.HttpMethods.POST))
+            .setBody(simple("${headers.myinputBody}"))
+            
+            .multicast(new GroupedBodyAggregationStrategy())
+            
.to("http://inventory/notify/order?bridgeEndpoint=true&chunked=false";,
+                
"http://invoice/notify/order?bridgeEndpoint=true&chunked=false";)
+            .end()
+            .removeHeaders("*")
+            .log("return from parallelProcessing ${body}")
+            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
+            
.setBody(simple("{\"inventory\":${body[0]},\"invoice\":${body[1]}}"))
+            .unmarshal().json()
+            .log("-----DONE ${headers}")
+                
+        ;
+        
+
+        
+    }
+
+
+    
+    private static class Order implements java.io.Serializable{
+        private static final long serialVersionUID = 1L;
+        
+        private Integer orderId;
+        private Integer itemId;
+        private String orderItemName;
+        private Integer quantity;
+        private Integer price;
+        private String address;
+        private Integer zipCode;
+        private String datetime;
+        private String department;
+
+        public void setDepartment(String department){
+            this.department = department;
+        }
+        public String getDepartment(){
+            return "Inventory";
+        }
+        public void setDatetime(String datetime){
+            this.datetime = datetime;
+        }
+        public String getDatetime(){
+            return "2019-08-08 22:19:99";
+        }
+        public void setOrderId(Integer orderId){
+            this.orderId=orderId;
+        }
+        public void setItemId(Integer itemId){
+            this.itemId=itemId;
+        }
+        public void setOrderItemName(String orderItemName){
+            this.orderItemName=orderItemName;
+        }
+        public void setQuantity(Integer quantity){
+            this.quantity=quantity;
+        }
+        public void setPrice(Integer price){
+            this.price=price;
+        }
+        public void setAddress(String address){
+            this.address=address;
+        }
+        public void setZipCode(Integer zipCode){
+            this.zipCode=zipCode;
+        }
+        public Integer getOrderId(){
+            return this.orderId;
+        }
+        public Integer getItemId(){
+            return this.itemId;
+        }
+        public String getOrderItemName(){
+            return this.orderItemName;
+        }
+        public Integer getQuantity(){
+            return this.quantity;
+        }
+        public Integer getPrice(){
+            return this.price;
+        }
+        public String getAddress(){
+            return this.address;
+        }
+        public Integer getZipCode(){
+            return this.zipCode;
+        }
+    }
+}
\ No newline at end of file
diff --git a/examples/traits/tracing/README.md 
b/examples/traits/tracing/README.md
new file mode 100644
index 000000000..9c4b0803b
--- /dev/null
+++ b/examples/traits/tracing/README.md
@@ -0,0 +1,50 @@
+# Camel K Tracing Trait
+
+In this section you will find examples about fine tuning your `Integration` 
using **Tracing** `trait` capability.
+
+The Tracing trait can be used to automatically publish tracing information of 
interactions to an OpenTracing compatible collector.
+
+## Configure and Setup Jaeger
+
+1. Enable Ingress addon in Minikube 
+
+    $ minikube addons enable ingress
+
+2. Add Minikube IP to /etc/hosts:
+
+    $ echo "$(minikube ip) example.com" | sudo tee -a /etc/hosts
+
+3. To install Jaeger, make sure the Jaeger operator is installed:
+
+    $ kubetcl apply -f tracing/instance.yaml
+
+4. Apply the Jaeger All-in-one Template:
+
+    $ kubectl apply -f 
https://raw.githubusercontent.com/jaegertracing/jaeger-kubernetes/master/all-in-one/jaeger-all-in-one-template.yml
+
+
+## Enable OpenTracing and trace a REST API call in Camel K Route 
+
+Tracing is an important approach for controlling and monitoring the experience 
of users. We  will be creating two distributed services: `Order` which is a 
rest service, and `Inventory` which is also a rest service.
+
+Quarkus OpenTracing extension in Camel automatically creates a Camel 
OpenTracingTracer and binds it to the Camel registry. Simply configure the 
properties to enable open tracing.
+
+See `quarkus.properties` for details.
+
+    kamel run InventoryService.java --name inventory -d 
mvn:org.apache.camel.quarkus:camel-quarkus-opentracing  -d camel-jackson 
--property-file quarkus.properties -t quarkus.enabled=true
+
+Let's inject the Opentracing Tracer to the camel OrderService.java 
application. Let's start the inventory service. 
+
+See `customizers/OpentracingCustomizer.java` for more details. 
+
+    kamel run --name order OrderService.java 
customizers/OpentracingCustomizer.java -d camel-opentracing -d 
mvn:io.jaegertracing:jaeger-client:1.2.0 -d camel-jackson -d camel-undertow -d 
camel-swagger-java --property-file application.properties
+
+## View the Jaeger UI 
+
+    minikube service jaeger-query --url
+
+In the Jaeger interface we can see the details as:
+
+![Jeager Tracing Interface](interface/jaegerInterface.png)
+
+You can make a few requests the REST Service with custom transaction values 
defined by curl. 
diff --git a/examples/traits/tracing/application.properties 
b/examples/traits/tracing/application.properties
new file mode 100644
index 000000000..b2cb50f50
--- /dev/null
+++ b/examples/traits/tracing/application.properties
@@ -0,0 +1 @@
+jaeger.endpoint=http://jaeger-all-in-one-inmemory-collector:14268/api/tracess
\ No newline at end of file
diff --git a/examples/traits/tracing/customizers/OpentracingCustomizer.java 
b/examples/traits/tracing/customizers/OpentracingCustomizer.java
new file mode 100644
index 000000000..5bdbd5bf1
--- /dev/null
+++ b/examples/traits/tracing/customizers/OpentracingCustomizer.java
@@ -0,0 +1,60 @@
+/*
+ * 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 customizers;
+
+// camel-k: language=java
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.PropertyInject;
+import org.apache.camel.opentracing.OpenTracingTracer;
+
+import io.opentracing.Tracer;
+
+import io.jaegertracing.Configuration;
+import io.jaegertracing.Configuration.ReporterConfiguration;
+import io.jaegertracing.Configuration.SamplerConfiguration;
+import io.jaegertracing.Configuration.SenderConfiguration;
+
+public class OpentracingCustomizer {
+
+    @BindToRegistry
+    public static OpenTracingTracer tracer(
+        CamelContext ctx, 
+        @PropertyInject("env:CAMEL_K_INTEGRATION") String name, 
+        @PropertyInject("jaeger.endpoint") String endpoint) {
+
+            OpenTracingTracer openTracingTracer = new OpenTracingTracer();
+            openTracingTracer.setTracer(new Configuration(name)
+                .withReporter(new ReporterConfiguration()
+                    .withSender(new SenderConfiguration()
+                        .withEndpoint(endpoint)
+                    )
+                )
+                .withSampler(new SamplerConfiguration()
+                    .withType("const")    
+                    .withParam(1)
+                )
+                .getTracer()
+            );
+            openTracingTracer.init(ctx);
+            return openTracingTracer;
+    }
+
+}
\ No newline at end of file
diff --git a/examples/traits/tracing/instance.yaml 
b/examples/traits/tracing/instance.yaml
new file mode 100644
index 000000000..9b1eab9be
--- /dev/null
+++ b/examples/traits/tracing/instance.yaml
@@ -0,0 +1,4 @@
+apiVersion: jaegertracing.io/v1
+kind: Jaeger
+metadata:
+  name: instances
\ No newline at end of file
diff --git a/examples/traits/tracing/interface/jaegerInterface.png 
b/examples/traits/tracing/interface/jaegerInterface.png
new file mode 100644
index 000000000..1dbdbff30
Binary files /dev/null and 
b/examples/traits/tracing/interface/jaegerInterface.png differ
diff --git a/examples/traits/tracing/quarkus.properties 
b/examples/traits/tracing/quarkus.properties
new file mode 100644
index 000000000..74ff9120c
--- /dev/null
+++ b/examples/traits/tracing/quarkus.properties
@@ -0,0 +1,4 @@
+quarkus.jaeger.service-name=inventory
+quarkus.jaeger.sampler-type=const
+quarkus.jaeger.sampler-param=1
+quarkus.jaeger.endpoint=http://jaeger-all-in-one-inmemory-collector:14268/api/traces
\ No newline at end of file

Reply via email to