|
| 1 | +# Library Instrumentation for Netty version 4.1 and higher |
| 2 | + |
| 3 | +Provides OpenTelemetry instrumentation for [Netty](https://netty.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-netty-4.1). |
| 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-netty-4.1</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-netty-4.1:OPENTELEMETRY_VERSION") |
| 28 | +``` |
| 29 | + |
| 30 | +### Usage |
| 31 | + |
| 32 | +#### HTTP Client |
| 33 | + |
| 34 | +```java |
| 35 | +import io.netty.bootstrap.Bootstrap; |
| 36 | +import io.netty.channel.Channel; |
| 37 | +import io.netty.channel.ChannelInitializer; |
| 38 | +import io.netty.channel.EventLoopGroup; |
| 39 | +import io.netty.channel.socket.SocketChannel; |
| 40 | +import io.netty.handler.codec.http.HttpClientCodec; |
| 41 | +import io.opentelemetry.api.OpenTelemetry; |
| 42 | +import io.opentelemetry.context.Context; |
| 43 | +import io.opentelemetry.instrumentation.netty.v4_1.NettyClientTelemetry; |
| 44 | + |
| 45 | +// Get an OpenTelemetry instance |
| 46 | +OpenTelemetry openTelemetry = ...; |
| 47 | + |
| 48 | +NettyClientTelemetry clientTelemetry = NettyClientTelemetry.create(openTelemetry); |
| 49 | + |
| 50 | +EventLoopGroup eventLoopGroup = ...; // Use appropriate EventLoopGroup for your platform |
| 51 | +Class<? extends Channel> channelClass = ...; // Use appropriate Channel class for your platform |
| 52 | + |
| 53 | +Bootstrap bootstrap = new Bootstrap(); |
| 54 | +bootstrap.group(eventLoopGroup) |
| 55 | + .channel(channelClass) |
| 56 | + .handler(new ChannelInitializer<SocketChannel>() { |
| 57 | + @Override |
| 58 | + protected void initChannel(SocketChannel ch) { |
| 59 | + ch.pipeline() |
| 60 | + .addLast(new HttpClientCodec()) |
| 61 | + .addLast(clientTelemetry.createCombinedHandler()) |
| 62 | + .addLast(new YourClientHandler()); // Your application handler |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | +Channel channel = bootstrap.connect("localhost", 8080).sync().channel(); |
| 67 | +NettyClientTelemetry.setChannelContext(channel, Context.current()); |
| 68 | +``` |
| 69 | + |
| 70 | +#### HTTP Server |
| 71 | + |
| 72 | +```java |
| 73 | +import io.netty.bootstrap.ServerBootstrap; |
| 74 | +import io.netty.channel.ChannelInitializer; |
| 75 | +import io.netty.channel.EventLoopGroup; |
| 76 | +import io.netty.channel.ServerChannel; |
| 77 | +import io.netty.channel.socket.SocketChannel; |
| 78 | +import io.netty.handler.codec.http.HttpServerCodec; |
| 79 | +import io.opentelemetry.api.OpenTelemetry; |
| 80 | +import io.opentelemetry.instrumentation.netty.v4_1.NettyServerTelemetry; |
| 81 | + |
| 82 | +// Get an OpenTelemetry instance |
| 83 | +OpenTelemetry openTelemetry = ...; |
| 84 | + |
| 85 | +NettyServerTelemetry serverTelemetry = NettyServerTelemetry.create(openTelemetry); |
| 86 | + |
| 87 | +EventLoopGroup bossGroup = ...; // Use appropriate EventLoopGroup for your platform |
| 88 | +EventLoopGroup workerGroup = ...; // Use appropriate EventLoopGroup for your platform |
| 89 | +Class<? extends ServerChannel> serverChannelClass = ...; // Use appropriate ServerChannel class for your platform |
| 90 | + |
| 91 | +ServerBootstrap bootstrap = new ServerBootstrap(); |
| 92 | +bootstrap.group(bossGroup, workerGroup) |
| 93 | + .channel(serverChannelClass) |
| 94 | + .childHandler(new ChannelInitializer<SocketChannel>() { |
| 95 | + @Override |
| 96 | + protected void initChannel(SocketChannel ch) { |
| 97 | + ch.pipeline() |
| 98 | + .addLast(new HttpServerCodec()) |
| 99 | + .addLast(serverTelemetry.createCombinedHandler()) |
| 100 | + .addLast(new YourServerHandler()); // Your application handler |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | +bootstrap.bind(8080).sync(); |
| 105 | +``` |
0 commit comments