|
| 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.nio.NioEventLoopGroup; |
| 39 | +import io.netty.channel.socket.SocketChannel; |
| 40 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 41 | +import io.netty.handler.codec.http.HttpClientCodec; |
| 42 | +import io.opentelemetry.api.OpenTelemetry; |
| 43 | +import io.opentelemetry.context.Context; |
| 44 | +import io.opentelemetry.instrumentation.netty.v4_1.NettyClientTelemetry; |
| 45 | + |
| 46 | +// Get an OpenTelemetry instance |
| 47 | +OpenTelemetry openTelemetry = ...; |
| 48 | + |
| 49 | +NettyClientTelemetry clientTelemetry = NettyClientTelemetry.create(openTelemetry); |
| 50 | + |
| 51 | +Bootstrap bootstrap = new Bootstrap(); |
| 52 | +bootstrap.group(new NioEventLoopGroup()) |
| 53 | + .channel(NioSocketChannel.class) |
| 54 | + .handler(new ChannelInitializer<SocketChannel>() { |
| 55 | + @Override |
| 56 | + protected void initChannel(SocketChannel ch) { |
| 57 | + ch.pipeline() |
| 58 | + .addLast(new HttpClientCodec()) |
| 59 | + .addLast(clientTelemetry.createRequestHandler()) |
| 60 | + .addLast(clientTelemetry.createResponseHandler()); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | +Channel channel = bootstrap.connect("localhost", 8080).sync().channel(); |
| 65 | +NettyClientTelemetry.setChannelContext(channel, Context.current()); |
| 66 | +``` |
| 67 | + |
| 68 | +#### HTTP Server |
| 69 | + |
| 70 | +```java |
| 71 | +import io.netty.bootstrap.ServerBootstrap; |
| 72 | +import io.netty.channel.ChannelInitializer; |
| 73 | +import io.netty.channel.EventLoopGroup; |
| 74 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 75 | +import io.netty.channel.socket.SocketChannel; |
| 76 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 77 | +import io.netty.handler.codec.http.HttpServerCodec; |
| 78 | +import io.opentelemetry.api.OpenTelemetry; |
| 79 | +import io.opentelemetry.instrumentation.netty.v4_1.NettyServerTelemetry; |
| 80 | + |
| 81 | +// Get an OpenTelemetry instance |
| 82 | +OpenTelemetry openTelemetry = ...; |
| 83 | + |
| 84 | +NettyServerTelemetry serverTelemetry = NettyServerTelemetry.create(openTelemetry); |
| 85 | + |
| 86 | +EventLoopGroup bossGroup = new NioEventLoopGroup(); |
| 87 | +EventLoopGroup workerGroup = new NioEventLoopGroup(); |
| 88 | + |
| 89 | +ServerBootstrap bootstrap = new ServerBootstrap(); |
| 90 | +bootstrap.group(bossGroup, workerGroup) |
| 91 | + .channel(NioServerSocketChannel.class) |
| 92 | + .childHandler(new ChannelInitializer<SocketChannel>() { |
| 93 | + @Override |
| 94 | + protected void initChannel(SocketChannel ch) { |
| 95 | + ch.pipeline() |
| 96 | + .addLast(new HttpServerCodec()) |
| 97 | + .addLast(serverTelemetry.createRequestHandler()) |
| 98 | + .addLast(serverTelemetry.createResponseHandler()); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | +bootstrap.bind(8080).sync(); |
| 103 | +``` |
0 commit comments