Skip to content

Commit

Permalink
[4.x] - MP Telemetry examples (#6813)
Browse files Browse the repository at this point in the history
MP Telemetry Examples
---------

Signed-off-by: Dmitry Aleksandrov <dmitry.aleksandrov@oracle.com>
Co-authored-by: Romain Grecourt <romain.grecourt@oracle.com>
  • Loading branch information
dalexandrov and romain-grecourt committed Jun 27, 2023
1 parent b705956 commit fc4c3bf
Show file tree
Hide file tree
Showing 16 changed files with 731 additions and 2 deletions.
65 changes: 63 additions & 2 deletions examples/microprofile/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
# Helidon MP Examples
# Helidon MicroProfile Telemetry Example

This directory contains Helidon MP examples.
This example implements demonstrates usage of MP Telemetry Tracing.

## Build and run

With JDK17+
```bash
mvn package
java -jar greeting/target/helidon-examples-microprofile-telemetry-greeting.jar
```

Run Jaeger tracer. If you prefer to use Docker, run in terminal:
```bash
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-e COLLECTOR_OTLP_ENABLED=true \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.43
```

If you have Jaeger all-in-one installed, use this command:

```bash
jaeger-all-in-one --collector.zipkin.host-port=9411 --collector.otlp.enabled=true
```

Run the Secondary service:

With JDK17+
```bash
mvn package
java -jar secondary/target/helidon-examples-microprofile-telemetry-secondary.jar
```

## Exercise the application

```
curl -X GET http://localhost:8080/greet
"Hello World!"
curl -X GET http://localhost:8080/greet/span
{"Span":"PropagatedSpan{ImmutableSpanContext{traceId=00000000000000000000000000000000, spanId=0000000000000000, traceFlags=00, traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=false}}"}
curl -X GET http://localhost:8080/greet/custom
{
"Custom Span": "SdkSpan{traceId=bea7da56d1fe82400af8ec0a8adb370d, spanId=57647ead5dc32ae7, parentSpanContext=ImmutableSpanContext{traceId=bea7da56d1fe82400af8ec0a8adb370d, spanId=0ca670f1e3330ea5, traceFlags=01, traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=true}, name=custom, kind=INTERNAL, attributes=AttributesMap{data={attribute=value}, capacity=128, totalAddedValues=1}, status=ImmutableStatusData{statusCode=UNSET, description=}, totalRecordedEvents=0, totalRecordedLinks=0, startEpochNanos=1683724682576003542, endEpochNanos=1683724682576006000}"
}
curl -X GET http://localhost:8080/greet/outbound
Secondary
```

Proceed Jaeger UI http://localhost:16686. In the top-down menu select "greeting-service" and click "Find traces". Tracing information should become available.
1 change: 1 addition & 0 deletions examples/microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
<module>bean-validation</module>
<module>http-status-count-mp</module>
<module>lra</module>
<module>telemetry</module>
</modules>
</project>
64 changes: 64 additions & 0 deletions examples/microprofile/telemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Helidon MicroProfile Telemetry Example

This example implements demonstrates usage of MP Telemetry Tracing.

## Build and run

With JDK17+
```bash
mvn package
java -jar greeting/target/helidon-examples-microprofile-telemetry-greeting.jar
```

Run Jaeger tracer. If you prefer to use Docker, run in terminal:
```bash
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-e COLLECTOR_OTLP_ENABLED=true \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.43
```

If you have Jaeger all-in-one installed, use this command:

```bash
jaeger-all-in-one --collector.zipkin.host-port=9411 --collector.otlp.enabled=true
```

Run the Secondary service:

With JDK17+
```bash
mvn package
java -jar secondary/target/helidon-examples-microprofile-telemetry-secondary.jar
```

## Exercise the application

```
curl -X GET http://localhost:8080/greet
"Hello World!"
curl -X GET http://localhost:8080/greet/span
{"Span":"PropagatedSpan{ImmutableSpanContext{traceId=00000000000000000000000000000000, spanId=0000000000000000, traceFlags=00, traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=false}}"}
curl -X GET http://localhost:8080/greet/custom
{
"Custom Span": "SdkSpan{traceId=bea7da56d1fe82400af8ec0a8adb370d, spanId=57647ead5dc32ae7, parentSpanContext=ImmutableSpanContext{traceId=bea7da56d1fe82400af8ec0a8adb370d, spanId=0ca670f1e3330ea5, traceFlags=01, traceState=ArrayBasedTraceState{entries=[]}, remote=false, valid=true}, name=custom, kind=INTERNAL, attributes=AttributesMap{data={attribute=value}, capacity=128, totalAddedValues=1}, status=ImmutableStatusData{statusCode=UNSET, description=}, totalRecordedEvents=0, totalRecordedLinks=0, startEpochNanos=1683724682576003542, endEpochNanos=1683724682576006000}"
}
curl -X GET http://localhost:8080/greet/outbound
Secondary
```

Proceed Jaeger UI http://localhost:16686. In the top-down menu select "greeting-service" and click "Find traces". Tracing information should become available.
77 changes: 77 additions & 0 deletions examples/microprofile/telemetry/greeting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-mp</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../../../../applications/mp/pom.xml</relativePath>
</parent>
<groupId>io.helidon.examples.microprofile</groupId>
<artifactId>helidon-examples-microprofile-telemetry-greeting</artifactId>
<name>Microprofile Telemetry Greeting Example</name>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-jaeger</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.telemetry</groupId>
<artifactId>helidon-microprofile-telemetry</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.smallrye</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<executions>
<execution>
<id>make-index</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed 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 io.helidon.examples.microprofile.telemetry;

import java.util.Collections;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import jakarta.inject.Inject;
import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import org.glassfish.jersey.server.Uri;


/**
* A simple JAX-RS resource to greet you. Examples:
*
* Get default greeting message:
* curl -X GET http://localhost:8080/greet
*
* Get Span information:
* curl -X GET http://localhost:8080/greet/span
*
* Call secondary service:
* curl -X GET http://localhost:8080/greet/outbound
*/
@Path("/greet")
public class GreetResource {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());

private Span span;

private Tracer tracer;

@Uri("http://localhost:8081/secondary")
private WebTarget target;

@Inject
GreetResource(Span span, Tracer tracer) {
this.span = span;
this.tracer = tracer;
}

/**
* Return a worldly greeting message.
*
* @return {@link String}
*/
@GET
@WithSpan("default")
public String getDefaultMessage() {
return "Hello World";
}

/**
* Create an internal custom span and return its description.
* @return {@link JsonObject}
*/
@GET
@Path("custom")
@Produces(MediaType.APPLICATION_JSON)
@WithSpan
public JsonObject useCustomSpan(){
Span span = tracer.spanBuilder("custom")
.setSpanKind(SpanKind.INTERNAL)
.setAttribute("attribute", "value")
.startSpan();
span.end();

return JSON.createObjectBuilder()
.add("Custom Span", span.toString())
.build();
}
/**
* Get Span info.
*
* @return {@link JsonObject}
*/
@GET
@Path("span")
@Produces(MediaType.APPLICATION_JSON)
@WithSpan
public JsonObject getSpanInfo(){
return JSON.createObjectBuilder()
.add("Span", span.toString())
.build();
}

/**
* Call the secondary service running on port 8081.
*
* @return String from the secondary service.
*/
@GET
@Path("/outbound")
@WithSpan("outbound")
public String outbound() {
return target.request().accept(MediaType.TEXT_PLAIN).get(String.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed 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.
*/

/**
* MicroProfile Telemetry example.
*/
package io.helidon.examples.microprofile.telemetry;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed 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.
-->
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
version="3.0"
bean-discovery-mode="annotated">
</beans>

0 comments on commit fc4c3bf

Please sign in to comment.