You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2023/01/09 08:14:03 UTC

[camel-k-examples] branch main updated: fix(trait): Fix tracing example

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-examples.git


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

commit 1c11d0275a6570bb7a37bd49245c0f6b858dfc32
Author: Gaelle Fournier <ga...@gmail.com>
AuthorDate: Fri Jan 6 15:45:39 2023 +0100

    fix(trait): Fix tracing example
---
 generic-examples/README.md                         |  3 +-
 .../traits/tracing/InventoryService.java           | 16 ++---
 generic-examples/traits/tracing/OrderService.java  | 82 +++++-----------------
 generic-examples/traits/tracing/README.md          | 66 +++++++++++++----
 .../traits/tracing/application.properties          |  1 -
 .../tracing/customizers/OpentracingCustomizer.java | 60 ----------------
 generic-examples/traits/tracing/quarkus.properties |  4 --
 7 files changed, 79 insertions(+), 153 deletions(-)

diff --git a/generic-examples/README.md b/generic-examples/README.md
index 9bd1564..ab70585 100644
--- a/generic-examples/README.md
+++ b/generic-examples/README.md
@@ -55,4 +55,5 @@ Traits configuration will be very helpful to fine tune your `Integration`. Here
 |---|---|---|
 | Container | How to customize with `container` trait| [see examples](./traits/container/)|
 | Health | How to customize with `health` trait| [see examples](./traits/health/)|
-| JVM | How to use `jvm` trait| [see examples](./traits/jvm/)|
\ No newline at end of file
+| JVM | How to use `jvm` trait| [see examples](./traits/jvm/)|
+| Tracing (opentracing) | How to use `tracing` trait| [see examples](./traits/tracing/)|
\ No newline at end of file
diff --git a/generic-examples/traits/tracing/InventoryService.java b/generic-examples/traits/tracing/InventoryService.java
index 8afbc70..be194fc 100644
--- a/generic-examples/traits/tracing/InventoryService.java
+++ b/generic-examples/traits/tracing/InventoryService.java
@@ -36,30 +36,28 @@ public class InventoryService extends RouteBuilder {
     
     @Override
     public void configure() throws Exception {
-        
+        restConfiguration()
+            .enableCORS(true)
+            .bindingMode(RestBindingMode.json);
+
         rest()
-            .post("/notify/order")
+            .post("/notify/order/place")
                 .to("direct:notify");
 
         
-        JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
-        jacksonDataFormat.setUnmarshalType(Map.class);
         JacksonDataFormat invDataFormat = new JacksonDataFormat();
         invDataFormat.setUnmarshalType(InventoryNotification.class);
 
-        
         from("direct:notify")
+            .log("notifyorder--> ${body}")
             .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)
+            .log("Inventory Notified ${body}")
             .convertBodyTo(String.class)
         ;
-
     }
 
