Skip to content
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

Websocket access logging feature #1743

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.synapse.config.SynapsePropertiesLoader;
Expand Down Expand Up @@ -109,6 +110,7 @@ protected void initChannel(SocketChannel websocketChannel) throws Exception {
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_CHUNK_SIZE, "8192"));
int maxContentLength = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue(
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH, "65536"));
p.addLast(new WebSocketAccessLoggingHandler(LogLevel.DEBUG));
p.addLast("codec", new HttpServerCodec(maxInitLength, maxHeaderSize, maxChunkSize));
p.addLast("aggregator", new HttpObjectAggregator(maxContentLength));
p.addLast("frameAggregator", new WebSocketFrameAggregator(Integer.MAX_VALUE));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.wso2.carbon.inbound.endpoint.protocol.websocket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.logging.ByteBufFormat;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.SocketAddress;

/**
* This class is used for managing logging the access logs of the WebSocket APIs.
*/
public class WebSocketAccessLoggingHandler extends LoggingHandler {

WebsocketAccessLogger websocketAccessLogger;

public WebSocketAccessLoggingHandler(LogLevel level) {
super(level, ByteBufFormat.HEX_DUMP);
// Initialize log properties
websocketAccessLogger = new WebsocketAccessLogger(this.logger);
}

@Override
public void channelRegistered(ChannelHandlerContext ctx) {
if (this.logger.isEnabled(this.internalLevel)) {
websocketAccessLogger.logChannelRegistered(ctx);
}
ctx.fireChannelRegistered();
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (logger.isEnabled(internalLevel)) {
websocketAccessLogger.logChannelRead(ctx, msg);
}
ctx.fireChannelRead(msg);
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (logger.isEnabled(internalLevel)) {
websocketAccessLogger.logChannelWrite(ctx, msg);
}
ctx.write(msg, promise);
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) {
if (logger.isEnabled(internalLevel)) {
websocketAccessLogger.logChannelUnregistered(ctx);
}
ctx.fireChannelUnregistered();
}

@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.fireChannelActive();
}

@Override
public void channelInactive(ChannelHandlerContext ctx) {
ctx.fireChannelInactive();
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
}

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
ctx.fireUserEventTriggered(evt);
}

@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) {
ctx.fireChannelWritabilityChanged();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.fireExceptionCaught(cause);
}

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
ctx.close(promise);
}

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}

@Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
ctx.bind(localAddress, promise);
}

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
ChannelPromise promise) throws Exception {
ctx.connect(remoteAddress, localAddress, promise);
}

@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
ctx.disconnect(promise);
}

@Override
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
ctx.deregister(promise);
}
}
Loading