-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for GELF HTTP transport
Closes #9
- Loading branch information
Jochen Schalanda
committed
Feb 5, 2018
1 parent
a55396e
commit dcd71d0
Showing
9 changed files
with
649 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
44 changes: 44 additions & 0 deletions
44
src/main/java/org/graylog2/gelfclient/encoder/GelfHttpEncoder.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.graylog2.gelfclient.encoder; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageEncoder; | ||
import io.netty.handler.codec.http.DefaultFullHttpRequest; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.netty.handler.codec.http.HttpHeaderValues; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
public class GelfHttpEncoder extends MessageToMessageEncoder<ByteBuf> { | ||
private static final Logger LOG = LoggerFactory.getLogger(GelfHttpEncoder.class); | ||
|
||
private final URI uri; | ||
|
||
public GelfHttpEncoder(URI uri) { | ||
this.uri = uri; | ||
} | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List<Object> list) throws Exception { | ||
final FullHttpRequest request = new DefaultFullHttpRequest( | ||
HttpVersion.HTTP_1_1, HttpMethod.POST, uri.getRawPath(), msg.retain()); | ||
request.headers().set(HttpHeaderNames.HOST, uri.getHost()); | ||
request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); | ||
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, msg.readableBytes()); | ||
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); | ||
|
||
list.add(request); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
LOG.error("Error while encoding HTTP request", cause); | ||
ctx.close(); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
src/main/java/org/graylog2/gelfclient/transport/GelfHttpTransport.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2018 Graylog, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.graylog2.gelfclient.transport; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import io.netty.handler.codec.http.HttpClientCodec; | ||
import io.netty.handler.codec.http.HttpContentDecompressor; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslContextBuilder; | ||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory; | ||
import org.graylog2.gelfclient.GelfConfiguration; | ||
import org.graylog2.gelfclient.encoder.GelfHttpEncoder; | ||
import org.graylog2.gelfclient.encoder.GelfMessageJsonEncoder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A {@link GelfTransport} implementation that uses HTTP(S) to send GELF messages. | ||
* <p>This class is thread-safe.</p> | ||
*/ | ||
public class GelfHttpTransport extends AbstractGelfTransport { | ||
private static final Logger LOG = LoggerFactory.getLogger(GelfHttpTransport.class); | ||
|
||
/** | ||
* Creates a new TCP GELF transport. | ||
* | ||
* @param config the GELF client configuration | ||
*/ | ||
public GelfHttpTransport(GelfConfiguration config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
protected void createBootstrap(final EventLoopGroup workerGroup) { | ||
final Bootstrap bootstrap = new Bootstrap(); | ||
final GelfSenderThread senderThread = new GelfSenderThread(queue, config.getMaxInflightSends()); | ||
|
||
bootstrap.group(workerGroup) | ||
.channel(NioSocketChannel.class) | ||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()) | ||
.option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay()) | ||
.option(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive()) | ||
.remoteAddress(config.getRemoteAddress()) | ||
.handler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
if (config.isTlsEnabled()) { | ||
LOG.debug("TLS enabled."); | ||
final SslContext sslContext; | ||
|
||
if (!config.isTlsCertVerificationEnabled()) { | ||
// If the cert should not be verified just use an insecure trust manager. | ||
LOG.debug("TLS certificate verification disabled!"); | ||
sslContext = SslContextBuilder.forClient() | ||
.trustManager(InsecureTrustManagerFactory.INSTANCE) | ||
.build(); | ||
} else if (config.getTlsTrustCertChainFile() != null) { | ||
// If a cert chain file is set, use it. | ||
LOG.debug("TLS certificate chain file: {}", config.getTlsTrustCertChainFile()); | ||
sslContext = SslContextBuilder.forClient() | ||
.trustManager(config.getTlsTrustCertChainFile()) | ||
.build(); | ||
} else { | ||
// Otherwise use the JVM default cert chain. | ||
sslContext = SslContextBuilder.forClient().build(); | ||
} | ||
|
||
ch.pipeline().addLast(sslContext.newHandler(ch.alloc())); | ||
} | ||
|
||
ch.pipeline().addLast(new HttpClientCodec()); | ||
ch.pipeline().addLast(new HttpContentDecompressor()); | ||
ch.pipeline().addLast(new GelfHttpEncoder(config.getUri())); | ||
ch.pipeline().addLast(new GelfMessageJsonEncoder()); | ||
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { | ||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
senderThread.start(ctx.channel()); | ||
} | ||
|
||
@Override | ||
public void channelInactive(ChannelHandlerContext ctx) throws Exception { | ||
LOG.info("Channel disconnected!"); | ||
senderThread.stop(); | ||
scheduleReconnect(ctx.channel().eventLoop()); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
LOG.error("Exception caught", cause); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
if (config.getSendBufferSize() != -1) { | ||
bootstrap.option(ChannelOption.SO_SNDBUF, config.getSendBufferSize()); | ||
} | ||
|
||
bootstrap.connect().addListener(new ChannelFutureListener() { | ||
@Override | ||
public void operationComplete(ChannelFuture future) throws Exception { | ||
if (future.isSuccess()) { | ||
LOG.debug("Connected!"); | ||
} else { | ||
LOG.error("Connection failed: {}", future.cause().getMessage()); | ||
scheduleReconnect(future.channel().eventLoop()); | ||
} | ||
} | ||
}); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/org/graylog2/gelfclient/encoder/GelfHttpEncoderTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2018 Graylog, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.graylog2.gelfclient.encoder; | ||
|
||
import io.netty.buffer.ByteBufUtil; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
import io.netty.handler.codec.EncoderException; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
import static org.testng.AssertJUnit.assertTrue; | ||
|
||
public class GelfHttpEncoderTest { | ||
|
||
@Test(expectedExceptions = EncoderException.class) | ||
public void testExceptionIsPassedThrough() throws Exception { | ||
final EmbeddedChannel channel = new EmbeddedChannel(new GelfHttpEncoder(null)); | ||
channel.writeOutbound(Unpooled.EMPTY_BUFFER); | ||
} | ||
|
||
@Test | ||
public void testEncode() throws Exception { | ||
final URI uri = URI.create("http://example.org:8080/gelf"); | ||
final EmbeddedChannel channel = new EmbeddedChannel(new GelfHttpEncoder(uri)); | ||
assertTrue(channel.writeOutbound(Unpooled.copiedBuffer("{}", StandardCharsets.UTF_8))); | ||
assertTrue(channel.finish()); | ||
|
||
final FullHttpRequest request = channel.readOutbound(); | ||
assertEquals(HttpMethod.POST, request.method()); | ||
assertEquals("/gelf", request.uri()); | ||
assertEquals("application/json", request.headers().get(HttpHeaderNames.CONTENT_TYPE)); | ||
assertEquals("2", request.headers().get(HttpHeaderNames.CONTENT_LENGTH)); | ||
|
||
final byte[] bytes = ByteBufUtil.getBytes(request.content()); | ||
assertEquals(new byte[]{'{', '}'}, bytes); | ||
} | ||
} |
Oops, something went wrong.