Skip to content

Commit

Permalink
MP Telemetry Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexandrov committed May 15, 2023
1 parent 870fed6 commit 579128c
Show file tree
Hide file tree
Showing 16 changed files with 658 additions and 0 deletions.
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>
59 changes: 59 additions & 0 deletions examples/microprofile/telemetry/greeting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Helidon MicroProfile Telemetry Example

This example implements demonstrates usage of MP Telemetry Tracing.

## Build and run

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

Run Jaeger tracer

```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.41
```

Run the Secondary service. Go to `secondary` folder and run:

With JDK17+
```bash
mvn package
java -jar 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 "search button". Tracing information should become available.
73 changes: 73 additions & 0 deletions examples/microprofile/telemetry/greeting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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.helidon.microprofile.telemetry</groupId>
<artifactId>helidon-microprofile-telemetry</artifactId>
</dependency>
<dependency>
<groupId>org.jboss</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>org.jboss.jandex</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) 2018, 2022 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) 2018, 2021 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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c) 2013 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 server properties
server.port=8080
server.host=0.0.0.0

# Enable the optional MicroProfile Metrics REST.request metrics
metrics.rest-request.enabled=true

#OpenTelemtry
otel.sdk.disabled=false
otel.traces.exporter=jaeger
otel.service.name=greeting-service
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2013 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.
#

# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties

# Send messages to the console
handlers=io.helidon.logging.jul.HelidonConsoleHandler

# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n

# Global logging level. Can be overridden by specific loggers
.level=INFO

# Quiet Weld
org.jboss.level=WARNING

# Component specific log levels
#io.helidon.reactive.webserver.level=INFO
#io.helidon.config.level=INFO
#io.helidon.security.level=INFO
#io.helidon.microprofile.level=INFO
#io.helidon.common.level=INFO
#io.netty.level=INFO
#org.glassfish.jersey.level=INFO
#org.jboss.weld=INFO
Loading

0 comments on commit 579128c

Please sign in to comment.