This is an automated email from the ASF dual-hosted git repository. reta pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 1dd5d810b8ae7d13aef2d7138950aea224943913 Author: Andriy Redko <[email protected]> AuthorDate: Thu Sep 14 22:30:56 2023 -0400 CXF-8817: Add support for Micrometer Observation. Add tracing_micrometer sample project --- .../samples/jax_rs/tracing_micrometer/README.txt | 34 +++++ .../samples/jax_rs/tracing_micrometer/pom.xml | 154 +++++++++++++++++++++ .../java/demo/jaxrs/tracing/GoogleBooksApi.java | 31 +++++ .../jaxrs/tracing/ObservationRegistrySetup.java | 65 +++++++++ .../demo/jaxrs/tracing/OpenTelemetrySetup.java | 46 ++++++ .../java/demo/jaxrs/tracing/client/Client.java | 50 +++++++ .../java/demo/jaxrs/tracing/server/Catalog.java | 10 +- .../jaxrs/tracing/server/CatalogApplication.java | 48 +++++++ .../demo/jaxrs/tracing/server/CatalogStore.java | 71 ++++++++++ .../java/demo/jaxrs/tracing/server/Server.java | 55 ++++++++ .../java/demo/jaxrs/tracing/server/Catalog.java | 3 +- distribution/src/main/release/samples/pom.xml | 1 + parent/pom.xml | 5 + 13 files changed, 567 insertions(+), 6 deletions(-) diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/README.txt b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/README.txt new file mode 100644 index 0000000000..e8437eb451 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/README.txt @@ -0,0 +1,34 @@ +JAX-RS Micrometer Observation Demo +================= + +The demo shows a basic usage of Micrometer Observation APIs (distributed tracing) +with REST based Web Services using JAX-RS 2.0 (JSR-339). The REST server provides the +following services at URL http://localhost:9000/catalog: + + - GET to http://localhost:9000/catalog + - POST to http://localhost:9000/catalog + - GET to http://localhost:9000/catalog/<id> + - DELETE to URL http://localhost:9000/catalog/<id> + - GET to URL http://localhost:9000/catalog/search?q=<query> + +The last endpoint calls public Google Books API in order to search the books by +query criteria. It demonstrates the integration with native OpenTelemetry instrumentation. + +Building and running the demo using Maven +--------------------------------------- + +From the base directory of this sample (i.e., where this README file is +located), the Maven pom.xml file can be used to build and run the demo. + + +Using either UNIX or Windows: + + mvn install + mvn -Pserver (from one command line window) + mvn -Pclient (from a second command line window) + + +To remove the target dir, run mvn clean". + + + diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/pom.xml b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/pom.xml new file mode 100644 index 0000000000..df341513a9 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/pom.xml @@ -0,0 +1,154 @@ +<?xml version="1.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. +--> +<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 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>jax_rs_tracing_micrometer</artifactId> + <name>JAX-RS Demo Using Distributed Tracing with Micrometer Observation</name> + <description>JAX-RS Demo Using Distributed Tracing with Micrometer Observation</description> + <parent> + <groupId>org.apache.cxf.samples</groupId> + <artifactId>cxf-samples</artifactId> + <version>4.0.4-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + <properties> + <cxf.version>${project.version}</cxf.version> + </properties> + <profiles> + <profile> + <id>server</id> + <build> + <defaultGoal>test</defaultGoal> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <executions> + <execution> + <phase>test</phase> + <goals> + <goal>java</goal> + </goals> + <configuration> + <mainClass>demo.jaxrs.tracing.server.Server</mainClass> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>client</id> + <build> + <defaultGoal>test</defaultGoal> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <executions> + <execution> + <phase>test</phase> + <goals> + <goal>java</goal> + </goals> + <configuration> + <mainClass>demo.jaxrs.tracing.client.Client</mainClass> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + <dependencies> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-transports-http</artifactId> + <version>4.0.4-SNAPSHOT</version> + </dependency> + <!-- This dependency is needed if you're using the Jetty container --> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-transports-http-jetty</artifactId> + <version>4.0.4-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxrs</artifactId> + <version>4.0.4-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-rs-client</artifactId> + <version>4.0.4-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-integration-tracing-micrometer</artifactId> + <version>4.0.4-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-rs-extension-providers</artifactId> + <version>4.0.4-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>jakarta.json</groupId> + <artifactId>jakarta.json-api</artifactId> + </dependency> + <dependency> + <groupId>org.glassfish</groupId> + <artifactId>jakarta.json</artifactId> + </dependency> + <dependency> + <groupId>jakarta.ws.rs</groupId> + <artifactId>jakarta.ws.rs-api</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-webapp</artifactId> + </dependency> + <dependency> + <groupId>io.github.openfeign</groupId> + <artifactId>feign-core</artifactId> + <version>12.5</version> + </dependency> + <dependency> + <groupId>io.github.openfeign</groupId> + <artifactId>feign-httpclient</artifactId> + <version>12.5</version> + </dependency> + <dependency> + <groupId>io.github.openfeign</groupId> + <artifactId>feign-micrometer</artifactId> + <version>12.5</version> + </dependency> + <dependency> + <groupId>io.micrometer</groupId> + <artifactId>micrometer-tracing-bridge-otel</artifactId> + </dependency> + <dependency> + <groupId>io.opentelemetry</groupId> + <artifactId>opentelemetry-exporter-logging</artifactId> + </dependency> + </dependencies> +</project> diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/GoogleBooksApi.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/GoogleBooksApi.java new file mode 100644 index 0000000000..f5e0080cef --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/GoogleBooksApi.java @@ -0,0 +1,31 @@ +/** + * 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 demo.jaxrs.tracing; + +import feign.Headers; +import feign.Param; +import feign.RequestLine; +import feign.Response; + +public interface GoogleBooksApi { + @RequestLine("GET /books/v1/volumes?q={q}") + @Headers("Accept: application/json") + Response search(@Param("q") String query); +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/ObservationRegistrySetup.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/ObservationRegistrySetup.java new file mode 100644 index 0000000000..a6ba1f39eb --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/ObservationRegistrySetup.java @@ -0,0 +1,65 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 demo.jaxrs.tracing; + +import java.util.Collections; + +import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObservationHandler; +import io.micrometer.observation.ObservationRegistry; +import io.micrometer.tracing.handler.DefaultTracingObservationHandler; +import io.micrometer.tracing.handler.PropagatingReceiverTracingObservationHandler; +import io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler; +import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext; +import io.micrometer.tracing.otel.bridge.OtelPropagator; +import io.micrometer.tracing.otel.bridge.OtelTracer; +import io.micrometer.tracing.otel.bridge.Slf4JBaggageEventListener; +import io.micrometer.tracing.otel.bridge.Slf4JEventListener; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.context.propagation.TextMapPropagator; + +public class ObservationRegistrySetup { + public static ObservationRegistry setup(final OpenTelemetry openTelemetry) { + final OtelCurrentTraceContext bridgeContext = new OtelCurrentTraceContext(); + final io.opentelemetry.api.trace.Tracer otelTracer = openTelemetry.getTracerProvider().get("io.micrometer.micrometer-tracing"); + + final OtelPropagator propagator = new OtelPropagator(ContextPropagators.create( + TextMapPropagator.composite(W3CTraceContextPropagator.getInstance())), otelTracer); + + final Slf4JEventListener slf4JEventListener = new Slf4JEventListener(); + final Slf4JBaggageEventListener slf4JBaggageEventListener = new Slf4JBaggageEventListener(Collections.emptyList()); + + final ObservationRegistry observationRegistry = ObservationRegistry.create(); + final OtelTracer tracer = new OtelTracer(otelTracer, bridgeContext, event -> { + slf4JEventListener.onEvent(event); + slf4JBaggageEventListener.onEvent(event); + }); + + observationRegistry.observationConfig().observationHandler( + new FirstMatchingCompositeObservationHandler( + new PropagatingSenderTracingObservationHandler<>(tracer, propagator), + new PropagatingReceiverTracingObservationHandler<>(tracer, propagator), + new DefaultTracingObservationHandler(tracer) + ) + ); + + return observationRegistry; + } +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/OpenTelemetrySetup.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/OpenTelemetrySetup.java new file mode 100644 index 0000000000..60f9d44692 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/OpenTelemetrySetup.java @@ -0,0 +1,46 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 demo.jaxrs.tracing; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.exporter.logging.LoggingSpanExporter; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.resources.Resource; +import io.opentelemetry.sdk.trace.SdkTracerProvider; +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; +import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; + +public class OpenTelemetrySetup { + public static OpenTelemetrySdk setup(String serviceName) { + final Resource resource = Resource.getDefault() + .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName))); + + final SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder() + .addSpanProcessor(BatchSpanProcessor.builder(LoggingSpanExporter.create()).build()) + .setResource(resource).build(); + + final OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider) + .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) + .buildAndRegisterGlobal(); + + return openTelemetrySdk; + } +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/client/Client.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/client/Client.java new file mode 100644 index 0000000000..1b51b81867 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/client/Client.java @@ -0,0 +1,50 @@ +/** + * 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 demo.jaxrs.tracing.client; + +import java.util.Arrays; + +import demo.jaxrs.tracing.ObservationRegistrySetup; +import demo.jaxrs.tracing.OpenTelemetrySetup; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.tracing.micrometer.jaxrs.ObservationClientProvider; + +import io.opentelemetry.sdk.OpenTelemetrySdk; + +public final class Client { + private Client() { + } + + public static void main(final String[] args) throws Exception { + try (OpenTelemetrySdk openTelemetry = OpenTelemetrySetup.setup(Client.class.getName())) { + final ObservationClientProvider provider = new ObservationClientProvider( + ObservationRegistrySetup.setup(openTelemetry)); + + final Response response = WebClient + .create("http://localhost:9000/catalog", Arrays.asList(provider)) + .accept(MediaType.APPLICATION_JSON).get(); + + System.out.println(response.readEntity(String.class)); + response.close(); + } + } +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/Catalog.java similarity index 93% copy from distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java copy to distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/Catalog.java index 210fea66b4..1293b3fc9c 100644 --- a/distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/Catalog.java @@ -22,6 +22,7 @@ package demo.jaxrs.tracing.server; import java.io.IOException; import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -50,9 +51,8 @@ import org.apache.cxf.tracing.TracerContext; import demo.jaxrs.tracing.GoogleBooksApi; import feign.Feign; -import feign.httpclient.ApacheHttpClient; -import feign.opentracing.TracingClient; -import io.opentracing.Tracer; +import feign.micrometer.MicrometerObservationCapability; +import io.micrometer.observation.ObservationRegistry; @Path("/catalog") public class Catalog { @@ -145,11 +145,11 @@ public class Catalog { public JsonObject search(@QueryParam("q") final String query, @Context final TracerContext tracing) throws Exception { final GoogleBooksApi api = Feign .builder() - .client(new TracingClient(new ApacheHttpClient(), tracing.unwrap(Tracer.class))) + .addCapability(new MicrometerObservationCapability(tracing.unwrap(ObservationRegistry.class))) .target(GoogleBooksApi.class, "https://www.googleapis.com"); final feign.Response response = api.search(query); - try (final Reader reader = response.body().asReader()) { + try (final Reader reader = response.body().asReader(StandardCharsets.UTF_8)) { return Json.createReader(reader).readObject(); } } diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java new file mode 100644 index 0000000000..0e9536ab4c --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/CatalogApplication.java @@ -0,0 +1,48 @@ +/** + * 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 demo.jaxrs.tracing.server; + + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider; +import org.apache.cxf.tracing.micrometer.jaxrs.ObservationFeature; + +import demo.jaxrs.tracing.ObservationRegistrySetup; +import io.opentelemetry.api.GlobalOpenTelemetry; + +@ApplicationPath("/") +public class CatalogApplication extends Application { + @Override + public Set<Object> getSingletons() { + return new HashSet<>( + Arrays.asList( + new Catalog(), + new ObservationFeature(ObservationRegistrySetup.setup(GlobalOpenTelemetry.get())), + new JsrJsonpProvider() + ) + ); + } +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/CatalogStore.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/CatalogStore.java new file mode 100644 index 0000000000..5184012bf5 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/CatalogStore.java @@ -0,0 +1,71 @@ +/** + * 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 demo.jaxrs.tracing.server; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonArrayBuilder; +import jakarta.json.JsonObject; + +public class CatalogStore { + private final Map<String, String> books = new ConcurrentHashMap<>(); + + public CatalogStore() { + } + + public boolean remove(final String key) throws IOException { + return books.remove(key) != null; + } + + public JsonObject get(final String key) throws IOException { + final String title = books.get(key); + + if (title != null) { + return Json.createObjectBuilder() + .add("id", key) + .add("title", title) + .build(); + } + + return null; + } + + public void put(final String key, final String title) throws IOException { + books.put(key, title); + } + + public JsonArray scan() throws IOException { + final JsonArrayBuilder builder = Json.createArrayBuilder(); + + for (Map.Entry<String, String> entry: books.entrySet()) { + builder.add(Json.createObjectBuilder() + .add("id", entry.getKey()) + .add("title", entry.getValue()) + ); + } + + return builder.build(); + } + +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/Server.java b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/Server.java new file mode 100644 index 0000000000..126b2ab340 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/tracing_micrometer/src/main/java/demo/jaxrs/tracing/server/Server.java @@ -0,0 +1,55 @@ +/** + * 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 demo.jaxrs.tracing.server; + +import demo.jaxrs.tracing.OpenTelemetrySetup; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; + +public class Server { + protected Server() throws Exception { + org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(9000); + + // Register and map the dispatcher servlet + final ServletHolder servletHolder = new ServletHolder(new CXFNonSpringJaxrsServlet()); + final ServletContextHandler context = new ServletContextHandler(); + context.setContextPath("/"); + context.addServlet(servletHolder, "/*"); + + servletHolder.setInitParameter("jakarta.ws.rs.Application", CatalogApplication.class.getName()); + + try (OpenTelemetrySdk ignored = OpenTelemetrySetup.setup(CatalogApplication.class.getName())) { + server.setHandler(context); + server.start(); + server.join(); + } + } + + public static void main(String[] args) throws Exception { + new Server(); + System.out.println("Server ready..."); + + Thread.sleep(5 * 6000 * 1000); + System.out.println("Server exiting"); + System.exit(0); + } +} diff --git a/distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java b/distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java index 210fea66b4..75ba4287f8 100644 --- a/distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java +++ b/distribution/src/main/release/samples/jax_rs/tracing_opentelemetry/src/main/java/demo/jaxrs/tracing/server/Catalog.java @@ -22,6 +22,7 @@ package demo.jaxrs.tracing.server; import java.io.IOException; import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -149,7 +150,7 @@ public class Catalog { .target(GoogleBooksApi.class, "https://www.googleapis.com"); final feign.Response response = api.search(query); - try (final Reader reader = response.body().asReader()) { + try (final Reader reader = response.body().asReader(StandardCharsets.UTF_8)) { return Json.createReader(reader).readObject(); } } diff --git a/distribution/src/main/release/samples/pom.xml b/distribution/src/main/release/samples/pom.xml index 4a0964e000..e7dd2406e2 100644 --- a/distribution/src/main/release/samples/pom.xml +++ b/distribution/src/main/release/samples/pom.xml @@ -103,6 +103,7 @@ <module>jax_rs/tracing_opentracing</module> <module>jax_rs/tracing_opentelemetry</module> <module>jax_rs/tracing_opentelemetry_camel</module> + <module>jax_rs/tracing_micrometer</module> <module>jax_rs/websocket</module> <module>jax_rs/websocket_web</module> <module>jax_server_aegis_client</module> diff --git a/parent/pom.xml b/parent/pom.xml index ea38361694..fdbc690513 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -1870,6 +1870,11 @@ <artifactId>micrometer-tracing-bridge-brave</artifactId> <version>${cxf.micrometer-tracing.version}</version> </dependency> + <dependency> + <groupId>io.micrometer</groupId> + <artifactId>micrometer-tracing-bridge-otel</artifactId> + <version>${cxf.micrometer-tracing.version}</version> + </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-tracing-test</artifactId>
