Add netty server logging in tests

This commit is contained in:
Nikolay Martynov 2020-03-24 14:51:26 -04:00
parent fee6d942e8
commit e5caa48364
5 changed files with 38 additions and 4 deletions

View File

@ -21,6 +21,8 @@ import org.jboss.netty.handler.codec.http.HttpResponseStatus
import org.jboss.netty.handler.codec.http.HttpServerCodec import org.jboss.netty.handler.codec.http.HttpServerCodec
import org.jboss.netty.handler.logging.LoggingHandler import org.jboss.netty.handler.logging.LoggingHandler
import org.jboss.netty.logging.InternalLogLevel import org.jboss.netty.logging.InternalLogLevel
import org.jboss.netty.logging.InternalLoggerFactory
import org.jboss.netty.logging.Slf4JLoggerFactory
import org.jboss.netty.util.CharsetUtil import org.jboss.netty.util.CharsetUtil
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR
@ -37,8 +39,15 @@ import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1
class Netty38ServerTest extends HttpServerTest<ServerBootstrap> { class Netty38ServerTest extends HttpServerTest<ServerBootstrap> {
static final LoggingHandler LOGGING_HANDLER
static {
InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory())
LOGGING_HANDLER = new LoggingHandler(SERVER_LOGGER.name, InternalLogLevel.DEBUG, true)
}
ChannelPipeline channelPipeline() { ChannelPipeline channelPipeline() {
ChannelPipeline channelPipeline = new DefaultChannelPipeline() ChannelPipeline channelPipeline = new DefaultChannelPipeline()
channelPipeline.addFirst("logger", LOGGING_HANDLER)
channelPipeline.addLast("http-codec", new HttpServerCodec()) channelPipeline.addLast("http-codec", new HttpServerCodec())
channelPipeline.addLast("controller", new SimpleChannelHandler() { channelPipeline.addLast("controller", new SimpleChannelHandler() {
@ -109,7 +118,7 @@ class Netty38ServerTest extends HttpServerTest<ServerBootstrap> {
@Override @Override
ServerBootstrap startServer(int port) { ServerBootstrap startServer(int port) {
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory()) ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory())
bootstrap.setParentHandler(new LoggingHandler(InternalLogLevel.INFO)) bootstrap.setParentHandler(LOGGING_HANDLER)
bootstrap.setPipelineFactory(new ChannelPipelineFactory() { bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override @Override
ChannelPipeline getPipeline() throws Exception { ChannelPipeline getPipeline() throws Exception {

View File

@ -21,6 +21,8 @@ import org.jboss.netty.handler.codec.http.HttpResponseStatus
import org.jboss.netty.handler.codec.http.HttpServerCodec import org.jboss.netty.handler.codec.http.HttpServerCodec
import org.jboss.netty.handler.logging.LoggingHandler import org.jboss.netty.handler.logging.LoggingHandler
import org.jboss.netty.logging.InternalLogLevel import org.jboss.netty.logging.InternalLogLevel
import org.jboss.netty.logging.InternalLoggerFactory
import org.jboss.netty.logging.Slf4JLoggerFactory
import org.jboss.netty.util.CharsetUtil import org.jboss.netty.util.CharsetUtil
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR
@ -37,8 +39,15 @@ import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1
class Netty38ServerTest extends HttpServerTest<ServerBootstrap> { class Netty38ServerTest extends HttpServerTest<ServerBootstrap> {
static final LoggingHandler LOGGING_HANDLER
static {
InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory())
LOGGING_HANDLER = new LoggingHandler(SERVER_LOGGER.name, InternalLogLevel.DEBUG, true)
}
ChannelPipeline channelPipeline() { ChannelPipeline channelPipeline() {
ChannelPipeline channelPipeline = new DefaultChannelPipeline() ChannelPipeline channelPipeline = new DefaultChannelPipeline()
channelPipeline.addFirst("logger", LOGGING_HANDLER)
channelPipeline.addLast("http-codec", new HttpServerCodec()) channelPipeline.addLast("http-codec", new HttpServerCodec())
channelPipeline.addLast("controller", new SimpleChannelHandler() { channelPipeline.addLast("controller", new SimpleChannelHandler() {
@ -109,7 +118,7 @@ class Netty38ServerTest extends HttpServerTest<ServerBootstrap> {
@Override @Override
ServerBootstrap startServer(int port) { ServerBootstrap startServer(int port) {
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory()) ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory())
bootstrap.setParentHandler(new LoggingHandler(InternalLogLevel.INFO)) bootstrap.setParentHandler(LOGGING_HANDLER)
bootstrap.setPipelineFactory(new ChannelPipelineFactory() { bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override @Override
ChannelPipeline getPipeline() throws Exception { ChannelPipeline getPipeline() throws Exception {

View File

@ -34,16 +34,20 @@ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1
class Netty40ServerTest extends HttpServerTest<EventLoopGroup> { class Netty40ServerTest extends HttpServerTest<EventLoopGroup> {
static final LoggingHandler LOGGING_HANDLER = new LoggingHandler(SERVER_LOGGER.name, LogLevel.DEBUG)
@Override @Override
EventLoopGroup startServer(int port) { EventLoopGroup startServer(int port) {
def eventLoopGroup = new NioEventLoopGroup() def eventLoopGroup = new NioEventLoopGroup()
ServerBootstrap bootstrap = new ServerBootstrap() ServerBootstrap bootstrap = new ServerBootstrap()
.group(eventLoopGroup) .group(eventLoopGroup)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(LOGGING_HANDLER)
.childHandler([ .childHandler([
initChannel: { ch -> initChannel: { ch ->
ChannelPipeline pipeline = ch.pipeline() ChannelPipeline pipeline = ch.pipeline()
pipeline.addFirst("logger", LOGGING_HANDLER)
def handlers = [new HttpRequestDecoder(), new HttpResponseEncoder()] def handlers = [new HttpRequestDecoder(), new HttpResponseEncoder()]
handlers.each { pipeline.addLast(it) } handlers.each { pipeline.addLast(it) }
pipeline.addLast([ pipeline.addLast([

View File

@ -33,16 +33,20 @@ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1
class Netty41ServerTest extends HttpServerTest<EventLoopGroup> { class Netty41ServerTest extends HttpServerTest<EventLoopGroup> {
static final LoggingHandler LOGGING_HANDLER = new LoggingHandler(SERVER_LOGGER.name, LogLevel.DEBUG)
@Override @Override
EventLoopGroup startServer(int port) { EventLoopGroup startServer(int port) {
def eventLoopGroup = new NioEventLoopGroup() def eventLoopGroup = new NioEventLoopGroup()
ServerBootstrap bootstrap = new ServerBootstrap() ServerBootstrap bootstrap = new ServerBootstrap()
.group(eventLoopGroup) .group(eventLoopGroup)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(LOGGING_HANDLER)
.childHandler([ .childHandler([
initChannel: { ch -> initChannel: { ch ->
ChannelPipeline pipeline = ch.pipeline() ChannelPipeline pipeline = ch.pipeline()
pipeline.addFirst("logger", LOGGING_HANDLER)
def handlers = [new HttpServerCodec()] def handlers = [new HttpServerCodec()]
handlers.each { pipeline.addLast(it) } handlers.each { pipeline.addLast(it) }
pipeline.addLast([ pipeline.addLast([

View File

@ -1,5 +1,6 @@
package datadog.trace.agent.test.base package datadog.trace.agent.test.base
import ch.qos.logback.classic.Level
import datadog.opentracing.DDSpan import datadog.opentracing.DDSpan
import datadog.trace.agent.test.AgentTestRunner import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.asserts.ListWriterAssert import datadog.trace.agent.test.asserts.ListWriterAssert
@ -15,6 +16,8 @@ import okhttp3.HttpUrl
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import spock.lang.Shared import spock.lang.Shared
import spock.lang.Unroll import spock.lang.Unroll
@ -37,6 +40,11 @@ import static org.junit.Assume.assumeTrue
@Unroll @Unroll
abstract class HttpServerTest<SERVER> extends AgentTestRunner { abstract class HttpServerTest<SERVER> extends AgentTestRunner {
public static final Logger SERVER_LOGGER = LoggerFactory.getLogger("http-server")
static {
((ch.qos.logback.classic.Logger) SERVER_LOGGER).setLevel(Level.DEBUG)
}
@Shared @Shared
SERVER server SERVER server
@Shared @Shared