problem: NativeCall replies are logged only on complete

solution: add to the log at the moment of reply
This commit is contained in:
Igor Artamonov
2021-07-05 21:51:15 -04:00
parent 6cb703ec4e
commit 0d144a44a9
3 changed files with 54 additions and 80 deletions

View File

@@ -110,13 +110,14 @@ class AccessHandler(
.start(headers, call.attributes) .start(headers, call.attributes)
val callWrapper: ServerCall<ReqT, RespT> = OnNativeCallResponse( val callWrapper: ServerCall<ReqT, RespT> = OnNativeCallResponse(
call as ServerCall<BlockchainOuterClass.NativeCallRequest, BlockchainOuterClass.NativeCallReplyItem>, builder call as ServerCall<BlockchainOuterClass.NativeCallRequest, BlockchainOuterClass.NativeCallReplyItem>,
builder,
accessLogWriter
) as ServerCall<ReqT, RespT> ) as ServerCall<ReqT, RespT>
return OnNativeCall( return OnNativeCall(
next.startCall(callWrapper, headers) as ServerCall.Listener<BlockchainOuterClass.NativeCallRequest>, next.startCall(callWrapper, headers) as ServerCall.Listener<BlockchainOuterClass.NativeCallRequest>,
builder) { logs -> builder
accessLogWriter.submit(logs) ) as ServerCall.Listener<ReqT>
} as ServerCall.Listener<ReqT>
} }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@@ -205,29 +206,18 @@ class AccessHandler(
class OnNativeCall( class OnNativeCall(
val next: ServerCall.Listener<BlockchainOuterClass.NativeCallRequest>, val next: ServerCall.Listener<BlockchainOuterClass.NativeCallRequest>,
val builder: EventsBuilder.NativeCall, val builder: EventsBuilder.NativeCall
val done: (List<Events.NativeCall>) -> Unit
) : ForwardingServerCallListener<BlockchainOuterClass.NativeCallRequest>() { ) : ForwardingServerCallListener<BlockchainOuterClass.NativeCallRequest>() {
override fun onMessage(message: BlockchainOuterClass.NativeCallRequest) { override fun onMessage(message: BlockchainOuterClass.NativeCallRequest) {
val chain = message.chain val chain = message.chain
builder.withChain(chain.number) builder.withChain(chain.number)
message.itemsList.forEach { item -> message.itemsList.forEach { item ->
builder.onItem(item) builder.onRequest(item)
} }
super.onMessage(message) super.onMessage(message)
} }
override fun onCancel() {
super.onCancel()
done(builder.build())
}
override fun onComplete() {
super.onComplete()
done(builder.build())
}
override fun delegate(): ServerCall.Listener<BlockchainOuterClass.NativeCallRequest> { override fun delegate(): ServerCall.Listener<BlockchainOuterClass.NativeCallRequest> {
return next return next
} }
@@ -279,11 +269,14 @@ class AccessHandler(
class OnNativeCallResponse( class OnNativeCallResponse(
next: ServerCall<BlockchainOuterClass.NativeCallRequest, BlockchainOuterClass.NativeCallReplyItem>, next: ServerCall<BlockchainOuterClass.NativeCallRequest, BlockchainOuterClass.NativeCallReplyItem>,
val builder: EventsBuilder.NativeCall val builder: EventsBuilder.NativeCall,
val accessLogWriter: AccessLogWriter
) : BaseCallResponse<BlockchainOuterClass.NativeCallRequest, BlockchainOuterClass.NativeCallReplyItem>(next) { ) : BaseCallResponse<BlockchainOuterClass.NativeCallRequest, BlockchainOuterClass.NativeCallReplyItem>(next) {
override fun sendMessage(message: BlockchainOuterClass.NativeCallReplyItem) { override fun sendMessage(message: BlockchainOuterClass.NativeCallReplyItem) {
builder.onItemReply(message) accessLogWriter.submit(
builder.onReply(message)
)
super.sendMessage(message) super.sendMessage(message)
} }
} }

View File

@@ -186,12 +186,13 @@ class EventsBuilder {
class NativeCall : Base<NativeCall>() { class NativeCall : Base<NativeCall>() {
val items = ArrayList<Events.NativeCallItemDetails>() val items = ArrayList<Events.NativeCallItemDetails>()
val replies = HashMap<Int, Events.NativeCallReplyDetails>() val replies = HashMap<Int, Events.NativeCallReplyDetails>()
private var index = 0
override fun getT(): NativeCall { override fun getT(): NativeCall {
return this return this
} }
fun onItem(item: BlockchainOuterClass.NativeCallItem): NativeCall { fun onRequest(item: BlockchainOuterClass.NativeCallItem): NativeCall {
this.items.add( this.items.add(
Events.NativeCallItemDetails( Events.NativeCallItemDetails(
item.method, item.method,
@@ -202,30 +203,20 @@ class EventsBuilder {
return this return this
} }
fun onItemReply(reply: BlockchainOuterClass.NativeCallReplyItem): NativeCall { fun onReply(reply: BlockchainOuterClass.NativeCallReplyItem): Events.NativeCall {
this.replies[reply.id] = Events.NativeCallReplyDetails( val item = items.find { it.id == reply.id }!!
reply.id, return Events.NativeCall(
reply.succeed, request = requestDetails,
reply.payload?.size()?.toLong() ?: 0L total = items.size,
index = index++,
succeed = reply.succeed,
blockchain = chain,
nativeCall = item,
payloadSizeBytes = item.payloadSizeBytes,
id = UUID.randomUUID()
) )
return this
} }
fun build(): List<Events.NativeCall> {
return items.mapIndexed { index, item ->
val reply = replies[item.id]
Events.NativeCall(
request = requestDetails,
total = items.size,
index = index,
succeed = reply?.succeed ?: false,
blockchain = chain,
nativeCall = item,
payloadSizeBytes = item.payloadSizeBytes,
id = UUID.randomUUID()
)
}
}
} }
class Describe : Base<Describe>() { class Describe : Base<Describe>() {

View File

@@ -35,18 +35,15 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 act.request != null
with(act[0]) { act.request.remote != null
it.request != null with(act.request.remote) {
it.request.remote != null ips == ["127.0.0.1"]
with(it.request.remote) { userAgent == "grpc-go/1.30.0"
ips == ["127.0.0.1"] ip == "127.0.0.1"
userAgent == "grpc-go/1.30.0"
ip == "127.0.0.1"
}
} }
} }
@@ -62,11 +59,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
ips == ["30.56.100.15", "127.0.0.1"] ips == ["30.56.100.15", "127.0.0.1"]
ip == "30.56.100.15" ip == "30.56.100.15"
} }
@@ -84,11 +80,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
ips == ["192.168.1.1", "30.56.100.15"] ips == ["192.168.1.1", "30.56.100.15"]
userAgent == "grpc-go/1.30.0" userAgent == "grpc-go/1.30.0"
ip == "30.56.100.15" ip == "30.56.100.15"
@@ -107,11 +102,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
ips == ["30.56.100.15"] ips == ["30.56.100.15"]
userAgent == "grpc-go/1.30.0" userAgent == "grpc-go/1.30.0"
ip == "30.56.100.15" ip == "30.56.100.15"
@@ -130,11 +124,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
ips == ["30.56.100.15"] ips == ["30.56.100.15"]
userAgent == "grpc-go/1.30.0" userAgent == "grpc-go/1.30.0"
ip == "30.56.100.15" ip == "30.56.100.15"
@@ -153,11 +146,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
ips == ["2001:db8:0:0:0:ff00:42:8329", "0:0:0:0:0:0:0:1"] ips == ["2001:db8:0:0:0:ff00:42:8329", "0:0:0:0:0:0:0:1"]
ip == "2001:db8:0:0:0:ff00:42:8329" ip == "2001:db8:0:0:0:ff00:42:8329"
} }
@@ -174,11 +166,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
userAgent == "grpc-go/1.30.0 xss" userAgent == "grpc-go/1.30.0 xss"
} }
} }
@@ -195,11 +186,10 @@ class EventsBaseBuilderSpec extends Specification {
def act = new EventsBuilder.NativeCall() def act = new EventsBuilder.NativeCall()
.start(metadata, attributes) .start(metadata, attributes)
.withChain(Chain.ETHEREUM.id) .withChain(Chain.ETHEREUM.id)
.onItem(BlockchainOuterClass.NativeCallItem.getDefaultInstance()) .onRequest(BlockchainOuterClass.NativeCallItem.getDefaultInstance())
.build() .onReply(BlockchainOuterClass.NativeCallReplyItem.getDefaultInstance())
then: then:
act.size() == 1 with(act.request.remote) {
with(act[0].request.remote) {
userAgent.length() == 128 userAgent.length() == 128
userAgent == "0123456_1_0123456_2_0123456_3_0123456_4_0123456_5_0123456_6_0123456_7_0123456_8_0123456_9_0123456_0_0123456_1_0123456_2_0123456_" userAgent == "0123456_1_0123456_2_0123456_3_0123456_4_0123456_5_0123456_6_0123456_7_0123456_8_0123456_9_0123456_0_0123456_1_0123456_2_0123456_"
} }