Skip to content

Commit c5e7370

Browse files
author
Azeem Muzammil
committed
Implement HTTP/1.1 access logging
1 parent a76a2c5 commit c5e7370

File tree

11 files changed

+158
-37
lines changed

11 files changed

+158
-37
lines changed

ballerina/Ballerina.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
org = "ballerina"
33
name = "http"
4-
version = "2.10.13"
4+
version = "2.10.14"
55
authors = ["Ballerina"]
66
keywords = ["http", "network", "service", "listener", "client"]
77
repository = "https://github.com/ballerina-platform/module-ballerina-http"
@@ -16,8 +16,8 @@ graalvmCompatible = true
1616
[[platform.java17.dependency]]
1717
groupId = "io.ballerina.stdlib"
1818
artifactId = "http-native"
19-
version = "2.10.13"
20-
path = "../native/build/libs/http-native-2.10.13.jar"
19+
version = "2.10.14"
20+
path = "../native/build/libs/http-native-2.10.14-SNAPSHOT.jar"
2121

2222
[[platform.java17.dependency]]
2323
groupId = "io.ballerina.stdlib"

ballerina/CompilerPlugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ id = "http-compiler-plugin"
33
class = "io.ballerina.stdlib.http.compiler.HttpCompilerPlugin"
44

55
[[dependency]]
6-
path = "../compiler-plugin/build/libs/http-compiler-plugin-2.10.13.jar"
6+
path = "../compiler-plugin/build/libs/http-compiler-plugin-2.10.14-SNAPSHOT.jar"

