Deal with bit rot in the p2p tracing support

This commit is contained in:
Zahary Karadjov 2020-03-18 20:22:32 +02:00
parent b3b5854003
commit 9c442bf65b
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 54 additions and 37 deletions

View File

@ -135,6 +135,7 @@ let
Option {.compileTime.} = ident "Option" Option {.compileTime.} = ident "Option"
Future {.compileTime.} = ident "Future" Future {.compileTime.} = ident "Future"
Void {.compileTime.} = ident "void" Void {.compileTime.} = ident "void"
writeField {.compileTime.} = ident "writeField"
template Opt(T): auto = newTree(nnkBracketExpr, Option, T) template Opt(T): auto = newTree(nnkBracketExpr, Option, T)
template Fut(T): auto = newTree(nnkBracketExpr, Future, T) template Fut(T): auto = newTree(nnkBracketExpr, Future, T)
@ -153,19 +154,24 @@ when tracingEnabled:
fields: openarray[NimNode]): NimNode = fields: openarray[NimNode]): NimNode =
## This generates the tracing code inserted in the message sending procs ## This generates the tracing code inserted in the message sending procs
## `fields` contains all the params that were serialized in the message ## `fields` contains all the params that were serialized in the message
var tracer = ident("tracer") let
tracer = ident "tracer"
tracerStream = ident "tracerStream"
logMsgEventImpl = ident "logMsgEventImpl"
result = quote do: result = quote do:
var `tracer` = init StringJsonWriter var `tracerStream` = init OutputStream
var `tracer` = JsonWriter.init(`tracerStream`)
beginRecord(`tracer`) beginRecord(`tracer`)
for f in fields: for f in fields:
result.add newCall(bindSym"writeField", tracer, newLit($f), f) result.add newCall(writeField, tracer, newLit($f), f)
result.add quote do: result.add quote do:
endRecord(`tracer`) endRecord(`tracer`)
logMsgEventImpl("outgoing_msg", `peer`, `logMsgEventImpl`("outgoing_msg", `peer`,
`protocolInfo`, `msgName`, getOutput(`tracer`)) `protocolInfo`, `msgName`,
getOutput(`tracerStream`, string))
proc createPeerState[Peer, ProtocolState](peer: Peer): RootRef = proc createPeerState[Peer, ProtocolState](peer: Peer): RootRef =
var res = new ProtocolState var res = new ProtocolState
@ -528,7 +534,6 @@ proc writeParamsAsRecord*(params: openarray[NimNode],
outputStream, Format, RecordType: NimNode): NimNode = outputStream, Format, RecordType: NimNode): NimNode =
var var
appendParams = newStmtList() appendParams = newStmtList()
writeField = ident "writeField"
recordWriterCtx = ident "recordWriterCtx" recordWriterCtx = ident "recordWriterCtx"
writer = ident "writer" writer = ident "writer"
@ -584,11 +589,13 @@ proc useStandardBody*(sendProc: SendProc,
sendCall = sendCallGenerator(recipient, msgBytes) sendCall = sendCallGenerator(recipient, msgBytes)
tracing = when tracingEnabled: logSentMsgFields(recipient, tracing = when not tracingEnabled:
newLit(msg.protocol.name), newStmtList()
else:
logSentMsgFields(recipient,
msg.protocol.protocolInfoVar,
$msg.ident, $msg.ident,
sendProc.msgParams) sendProc.msgParams)
else: newStmtList()
sendProc.setBody quote do: sendProc.setBody quote do:
mixin init, WriterType, beginRecord, endRecord, getOutput mixin init, WriterType, beginRecord, endRecord, getOutput

View File

@ -2,9 +2,8 @@ const tracingEnabled = defined(p2pdump)
when tracingEnabled: when tracingEnabled:
import import
macros, typetraits, typetraits,
serialization, json_serialization/writer, json_serialization, chronicles #, chronicles_tail/configuration
chronicles, chronicles_tail/configuration
export export
# XXX: Nim visibility rules get in the way here. # XXX: Nim visibility rules get in the way here.
@ -17,21 +16,25 @@ when tracingEnabled:
# TODO: File this as an issue # TODO: File this as an issue
logStream p2pMessages[json[file(p2p_messages.json,truncate)]] logStream p2pMessages[json[file(p2p_messages.json,truncate)]]
p2pMessages.useTailPlugin "p2p_tracing_ctail_plugin.nim" # p2pMessages.useTailPlugin "p2p_tracing_ctail_plugin.nim"
template logRecord(eventName: static[string], args: varargs[untyped]) = template logRecord(eventName: static[string], args: varargs[untyped]) =
p2pMessages.log LogLevel.NONE, eventName, topics = "p2pdump", args p2pMessages.log LogLevel.NONE, eventName, topics = "p2pdump", args
proc initTracing(baseProtocol: ProtocolInfo, proc initTracing*(baseProtocol: ProtocolInfo,
userProtocols: seq[ProtocolInfo]) = userProtocols: seq[ProtocolInfo]) =
once: once:
var w = init StringJsonWriter var s = init OutputStream
var w = JsonWriter.init(s)
proc addProtocol(p: ProtocolInfo) = proc addProtocol(p: ProtocolInfo) =
w.writeFieldName p.name w.writeFieldName p.name
w.beginRecord() w.beginRecord()
var i = 0
for msg in p.messages: for msg in p.messages:
w.writeField $msg.id, msg.name let msgId = i # msg.id
w.writeField $msgId, msg.name
inc i
w.endRecordField() w.endRecordField()
w.beginRecord() w.beginRecord()
@ -40,55 +43,62 @@ when tracingEnabled:
addProtocol userProtocol addProtocol userProtocol
w.endRecord() w.endRecord()
logRecord "p2p_protocols", data = JsonString(w.getOutput) logRecord "p2p_protocols", data = JsonString(s.getOutput(string))
proc logMsgEventImpl(eventName: static[string], proc logMsgEventImpl*(eventName: static[string],
peer: Peer, peer: Peer,
protocol: ProtocolInfo, protocol: ProtocolInfo,
msgName: string, msgName: string,
json: string) = json: string) =
# this is kept as a separate proc to reduce the code bloat # this is kept as a separate proc to reduce the code bloat
logRecord eventName, port = int(peer.network.address.tcpPort), logRecord eventName, peer = $peer.remote,
peer = $peer.remote,
protocol = protocol.name, protocol = protocol.name,
msg = msgName, msg = msgName,
data = JsonString(json) data = JsonString(json)
proc logMsgEvent[Msg](eventName: static[string], peer: Peer, msg: Msg) = template logMsgEventImpl*(eventName: static[string],
mixin msgProtocol, protocolInfo, msgId responder: Responder,
protocol: ProtocolInfo,
msgName: string,
json: string) =
logMsgEventImpl(eventName, UntypedResponder(responder).peer,
protocol, msgName, json)
proc logMsgEvent[Msg](eventName: static[string], peer: Peer, msg: Msg) =
mixin msgProtocol, protocolInfo, msgId, RecType
type R = RecType(Msg)
logMsgEventImpl(eventName, peer, logMsgEventImpl(eventName, peer,
Msg.msgProtocol.protocolInfo, Msg.msgProtocol.protocolInfo,
Msg.type.name, Msg.type.name,
StringJsonWriter.encode(msg)) Json.encode(R msg))
template logSentMsg(peer: Peer, msg: auto) = template logSentMsg*(peer: Peer, msg: auto) =
logMsgEvent("outgoing_msg", peer, msg) logMsgEvent("outgoing_msg", peer, msg)
template logReceivedMsg(peer: Peer, msg: auto) = template logReceivedMsg*(peer: Peer, msg: auto) =
logMsgEvent("incoming_msg", peer, msg) logMsgEvent("incoming_msg", peer, msg)
template logConnectedPeer(p: Peer) = template logConnectedPeer*(p: Peer) =
logRecord "peer_connected", logRecord "peer_connected",
port = int(p.network.address.tcpPort), port = int(p.network.address.tcpPort),
peer = $p.remote peer = $p.remote
template logAcceptedPeer(p: Peer) = template logAcceptedPeer*(p: Peer) =
logRecord "peer_accepted", logRecord "peer_accepted",
port = int(p.network.address.tcpPort), port = int(p.network.address.tcpPort),
peer = $p.remote peer = $p.remote
template logDisconnectedPeer(p: Peer) = template logDisconnectedPeer*(p: Peer) =
logRecord "peer_disconnected", logRecord "peer_disconnected",
port = int(p.network.address.tcpPort), port = int(p.network.address.tcpPort),
peer = $p.remote peer = $p.remote
else: else:
template initTracing(baseProtocol: ProtocolInfo, template initTracing*(baseProtocol: ProtocolInfo,
userProtocols: seq[ProtocolInfo])= discard userProtocols: seq[ProtocolInfo])= discard
template logSentMsg(peer: Peer, msg: auto) = discard template logSentMsg*(peer: Peer, msg: auto) = discard
template logReceivedMsg(peer: Peer, msg: auto) = discard template logReceivedMsg*(peer: Peer, msg: auto) = discard
template logConnectedPeer(peer: Peer) = discard template logConnectedPeer*(peer: Peer) = discard
template logAcceptedPeer(peer: Peer) = discard template logAcceptedPeer*(peer: Peer) = discard
template logDisconnectedPeer(peer: Peer) = discard template logDisconnectedPeer*(peer: Peer) = discard