feat: api-trace
[govpp.git] / core / connection.go
index f3ff964..ee5a06b 100644 (file)
@@ -123,6 +123,8 @@ type Connection struct {
 
        msgControlPing      api.Message
        msgControlPingReply api.Message
+
+       apiTrace *trace // API tracer (disabled by default)
 }
 
 func newConnection(binapi adapter.VppAPI, attempts int, interval time.Duration) *Connection {
@@ -145,6 +147,10 @@ func newConnection(binapi adapter.VppAPI, attempts int, interval time.Duration)
                subscriptions:       make(map[uint16][]*subscriptionCtx),
                msgControlPing:      msgControlPing,
                msgControlPingReply: msgControlPingReply,
+               apiTrace: &trace{
+                       list: make([]*api.Record, 0),
+                       mux:  &sync.Mutex{},
+               },
        }
        binapi.SetMsgCallback(c.msgCallback)
        return c
@@ -480,3 +486,24 @@ func (c *Connection) sendConnEvent(event ConnectionEvent) {
                log.Warn("Connection state channel is full, discarding value.")
        }
 }
+
+// Trace gives access to the API trace interface
+func (c *Connection) Trace() api.Trace {
+       return c.apiTrace
+}
+
+// trace records api message
+func (c *Connection) trace(msg api.Message, chId uint16, t time.Time, isReceived bool) {
+       if atomic.LoadInt32(&c.apiTrace.isEnabled) == 0 {
+               return
+       }
+       entry := &api.Record{
+               Message:    msg,
+               Timestamp:  t,
+               IsReceived: isReceived,
+               ChannelID:  chId,
+       }
+       c.apiTrace.mux.Lock()
+       c.apiTrace.list = append(c.apiTrace.list, entry)
+       c.apiTrace.mux.Unlock()
+}