Last active
October 13, 2021 14:10
-
-
Save HaloFour/a08c5db29af2df18a27b849430d2eed9 to your computer and use it in GitHub Desktop.
Reactor Netty using context-aware HTTP server metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.SocketAddress; | |
import java.time.Duration; | |
import reactor.core.publisher.Mono; | |
import reactor.netty.Connection; | |
import reactor.netty.ConnectionObserver; | |
import reactor.netty.http.server.ContextAwareHttpServerMetricsRecorder; | |
import reactor.netty.http.server.HttpServer; | |
import reactor.util.context.Context; | |
import reactor.util.context.ContextView; | |
public class TestProject { | |
public static void main(String[] args) throws IOException { | |
var server = HttpServer.create() | |
.metrics(true, ServerMetricsReporter::new) | |
.childObserve(new ConnectionObserver() { | |
@Override | |
public void onStateChange(Connection connection, State newState) { } | |
@Override | |
public Context currentContext() { | |
return Context.of("Hello", "World"); | |
} | |
}) | |
.observe(new ConnectionObserver() { | |
@Override | |
public void onStateChange(Connection connection, State newState) { } | |
@Override | |
public Context currentContext() { | |
return Context.of("Hello", "There"); | |
} | |
}) | |
.route(routes -> routes | |
.get("/hello", (req, res) -> res | |
.status(200) | |
.sendString(Mono.just("Hello!"))) | |
) | |
.bindAddress(() -> new InetSocketAddress(InetAddress.getLoopbackAddress(), 8080)) | |
.bindNow(); | |
try { | |
System.out.println("Press enter to quit."); | |
System.in.read(); | |
} finally { | |
server.disposeNow(); | |
} | |
} | |
} | |
final class ServerMetricsReporter extends ContextAwareHttpServerMetricsRecorder { | |
@Override | |
public void recordDataReceivedTime(ContextView contextView, String uri, String method, Duration time) { | |
} | |
@Override | |
public void recordDataSentTime(ContextView contextView, String uri, String method, String status, Duration time) { | |
} | |
@Override | |
public void recordResponseTime(ContextView contextView, String uri, String method, String status, Duration time) { | |
// following the stack trace shows that `HttpServerOperations.listener` is `ServerTransport.ChildObserver` | |
// which does not implement `currentContext` thus falls to the default implementation that returns `Context.empty()` | |
assert contextView.hasKey("Hello"); | |
} | |
@Override | |
public void incrementErrorsCount(ContextView contextView, SocketAddress remoteAddress, String uri) { | |
} | |
@Override | |
public void recordDataReceived(ContextView contextView, SocketAddress remoteAddress, String uri, long bytes) { | |
} | |
@Override | |
public void recordDataSent(ContextView contextView, SocketAddress remoteAddress, String uri, long bytes) { | |
} | |
@Override | |
public void incrementErrorsCount(ContextView contextView, SocketAddress remoteAddress) { | |
} | |
@Override | |
public void recordConnectTime(ContextView contextView, SocketAddress remoteAddress, Duration time, String status) { | |
} | |
@Override | |
public void recordDataReceived(ContextView contextView, SocketAddress remoteAddress, long bytes) { | |
} | |
@Override | |
public void recordDataSent(ContextView contextView, SocketAddress remoteAddress, long bytes) { | |
} | |
@Override | |
public void recordTlsHandshakeTime(ContextView contextView, SocketAddress remoteAddress, Duration time, String status) { | |
} | |
@Override | |
public void recordResolveAddressTime(SocketAddress remoteAddress, Duration time, String status) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment