-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial support for GELF HTTP transport #37
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* 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()); | ||
senderThreadReference.set(senderThread); | ||
|
||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not taking the |
||
.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!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see lots of these errors when running against a real HTTP server and it also drops messages.
|
||
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()); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
uri
is set but it's not used in#getRemoteAddress()
. We should probably check if theuri
field is set and use it as an override for thehostname
andport
fields.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getUri()
anduri()
methods are also missing javadoc.