Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SourceAddr and TLS metadata to lj.Batch #29

Merged
merged 5 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add connection metadata to `lj.Batch`. [#22](https://github.com/elastic/go-lumber/pull/22)
andrewkroh marked this conversation as resolved.
Show resolved Hide resolved

### Changed

- Require Go 1.17 to use module. [#28](https://github.com/elastic/go-lumber/pull/28)
Expand Down
4 changes: 2 additions & 2 deletions cmd/tst-lj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// server supports all lumberjack protocol versions, which must be explicitely enabled
// from command line. For printing list of known command line flags run:
//
// tst-lj -h
// tst-lj -h
package main

import (
Expand Down Expand Up @@ -75,7 +75,7 @@ func main() {
}()

printLog := func(batch *lj.Batch) bool {
log.Printf("Received batch of %v events\n", len(batch.Events))
log.Printf("Received batch of %v events from %s", len(batch.Events), batch.SourceAddr)
return true
}

Expand Down
19 changes: 15 additions & 4 deletions lj/lj.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@
// Package lj implements common lumberjack types and functions.
package lj

import (
"crypto/tls"
)

// Batch is an ACK-able batch of events as has been received by lumberjack
// server implemenentations. Batches must be ACKed, for the server
// implementations returning an ACK to it's clients.
type Batch struct {
Events []interface{}
ack chan struct{}
ack chan struct{}
TLS *tls.ConnectionState // TLS connection metadata. Nil for non-TLS connections.
SourceAddr string // Source address of the connection.
andrewkroh marked this conversation as resolved.
Show resolved Hide resolved
Events []interface{}
}

// NewBatch creates a new ACK-able batch.
func NewBatch(evts []interface{}) *Batch {
return &Batch{evts, make(chan struct{})}
func NewBatch(evts []interface{}, remoteAddr string, tlsState *tls.ConnectionState) *Batch {
andrewkroh marked this conversation as resolved.
Show resolved Hide resolved
return &Batch{
ack: make(chan struct{}),
TLS: tlsState,
SourceAddr: remoteAddr,
Events: evts,
}
}

// ACK acknowledges a batch initiating propagation of ACK to clients.
Expand Down
27 changes: 18 additions & 9 deletions server/v1/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1

import (
"bufio"
"crypto/tls"
"encoding/binary"
"io"
"net"
Expand All @@ -32,18 +33,26 @@ import (
)

type reader struct {
in *bufio.Reader
conn net.Conn
timeout time.Duration
buf []byte
conn net.Conn
in *bufio.Reader
tlsState *tls.ConnectionState
remoteAddr string
buf []byte
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are confident that it's nearly always the case that the buf is not grown beyond 64 bytes long, maybe make it be a slice of another [64]byte field?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not confident on this so I'm going to leave it as is. But good idea.

timeout time.Duration
}

func newReader(c net.Conn, to time.Duration) *reader {
r := &reader{
in: bufio.NewReader(c),
conn: c,
timeout: to,
buf: make([]byte, 0, 64),
conn: c,
in: bufio.NewReader(c),
remoteAddr: c.RemoteAddr().String(),
buf: make([]byte, 0, 64),
timeout: to,
}

if tlsConn, ok := c.(*tls.Conn); ok {
s := tlsConn.ConnectionState()
r.tlsState = &s
}
return r
}
Expand Down Expand Up @@ -76,7 +85,7 @@ func (r *reader) ReadBatch() (*lj.Batch, error) {
return nil, err
}

return lj.NewBatch(events), nil
return lj.NewBatch(events, r.remoteAddr, r.tlsState), nil
}

func (r *reader) readEvents(in io.Reader, events []interface{}) ([]interface{}, error) {
Expand Down
31 changes: 20 additions & 11 deletions server/v2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v2

import (
"bufio"
"crypto/tls"
"encoding/binary"
"io"
"net"
Expand All @@ -32,22 +33,30 @@ import (
)

type reader struct {
in *bufio.Reader
conn net.Conn
timeout time.Duration
decoder jsonDecoder
buf []byte
conn net.Conn
in *bufio.Reader
tlsState *tls.ConnectionState
decoder jsonDecoder
remoteAddr string
buf []byte
andrewkroh marked this conversation as resolved.
Show resolved Hide resolved
timeout time.Duration
}

type jsonDecoder func([]byte, interface{}) error

func newReader(c net.Conn, to time.Duration, jsonDecoder jsonDecoder) *reader {
r := &reader{
in: bufio.NewReader(c),
conn: c,
timeout: to,
decoder: jsonDecoder,
buf: make([]byte, 0, 64),
conn: c,
in: bufio.NewReader(c),
decoder: jsonDecoder,
remoteAddr: c.RemoteAddr().String(),
buf: make([]byte, 0, 64),
timeout: to,
}

if tlsConn, ok := c.(*tls.Conn); ok {
s := tlsConn.ConnectionState()
r.tlsState = &s
}
return r
}
Expand Down Expand Up @@ -80,7 +89,7 @@ func (r *reader) ReadBatch() (*lj.Batch, error) {
return nil, err
}

return lj.NewBatch(events), nil
return lj.NewBatch(events, r.remoteAddr, r.tlsState), nil
}

func (r *reader) readEvents(in io.Reader, events []interface{}) ([]interface{}, error) {
Expand Down