-
     private static class InventoryNotification {
         private Integer orderId;
         private Integer itemId;
diff --git a/generic-examples/traits/tracing/OrderService.java b/generic-examples/traits/tracing/OrderService.java
index 2e3fa5e..f706927 100644
--- a/generic-examples/traits/tracing/OrderService.java
+++ b/generic-examples/traits/tracing/OrderService.java
@@ -36,118 +36,72 @@ public class OrderService extends RouteBuilder {
     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()
+        rest()
             .post("/place")
                 .to("direct:placeorder");
 
         JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
         jacksonDataFormat.setUnmarshalType(Order.class);
-        
-        
 
         from("direct:placeorder")
-            .log("placeorder--> ${body}")
+            .log("-----IN ${headers}")
             .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()
+            .log("inputBody --> ${body}")
+            .to("http://inventory/notify/order?bridgeEndpoint=true")
             .removeHeaders("*")
-            .log("return from parallelProcessing ${body}")
+            .log("responseBody from inventory --> ${body}")
             .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
-            .setBody(simple("{\"inventory\":${body[0]},\"invoice\":${body[1]}}"))
+            .setBody(simple("{\"inventory\":${body}}"))
             .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 String orderItemName;
         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 setOrderItemName(String orderItemName){
+            this.orderItemName=orderItemName;
+        }
         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 String getOrderItemName(){
+            return this.orderItemName;
+        }
         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/generic-examples/traits/tracing/README.md b/generic-examples/traits/tracing/README.md
index 9c4b080..274af50 100644
--- a/generic-examples/traits/tracing/README.md
+++ b/generic-examples/traits/tracing/README.md
@@ -8,43 +8,81 @@ The Tracing trait can be used to automatically publish tracing information of in
 
 1. Enable Ingress addon in Minikube 
 
-    $ minikube addons enable ingress
+```sh
+$ minikube addons enable ingress
+```
 
 2. Add Minikube IP to /etc/hosts:
 
-    $ echo "$(minikube ip) example.com" | sudo tee -a /etc/hosts
+```sh
+$ echo "$(minikube ip) example.com" | sudo tee -a /etc/hosts
+```
 
-3. To install Jaeger, make sure the Jaeger operator is installed:
+3. Make sure Jaeger operator is available (see https://www.jaegertracing.io/docs for installation details)
 
-    $ kubetcl apply -f tracing/instance.yaml
+4. To use Jaeger, you can install the AllInOne image:
 
-4. Apply the Jaeger All-in-one Template:
+```sh
+$ kubetcl apply -f instance.yaml
+```
 
-    $ kubectl apply -f https://raw.githubusercontent.com/jaegertracing/jaeger-kubernetes/master/all-in-one/jaeger-all-in-one-template.yml
+5. Check the presence of the Jaeger instance
 
+```sh
+$ kubectl get jaeger
+NAME        STATUS    VERSION   STRATEGY   STORAGE   AGE
+instances   Running   1.40.0    allinone   memory    9m16s
+```
 
 ## 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.
+Quarkus OpenTracing extension in Camel automatically creates a Camel OpenTracingTracer and binds it to the Camel registry. Simply declare the traits 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
+```sh
+kamel run InventoryService.java --name inventory \
+   -d camel-jackson \
+   -t tracing.enabled=true \
+   -t tracing.sampler-type=const \ 
+   -t tracing.sampler-param=1 
+```
 
-Let's inject the Opentracing Tracer to the camel OrderService.java application. Let's start the inventory service. 
+This will :
+* enable tracing  
+* automaticly discover of jaeger tracing endpoint
+* sample all traces
+
+To specify the endpoint use the following trait configuration `-t tracing.endpoint=http://instance-collector:14268/api/traces`
 
-See `customizers/OpentracingCustomizer.java` for more details. 
+Let's inject the Opentracing Tracer to the camel OrderService.java application. Let's start the inventory service. 
 
-    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
+```sh
+kamel run OrderService.java --name order \
+   -d camel-jackson \
+   -t tracing.enabled=true \
+   -t tracing.sampler-type=const \ 
+   -t tracing.sampler-param=1
+```
 
 ## View the Jaeger UI 
 
-    minikube service jaeger-query --url
+If you installed the Jaeger Operator as describred, you should be able to access Jaeger interface on minikube : http://example.com.
 
 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. 
+You can make a few requests the REST Service with custom transaction values defined by curl, provided you made the `order` and `inventory` services available (using the **Service** trait is an easy way).
+
+```sh
+curl http://<order-service-external>/place -d '
+{
+    "orderId":58, 
+    "itemId":12, 
+    "quantity":1, 
+    "orderItemName":"awesome item",
+    "price":99
+}' -v -H "Content-Type: application/json"
+```
diff --git a/generic-examples/traits/tracing/application.properties b/generic-examples/traits/tracing/application.properties
deleted file mode 100644
index b2cb50f..0000000
--- a/generic-examples/traits/tracing/application.properties
+++ /dev/null
@@ -1 +0,0 @@
-jaeger.endpoint=http://jaeger-all-in-one-inmemory-collector:14268/api/tracess
\ No newline at end of file
diff --git a/generic-examples/traits/tracing/customizers/OpentracingCustomizer.java b/generic-examples/traits/tracing/customizers/OpentracingCustomizer.java
deleted file mode 100644
index 5bdbd5b..0000000
--- a/generic-examples/traits/tracing/customizers/OpentracingCustomizer.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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/generic-examples/traits/tracing/quarkus.properties b/generic-examples/traits/tracing/quarkus.properties
deleted file mode 100644
index 74ff912..0000000
--- a/generic-examples/traits/tracing/quarkus.properties
+++ /dev/null
@@ -1,4 +0,0 @@
-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