Skip to content

Commit

Permalink
fix(trait): Fix tracing example
Browse files Browse the repository at this point in the history
  • Loading branch information
gansheer committed Jan 6, 2023
1 parent 894b2d0 commit 35770c2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 153 deletions.
3 changes: 2 additions & 1 deletion generic-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)|
| JVM | How to use `jvm` trait| [see examples](./traits/jvm/)|
| Tracing (opentracing) | How to use `tracing` trait| [see examples](./traits/tracing/)|
16 changes: 7 additions & 9 deletions generic-examples/traits/tracing/InventoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
82 changes: 18 additions & 64 deletions generic-examples/traits/tracing/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}
66 changes: 52 additions & 14 deletions generic-examples/traits/tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
1 change: 0 additions & 1 deletion generic-examples/traits/tracing/application.properties

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions generic-examples/traits/tracing/quarkus.properties

This file was deleted.

0 comments on commit 35770c2

Please sign in to comment.