|
| 1 | +# Library Instrumentation for Ratpack version 1.7 and higher |
| 2 | + |
| 3 | +Provides OpenTelemetry instrumentation for [Ratpack](https://ratpack.io/), enabling HTTP client and |
| 4 | +server spans and metrics. |
| 5 | + |
| 6 | +## Quickstart |
| 7 | + |
| 8 | +### Add these dependencies to your project |
| 9 | + |
| 10 | +Replace `OPENTELEMETRY_VERSION` with the [latest release](https://central.sonatype.com/artifact/io.opentelemetry.instrumentation/opentelemetry-ratpack-1.7). |
| 11 | + |
| 12 | +For Maven, add to your `pom.xml` dependencies: |
| 13 | + |
| 14 | +```xml |
| 15 | +<dependencies> |
| 16 | + <dependency> |
| 17 | + <groupId>io.opentelemetry.instrumentation</groupId> |
| 18 | + <artifactId>opentelemetry-ratpack-1.7</artifactId> |
| 19 | + <version>OPENTELEMETRY_VERSION</version> |
| 20 | + </dependency> |
| 21 | +</dependencies> |
| 22 | +``` |
| 23 | + |
| 24 | +For Gradle, add to your dependencies: |
| 25 | + |
| 26 | +```kotlin |
| 27 | +implementation("io.opentelemetry.instrumentation:opentelemetry-ratpack-1.7:OPENTELEMETRY_VERSION") |
| 28 | +``` |
| 29 | + |
| 30 | +### Usage |
| 31 | + |
| 32 | +The instrumentation library provides implementations for both server and client instrumentation |
| 33 | +that wrap Ratpack components. |
| 34 | + |
| 35 | +```java |
| 36 | +import io.opentelemetry.api.OpenTelemetry; |
| 37 | +import io.opentelemetry.instrumentation.ratpack.v1_7.RatpackServerTelemetry; |
| 38 | +import io.opentelemetry.instrumentation.ratpack.v1_7.RatpackClientTelemetry; |
| 39 | +import ratpack.server.RatpackServer; |
| 40 | +import ratpack.http.client.HttpClient; |
| 41 | + |
| 42 | +public class RatpackConfiguration { |
| 43 | + |
| 44 | + // Create a server with OpenTelemetry instrumentation |
| 45 | + public RatpackServer createTracedServer(OpenTelemetry openTelemetry) throws Exception { |
| 46 | + RatpackServerTelemetry serverTelemetry = RatpackServerTelemetry.create(openTelemetry); |
| 47 | + return RatpackServer.start(server -> { |
| 48 | + server.registryOf(serverTelemetry::configureRegistry); |
| 49 | + server.handlers(chain -> |
| 50 | + chain.get(ctx -> ctx.render("Hello, World!")) |
| 51 | + ); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + // Create an instrumented HttpClient |
| 56 | + public HttpClient createTracedClient(OpenTelemetry openTelemetry) { |
| 57 | + RatpackClientTelemetry clientTelemetry = RatpackClientTelemetry.create(openTelemetry); |
| 58 | + return clientTelemetry.instrument(createClient()); |
| 59 | + } |
| 60 | + |
| 61 | + // Configuration of the HttpClient goes here |
| 62 | + private HttpClient createClient() { |
| 63 | + return HttpClient.of(spec -> {}); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
0 commit comments