Skip to content

Commit

Permalink
Add timestamp to protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
gfr10598 committed Jun 7, 2018
1 parent fcef0fc commit 841fc3e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions inetdiag/inetdiag.go
Expand Up @@ -236,6 +236,7 @@ type ParsedMessage struct {

// Parse parsed the NetlinkMessage into a ParsedMessage. If skipLocal is true, it will return nil for
// loopback, local unicast, multicast, and unspecified connections.
// Note that Parse does not populate the Timestamp field, so caller should do so.
func Parse(msg *syscall.NetlinkMessage, skipLocal bool) (*ParsedMessage, error) {
if msg.Header.Type != 20 {
return nil, ErrNotType20
Expand Down
3 changes: 3 additions & 0 deletions nl-proto/tcpinfo.proto
Expand Up @@ -306,4 +306,7 @@ message TCPDiagnosticsProto {
oneof shutdown {
uint32 shutdown_mask = 10;
}

// Timestamp of batch of messages containing this message.
int64 timestamp = 11;
}
4 changes: 3 additions & 1 deletion nl-proto/tools/convert.go
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"log"
"syscall"
"time"
"unsafe"

"github.com/m-lab/tcp-info/inetdiag"
Expand Down Expand Up @@ -91,7 +92,7 @@ func AttrToField(all *tcpinfo.TCPDiagnosticsProto, rta *syscall.NetlinkRouteAttr

// CreateProto creates a fully populated TCPDiagnosticsProto from the parsed elements of a type 20 netlink message.
// This assumes the netlink message is type 20, and behavior is undefined if it is not.
func CreateProto(header syscall.NlMsghdr, idm *inetdiag.InetDiagMsg, attrs []*syscall.NetlinkRouteAttr) *tcpinfo.TCPDiagnosticsProto {
func CreateProto(time time.Time, header syscall.NlMsghdr, idm *inetdiag.InetDiagMsg, attrs []*syscall.NetlinkRouteAttr) *tcpinfo.TCPDiagnosticsProto {
all := tcpinfo.TCPDiagnosticsProto{}
all.InetDiagMsg = HeaderToProto(idm)
for i := range attrs {
Expand All @@ -100,6 +101,7 @@ func CreateProto(header syscall.NlMsghdr, idm *inetdiag.InetDiagMsg, attrs []*sy
}
}

all.Timestamp = time.UnixNano()
return &all
}

Expand Down
3 changes: 2 additions & 1 deletion nl-proto/tools/convert_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"syscall"
"testing"
"time"

"github.com/go-test/deep"
"github.com/m-lab/tcp-info/inetdiag"
Expand Down Expand Up @@ -49,7 +50,7 @@ func convertToProto(msg *syscall.NetlinkMessage, t *testing.T) *tcpinfo.TCPDiagn
if err != nil {
t.Fatal(err)
}
return tools.CreateProto(msg.Header, parsedMsg.InetDiagMsg, parsedMsg.Attributes[:])
return tools.CreateProto(time.Now(), msg.Header, parsedMsg.InetDiagMsg, parsedMsg.Attributes[:])
}

func TestReader(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion play.go
Expand Up @@ -74,7 +74,7 @@ func Marshal(filename string, marshaler chan *inetdiag.ParsedMessage, wg *sync.W
if !ok {
break
}
p := tools.CreateProto(msg.Header, msg.InetDiagMsg, msg.Attributes[:])
p := tools.CreateProto(msg.Timestamp, msg.Header, msg.InetDiagMsg, msg.Attributes[:])
if false {
log.Printf("%+v\n", p.InetDiagMsg)
log.Printf("%+v\n", p.TcpInfo)
Expand Down
2 changes: 1 addition & 1 deletion saver/saver.go
Expand Up @@ -61,7 +61,7 @@ func runMarshaller(taskChan <-chan Task, wg *sync.WaitGroup) {
log.Fatal("Nil writer")
}
msg := task.Message
pb := tools.CreateProto(msg.Header, msg.InetDiagMsg, msg.Attributes[:])
pb := tools.CreateProto(msg.Timestamp, msg.Header, msg.InetDiagMsg, msg.Attributes[:])
wire, err := proto.Marshal(pb)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit 841fc3e

Please sign in to comment.