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

View File

@ -2,9 +2,8 @@ const tracingEnabled = defined(p2pdump)
when tracingEnabled:
import
macros, typetraits,
serialization, json_serialization/writer,
chronicles, chronicles_tail/configuration
typetraits,
json_serialization, chronicles #, chronicles_tail/configuration
export
# XXX: Nim visibility rules get in the way here.
@ -17,21 +16,25 @@ when tracingEnabled:
# TODO: File this as an issue
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]) =
p2pMessages.log LogLevel.NONE, eventName, topics = "p2pdump", args
proc initTracing(baseProtocol: ProtocolInfo,
userProtocols: seq[ProtocolInfo]) =
proc initTracing*(baseProtocol: ProtocolInfo,
userProtocols: seq[ProtocolInfo]) =
once:
var w = init StringJsonWriter
var s = init OutputStream
var w = JsonWriter.init(s)
proc addProtocol(p: ProtocolInfo) =
w.writeFieldName p.name
w.beginRecord()
var i = 0
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.beginRecord()
@ -40,55 +43,62 @@ when tracingEnabled:
addProtocol userProtocol
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,
protocol: ProtocolInfo,
msgName: string,
json: string) =
# this is kept as a separate proc to reduce the code bloat
logRecord eventName, port = int(peer.network.address.tcpPort),
peer = $peer.remote,
logRecord eventName, peer = $peer.remote,
protocol = protocol.name,
msg = msgName,
data = JsonString(json)
proc logMsgEvent[Msg](eventName: static[string], peer: Peer, msg: Msg) =
mixin msgProtocol, protocolInfo, msgId
template logMsgEventImpl*(eventName: static[string],
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,
Msg.msgProtocol.protocolInfo,
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)
template logReceivedMsg(peer: Peer, msg: auto) =
template logReceivedMsg*(peer: Peer, msg: auto) =
logMsgEvent("incoming_msg", peer, msg)
template logConnectedPeer(p: Peer) =
template logConnectedPeer*(p: Peer) =
logRecord "peer_connected",
port = int(p.network.address.tcpPort),
peer = $p.remote
template logAcceptedPeer(p: Peer) =
template logAcceptedPeer*(p: Peer) =
logRecord "peer_accepted",
port = int(p.network.address.tcpPort),
peer = $p.remote
template logDisconnectedPeer(p: Peer) =
template logDisconnectedPeer*(p: Peer) =
logRecord "peer_disconnected",
port = int(p.network.address.tcpPort),
peer = $p.remote
else:
template initTracing(baseProtocol: ProtocolInfo,
template initTracing*(baseProtocol: ProtocolInfo,
userProtocols: seq[ProtocolInfo])= discard
template logSentMsg(peer: Peer, msg: auto) = discard
template logReceivedMsg(peer: Peer, msg: auto) = discard
template logConnectedPeer(peer: Peer) = discard
template logAcceptedPeer(peer: Peer) = discard
template logDisconnectedPeer(peer: Peer) = discard
template logSentMsg*(peer: Peer, msg: auto) = discard
template logReceivedMsg*(peer: Peer, msg: auto) = discard
template logConnectedPeer*(peer: Peer) = discard
template logAcceptedPeer*(peer: Peer) = discard
template logDisconnectedPeer*(peer: Peer) = discard