ballerina/Dependencies.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ modules = [
2525
[[package]]
2626
org = "ballerina"
2727
name = "cache"
28-
version = "3.7.0"
28+
version = "3.7.1"
2929
dependencies = [
3030
{org = "ballerina", name = "constraint"},
3131
{org = "ballerina", name = "jballerina.java"},
@@ -76,7 +76,7 @@ modules = [
7676
[[package]]
7777
org = "ballerina"
7878
name = "http"
79-
version = "2.10.13"
79+
version = "2.10.14"
8080
dependencies = [
8181
{org = "ballerina", name = "auth"},
8282
{org = "ballerina", name = "cache"},
@@ -283,7 +283,7 @@ modules = [
283283
[[package]]
284284
org = "ballerina"
285285
name = "observe"
286-
version = "1.2.0"
286+
version = "1.2.3"
287287
dependencies = [
288288
{org = "ballerina", name = "jballerina.java"}
289289
]

native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/DefaultHttpClientConnector.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ public HttpResponseFuture send(OutboundMsgHolder outboundMsgHolder, HttpCarbonMe
198198
new RequestWriteStarter(outboundMsgHolder, activeHttp2ClientChannel).startWritingContent();
199199
httpResponseFuture = outboundMsgHolder.getResponseFuture();
200200
httpResponseFuture.notifyResponseHandle(new ResponseHandle(outboundMsgHolder));
201-
202201
return httpResponseFuture;
203202
}
204203
}

native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/HttpOutboundRespListener.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.slf4j.Logger;
3636
import org.slf4j.LoggerFactory;
3737

38+
import java.util.Calendar;
3839
import java.util.Locale;
3940

4041
/**
@@ -53,6 +54,8 @@ public class HttpOutboundRespListener implements HttpConnectorListener {
5354
private ChunkConfig chunkConfig;
5455
private KeepAliveConfig keepAliveConfig;
5556
private String serverName;
57+
private Calendar inboundRequestArrivalTime;
58+
private String remoteAddress = "-";
5659

5760
public HttpOutboundRespListener(HttpCarbonMessage requestMsg, SourceHandler sourceHandler) {
5861
this.requestDataHolder = new RequestDataHolder(requestMsg);
@@ -64,6 +67,8 @@ public HttpOutboundRespListener(HttpCarbonMessage requestMsg, SourceHandler sour
6467
this.handlerExecutor = HttpTransportContextHolder.getInstance().getHandlerExecutor();
6568
this.serverName = sourceHandler.getServerName();
6669
this.listenerReqRespStateManager = requestMsg.listenerReqRespStateManager;
70+
this.remoteAddress = sourceHandler.getRemoteHost();
71+
this.inboundRequestArrivalTime = Calendar.getInstance();
6772
setBackPressureObservableToHttpResponseFuture();
6873
}
6974

@@ -146,4 +151,12 @@ public void setKeepAliveConfig(KeepAliveConfig config) {
146151
public SourceHandler getSourceHandler() {
147152
return sourceHandler;
148153
}
154+
155+
public Calendar getInboundRequestArrivalTime() {
156+
return inboundRequestArrivalTime;
157+
}
158+
159+
public String getRemoteAddress() {
160+
return remoteAddress;
161+
}
149162
}

native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/HttpServerChannelInitializer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@
6666
import javax.net.ssl.SSLContext;
6767
import javax.net.ssl.SSLEngine;
6868

69-
import static io.ballerina.stdlib.http.transport.contract.Constants.ACCESS_LOG;
70-
import static io.ballerina.stdlib.http.transport.contract.Constants.HTTP_ACCESS_LOG_HANDLER;
7169
import static io.ballerina.stdlib.http.transport.contract.Constants.HTTP_TRACE_LOG_HANDLER;
7270
import static io.ballerina.stdlib.http.transport.contract.Constants.MAX_ENTITY_BODY_VALIDATION_HANDLER;
7371
import static io.ballerina.stdlib.http.transport.contract.Constants.SECURITY;
@@ -220,9 +218,9 @@ public void configureHttpPipeline(ChannelPipeline serverPipeline, String initial
220218
if (httpTraceLogEnabled) {
221219
serverPipeline.addLast(HTTP_TRACE_LOG_HANDLER, new HttpTraceLoggingHandler(TRACE_LOG_DOWNSTREAM));
222220
}
223-
if (httpAccessLogEnabled) {
224-
serverPipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG));
225-
}
221+
// if (httpAccessLogEnabled) {
222+
// serverPipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG));
223+
// }
226224
}
227225
serverPipeline.addLast(URI_HEADER_LENGTH_VALIDATION_HANDLER, new UriAndHeaderLengthValidator(this.serverName));
228226
if (reqSizeValidationConfig.getMaxEntityBodySize() > -1) {
@@ -235,7 +233,7 @@ public void configureHttpPipeline(ChannelPipeline serverPipeline, String initial
235233
webSocketCompressionEnabled));
236234
serverPipeline.addLast(Constants.BACK_PRESSURE_HANDLER, new BackPressureHandler());
237235
serverPipeline.addLast(Constants.HTTP_SOURCE_HANDLER,
238-
new SourceHandler(this.serverConnectorFuture, this.interfaceId, this.chunkConfig,
236+
new SourceHandler(this.serverConnectorFuture, this, this.interfaceId, this.chunkConfig,
239237
keepAliveConfig, this.serverName, this.allChannels,
240238
this.listenerChannels, this.pipeliningEnabled, this.pipeliningLimit,
241239
this.pipeliningGroup));
@@ -267,9 +265,9 @@ private void configureH2cPipeline(ChannelPipeline pipeline) {
267265
pipeline.addLast(HTTP_TRACE_LOG_HANDLER,
268266
new HttpTraceLoggingHandler(TRACE_LOG_DOWNSTREAM));
269267
}
270-
if (httpAccessLogEnabled) {
271-
pipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG));
272-
}
268+
// if (httpAccessLogEnabled) {
269+
// pipeline.addLast(HTTP_ACCESS_LOG_HANDLER, new HttpAccessLoggingHandler(ACCESS_LOG));
270+
// }
273271

274272
final HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> {
275273
if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {

native/src/main/java/io/ballerina/stdlib/http/transport/contractimpl/listener/SourceHandler.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package io.ballerina.stdlib.http.transport.contractimpl.listener;
2121

22+
import io.ballerina.stdlib.http.api.logging.accesslog.HttpAccessLogMessage;
2223
import io.ballerina.stdlib.http.transport.contract.Constants;
2324
import io.ballerina.stdlib.http.transport.contract.ServerConnectorFuture;
2425
import io.ballerina.stdlib.http.transport.contract.config.ChunkConfig;
@@ -46,7 +47,10 @@
4647
import org.slf4j.Logger;
4748
import org.slf4j.LoggerFactory;
4849

50+
import java.net.InetSocketAddress;
4951
import java.net.SocketAddress;
52+
import java.util.ArrayList;
53+
import java.util.List;
5054
import java.util.Map;
5155
import java.util.PriorityQueue;
5256
import java.util.Queue;
@@ -73,8 +77,10 @@ public class SourceHandler extends ChannelInboundHandlerAdapter {
7377

7478
private KeepAliveConfig keepAliveConfig;
7579
private ServerConnectorFuture serverConnectorFuture;
80+
private HttpServerChannelInitializer serverChannelInitializer;
7681
private String interfaceId;
7782
private String serverName;
83+
private String remoteHost;
7884
private boolean idleTimeout;
7985
private ChannelGroup allChannels;
8086
private ChannelGroup listenerChannels;
@@ -87,12 +93,15 @@ public class SourceHandler extends ChannelInboundHandlerAdapter {
8793
private long sequenceId = 1L; //Keep track of the request order for http 1.1 pipelining
8894
private final Queue holdingQueue = new PriorityQueue<>(NUMBER_OF_INITIAL_EVENTS_HELD);
8995
private EventExecutorGroup pipeliningGroup;
96+
private List<HttpAccessLogMessage> httpAccessLogMessages;
9097

91-
public SourceHandler(ServerConnectorFuture serverConnectorFuture, String interfaceId, ChunkConfig chunkConfig,
92-
KeepAliveConfig keepAliveConfig, String serverName, ChannelGroup allChannels,
93-
ChannelGroup listenerChannels, boolean pipeliningEnabled, long pipeliningLimit,
94-
EventExecutorGroup pipeliningGroup) {
98+
public SourceHandler(ServerConnectorFuture serverConnectorFuture,
99+
HttpServerChannelInitializer serverChannelInitializer, String interfaceId,
100+
ChunkConfig chunkConfig, KeepAliveConfig keepAliveConfig, String serverName,
101+
ChannelGroup allChannels, ChannelGroup listenerChannels, boolean pipeliningEnabled,
102+
long pipeliningLimit, EventExecutorGroup pipeliningGroup) {
95103
this.serverConnectorFuture = serverConnectorFuture;
104+
this.serverChannelInitializer = serverChannelInitializer;
96105
this.interfaceId = interfaceId;
97106
this.chunkConfig = chunkConfig;
98107
this.keepAliveConfig = keepAliveConfig;
@@ -104,6 +113,7 @@ public SourceHandler(ServerConnectorFuture serverConnectorFuture, String interfa
104113
this.pipeliningEnabled = pipeliningEnabled;
105114
this.pipeliningLimit = pipeliningLimit;
106115
this.pipeliningGroup = pipeliningGroup;
116+
this.httpAccessLogMessages = new ArrayList<>();
107117
}
108118

109119
@SuppressWarnings("unchecked")
@@ -156,6 +166,12 @@ public void channelActive(final ChannelHandlerContext ctx) {
156166
handlerExecutor.executeAtSourceConnectionInitiation(Integer.toString(ctx.hashCode()));
157167
}
158168
this.remoteAddress = ctx.channel().remoteAddress();
169+
if (this.remoteAddress instanceof InetSocketAddress) {
170+
remoteHost = ((InetSocketAddress) this.remoteAddress).getAddress().toString();
171+
if (remoteHost.startsWith("/")) {
172+
remoteHost = remoteHost.substring(1);
173+
}
174+
}
159175
}
160176

161177
@Override
@@ -317,6 +333,10 @@ public ServerConnectorFuture getServerConnectorFuture() {
317333
return serverConnectorFuture;
318334
}
319335

336+
public HttpServerChannelInitializer getServerChannelInitializer() {
337+
return serverChannelInitializer;
338+
}
339+
320340
public ChunkConfig getChunkConfig() {
321341
return chunkConfig;
322342
}
@@ -329,6 +349,10 @@ public String getServerName() {
329349
return serverName;
330350
}
331351

352+
public String getRemoteHost() {
353+
return remoteHost;
354+
}
355+
332356
public void setConnectedState(boolean connectedState) {
333357
this.connectedState = connectedState;
334358
}
@@ -340,4 +364,12 @@ public void removeRequestEntry(HttpCarbonMessage inboundRequestMsg) {
340364
public void resetInboundRequestMsg() {
341365
this.inboundRequestMsg = null;
342366
}
367+
368+
public void addHttpAccessLogMessage(HttpAccessLogMessage httpAccessLogMessage) {
369+
this.httpAccessLogMessages.add(httpAccessLogMessage);
370+
}
371+
372+
public List<HttpAccessLogMessage> getHttpAccessLogMessages() {
373+
return this.httpAccessLogMessages;
374+
}
343375
}

0 commit comments

Comments
 (0)