Skip to content

Commit

Permalink
CAMEL-21118: Adds rest-cxf-opentelemetry (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarlett authored Aug 28, 2024
1 parent 146dabf commit f1fcd42
Show file tree
Hide file tree
Showing 37 changed files with 1,769 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ readme's instructions.
=== Examples

// examples: START
Number of Examples: 58 (0 deprecated)
Number of Examples: 59 (0 deprecated)

[width="100%",cols="4,2,4",options="header"]
|===
Expand Down Expand Up @@ -77,6 +77,8 @@ Number of Examples: 58 (0 deprecated)

| link:master/readme.adoc[Master] (master) | Clustering | An example showing how to work with Camel's Master component and Spring Boot

| link:rest-cxf-opentelemetry/README.adoc[Rest Cxf Opentelemetry] (rest-cxf-opentelemetry) | CXF | An example showing Camel REST using CXF and OpenTelemetry with Spring Boot

| link:soap-cxf/README.adoc[Soap Cxf] (soap-cxf) | CXF | An example showing the Camel SOAP CXF

| link:arangodb/README.adoc[Arangodb] (arangodb) | Database | An example showing the Camel ArangoDb component with Spring Boot
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<module>reactive-streams</module>
<module>resilience4j</module>
<module>rest-cxf</module>
<module>rest-cxf-opentelemetry</module>
<module>rest-openapi</module>
<module>rest-openapi-simple</module>
<module>rest-openapi-springdoc</module>
Expand Down
145 changes: 145 additions & 0 deletions rest-cxf-opentelemetry/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
== Spring Boot Example with Camel exposing REST services using Apache CXF, collecting distributed tracing using OpenTelemetry

=== Introduction

This example illustrates how to use https://projects.spring.io/spring-boot/[Spring Boot] with http://camel.apache.org[Camel] and https://cxf.apache.org/[Apache CXF] implementing REST services using bottom-up approach.

If you are using REST services directly from Camel and you want to use https://opentelemetry.io/[OpenTelemetry] please refer to link:../opentelemetry/README.adoc[the dedicated example]

There are 3 services which communicate each other, starting from the `random` service:

- random: the main service, exposes the entry point REST service and store the results
- even: the service that verifies the even numbers
- odd: the service that verifies the odd numbers

moreover there is a common module containing common classes

image::docs/overview.png[]

=== Build

You can build this example using the following command; it will also download the OpenTelemetry agent used to instrument the applications:

$ mvn package -Potel-agent

=== Run

Run docker-compose to start all the needed services:

$ docker-compose -f containers/docker-compose.yml up -d

the command runs:

- https://github.com/minio/minio[minio] as application storage
- https://github.com/open-telemetry/opentelemetry-collector[opentelemetry-collector] to receive the generated Trace/Span
- https://github.com/jaegertracing/jaeger[Jaeger] to visualize the traces

Run each services on separated terminals:

$ source containers/env.sh \
&& java -javaagent:target/opentelemetry-javaagent.jar \
-Dotel.service.name=random \
-jar rest-cxf-otel-random/target/*.jar

$ source containers/env.sh \
&& java -javaagent:target/opentelemetry-javaagent.jar \
-Dotel.service.name=even \
-Dserver.port=8081 \
-jar rest-cxf-otel-even/target/*.jar

$ source containers/env.sh \
&& java -javaagent:target/opentelemetry-javaagent.jar \
-Dotel.service.name=odd \
-Dserver.port=8082 \
-jar rest-cxf-otel-odd/target/*.jar

After the Spring Boot applications have been started, you can open the following URL in your web browser http://localhost:8080/services/ to access the list of services, including WADL definition

You can also access the REST endpoint from the command line:

i.e. to generate 5 random numbers run:

$ curl -X POST http://localhost:8080/services/api/play/5 -s | jq .

The command will produce an output like:

[source,json]
----
{
"result": {
"ODD": [
{
"number": 229,
"type": "ODD"
},
{
"number": 585,
"type": "ODD"
}
],
"EVEN": [
{
"number": 648,
"type": "EVEN"
},
{
"number": 670,
"type": "EVEN"
},
{
"number": 846,
"type": "EVEN"
}
]
},
"evenCount": 3,
"oddCount": 2
}
----

Now in the Jaeger UI http://localhost:16686 the traces will be available, both from Camel and from CXF instrumentation

image::docs/jaeger.png[]

The services can be stopped pressing `[CTRL] + [C]` in the shell, and the containers can be stopped with

$ docker-compose -f containers/docker-compose.yml down

=== Explain the code

The dependency `org.apache.cxf:cxf-integration-tracing-opentelemetry` provides `org.apache.cxf.tracing.opentelemetry.jaxrs.OpenTelemetryFeature` that instruments the OpenTelemetry API implemented in the java agent.

The feature is injected in the Spring context

[source,java]
----
include::rest-cxf-otel-common/src/main/java/org/apache/camel/example/springboot/cxf/otel/CxfConfig.java[lines=35..38]
----

and then it is configured in the Camel route as provider in the `cxfrs` endpoint

[source,java]
----
include::rest-cxf-otel-random/src/main/java/org/apache/camel/example/springboot/cxf/otel/CamelRouter.java[lines=51]
----

When the feature is enabled it is possible to add custom tags or logs using `TracerContext` from CXF
[source,java]
----
include::rest-cxf-otel-random/src/main/java/org/apache/camel/example/springboot/cxf/otel/RandomService.java[lines=56..58]
----

[source,java]
----
include::rest-cxf-otel-random/src/main/java/org/apache/camel/example/springboot/cxf/otel/RandomServiceImpl.java[lines=43..45]
----

=== Help and contributions

If you hit any problem using Camel or have some feedback, then please
https://camel.apache.org/community/support/[let us know].

We also love contributors, so
https://camel.apache.org/community/contributing/[get involved] :-)

The Camel riders!
35 changes: 35 additions & 0 deletions rest-cxf-opentelemetry/containers/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: "3.9"

services:

minio:
image: "quay.io/minio/minio:latest"
environment:
- MINIO_ROOT_USER=admin
- MINIO_ROOT_PASSWORD=admin123
ports:
- "9000:9000"
- "9001:9001"
command:
- server
- /data
- --console-address
- :9001

otel-collector:
image: otel/opentelemetry-collector:latest
command:
- --config=/etc/otelcol-cont/otel-collector.yml
volumes:
- ./otel-collector.yml:/etc/otelcol-cont/otel-collector.yml
ports:
- "4318:4318" # OTLP http receiver
- "4317:4317" # OTLP grpc receiver
depends_on:
- jaeger-all-in-one

jaeger-all-in-one:
image: quay.io/jaegertracing/all-in-one:latest
restart: always
ports:
- "16686:16686"
8 changes: 8 additions & 0 deletions rest-cxf-opentelemetry/containers/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/bash

export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4318/v1/traces
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=none
export OTEL_LOGS_EXPORTER=none
#export OTEL_JAVAAGENT_DEBUG=true
25 changes: 25 additions & 0 deletions rest-cxf-opentelemetry/containers/otel-collector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

processors:
batch:

exporters:
debug:
verbosity: detailed
otlp/jaeger:
endpoint: jaeger-all-in-one:4317
tls:
insecure: true

service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [debug,otlp/jaeger]
Binary file added rest-cxf-opentelemetry/docs/jaeger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rest-cxf-opentelemetry/docs/overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f1fcd42

Please sign in to comment.