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

Support ltsv #363

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 7 additions & 1 deletion sdk/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ func updateNginxConfigFileConfig(
switch directive.Directive {
case "log_format":
if len(directive.Args) >= 2 {
formatMap[directive.Args[0]] = strings.Join(directive.Args[1:], "")
if directive.Args[0] == "ltsv" {
formatMap[directive.Args[0]] = "ltsv"
} else {
formatMap[directive.Args[0]] = strings.Join(directive.Args[1:], "")
}
}
case "root":
if err := updateNginxConfigFileWithRoot(aux, directive.Args[0], seen, allowedDirectories, directoryMap); err != nil {
Expand Down Expand Up @@ -405,6 +409,8 @@ func updateNginxConfigWithAccessLog(file string, format string, nginxConfig *pro
al.Format = formatMap[format]
} else if format == "" || format == "combined" {
al.Format = predefinedAccessLogFormat
} else if format == "ltsv" {
al.Format = format
} else {
al.Format = ""
}
Expand Down
27 changes: 22 additions & 5 deletions sdk/config_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var files = []string{
"/tmp/testdata/nginx/other/mime.types",
"/tmp/testdata/logs/access1.log",
"/tmp/testdata/logs/access2.log",
"/tmp/testdata/logs/access3.log",
"/tmp/testdata/logs/error.log",
"/tmp/testdata/root/test.html",
"/tmp/testdata/foo/test.html",
Expand Down Expand Up @@ -89,6 +90,12 @@ var accessLogs = &proto.AccessLogs{
Permissions: "0644",
Readable: true,
},
{
Name: "/tmp/testdata/logs/access3.log",
Format: "ltsv",
Permissions: "0644",
Readable: true,
},
},
}

Expand Down Expand Up @@ -122,6 +129,10 @@ var tests = []struct {
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
log_format ltsv 'remote_addr: $remote_addr\t remote_user: $remote_user\t time_local: $time_local\t '
'request: $request\t status:$status\t body_bytes_sent: $body_bytes_sent\t '
'referer: $http_referer\t user_agent: $http_user_agent\t'
'rt: $request_time\t uct: $upstream_connect_time\t uht: $upstream_header_time\t urt: $upstream_response_time';

server_tokens off;
charset utf-8;
Expand Down Expand Up @@ -157,6 +168,7 @@ var tests = []struct {
}

access_log /tmp/testdata/logs/access2.log combined;
access_log /tmp/testdata/logs/access3.log ltsv;

}`,
plusApi: "http://127.0.0.1:80/privateapi",
Expand All @@ -171,7 +183,7 @@ var tests = []struct {
{
Name: "nginx.conf",
Permissions: "0644",
Lines: int32(52),
Lines: int32(58),
},
{
Name: "ca.crt",
Expand Down Expand Up @@ -286,7 +298,7 @@ var tests = []struct {
},
Zconfig: &proto.ZippedFile{
Contents: []uint8{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 1, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0},
Checksum: "b12f45dee53b801dffe5091354a985b16552d5453fdf832df6f34b3b2ef9d2ee",
Checksum: "44370346a326f3eec4d5a7e9554722e4b00a83e9c6f998d03f912a708c24bb39",
RootDirectory: "/tmp/testdata/nginx",
},
},
Expand Down Expand Up @@ -316,6 +328,10 @@ var tests = []struct {
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
log_format ltsv 'remote_addr: $remote_addr\t remote_user: $remote_user\t time_local: $time_local\t '
'request: $request\t status:$status\t body_bytes_sent: $body_bytes_sent\t '
'referer: $http_referer\t user_agent: $http_user_agent\t'
'rt: $request_time\t uct: $upstream_connect_time\t uht: $upstream_header_time\t urt: $upstream_response_time';

server_tokens off;
charset utf-8;
Expand All @@ -340,6 +356,7 @@ var tests = []struct {
}

access_log /tmp/testdata/logs/access2.log combined;
access_log /tmp/testdata/logs/access3.log ltsv;

}`,
plusApi: "http://127.0.0.1:80/stub_status",
Expand All @@ -364,7 +381,7 @@ var tests = []struct {
{
Name: "nginx2.conf",
Permissions: "0644",
Lines: int32(41),
Lines: int32(46),
},
{
Name: "ca.crt",
Expand Down Expand Up @@ -426,7 +443,7 @@ var tests = []struct {
},
Zconfig: &proto.ZippedFile{
Contents: []uint8{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 1, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0},
Checksum: "29fb1bed60766983ba835c80b3f4faf8aae145094e4d0b8b9cf5cb6b2bc3a9c3",
Checksum: "1b6422f8a17527b2e9f255b7362ab7c320cd4a2efea7bff3e402438e5877f00e",
RootDirectory: "/tmp/testdata/nginx",
},
},
Expand Down Expand Up @@ -1112,7 +1129,7 @@ func TestGetErrorAndAccessLogs(t *testing.T) {

func TestGetAccessLogs(t *testing.T) {
result := GetAccessLogs(accessLogs)
assert.Equal(t, []string{"/tmp/testdata/logs/access1.log", "/tmp/testdata/logs/access2.log"}, result)
assert.Equal(t, []string{"/tmp/testdata/logs/access1.log", "/tmp/testdata/logs/access2.log", "/tmp/testdata/logs/access3.log"}, result)
}

func TestGetErrorLogs(t *testing.T) {
Expand Down
22 changes: 15 additions & 7 deletions src/core/metrics/sources/nginx_access_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,22 @@ func (c *NginxAccessLog) logStats(ctx context.Context, logFile, logFormat string
gzipRatios, requestLengths, requestTimes, upstreamResponseLength, upstreamResponseTimes, upstreamConnectTimes, upstreamHeaderTimes := []float64{}, []float64{}, []float64{}, []float64{}, []float64{}, []float64{}, []float64{}

mu := sync.Mutex{}

t, err := tailer.NewPatternTailer(logFile, map[string]string{"DEFAULT": logPattern})
if err != nil {
log.Errorf("unable to tail %q: %v", logFile, err)
return
}
data := make(chan map[string]string, 1024)
go t.Tail(ctx, data)
if logPattern == "ltsv" {
t, err := tailer.NewLTSVTailer(logFile)
if err != nil {
log.Errorf("unable to tail %q: %v", logFile, err)
return
}
go t.Tail(ctx, data)
} else {
t, err := tailer.NewPatternTailer(logFile, map[string]string{"DEFAULT": logPattern})
if err != nil {
log.Errorf("unable to tail %q: %v", logFile, err)
return
}
go t.Tail(ctx, data)
}

tick := time.NewTicker(c.collectionInterval)
defer tick.Stop()
Expand Down
57 changes: 57 additions & 0 deletions src/core/tailer/tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package tailer
import (
"context"
"io"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/nxadm/tail"
Expand Down Expand Up @@ -66,6 +67,11 @@ type PatternTailer struct {
gc *grok.CompiledGrok
}

type LTSVTailer struct {
handle *tail.Tail
ltsvParseString func(line string) map[string]string
}

func NewTailer(file string) (*Tailer, error) {
t, err := tail.TailFile(file, tailConfig)
if err != nil {
Expand Down Expand Up @@ -95,6 +101,28 @@ func NewPatternTailer(file string, patterns map[string]string) (*PatternTailer,
return &PatternTailer{t, gc}, nil
}

func NewLTSVTailer(file string) (*LTSVTailer, error) {
ltsvParseString := func(line string) map[string]string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you move this out into its own function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dhurley I could move out the parse function outside.

columns := strings.Split(line, "\t")
lvs := make(map[string]string)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure what lvs and lv mean. Could you give the variables more meaningful names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dhurley Also Fix those name.

for _, column := range columns {
lv := strings.SplitN(column, ":", 2)
if len(lv) < 2 {
continue
}
label, value := strings.TrimSpace(lv[0]), strings.TrimSpace(lv[1])
lvs[label] = value
}
return lvs
}
t, err := tail.TailFile(file, tailConfig)
if err != nil {
return nil, err
}

return &LTSVTailer{t, ltsvParseString}, nil
}

func (t *Tailer) Tail(ctx context.Context, data chan<- string) {
for {
select {
Expand Down Expand Up @@ -150,3 +178,32 @@ func (t *PatternTailer) Tail(ctx context.Context, data chan<- map[string]string)
}
}
}

func (t *LTSVTailer) Tail(ctx context.Context, data chan<- map[string]string) {
for {
select {
case line := <-t.handle.Lines:
if line == nil {
return
}
if line.Err != nil {
continue
}

l := t.ltsvParseString(line.Text)
if l != nil {
data <- l
}
case <-ctx.Done():
ctxErr := ctx.Err()
switch ctxErr {
case context.DeadlineExceeded:
log.Tracef("Tailer cancelled because deadline was exceeded, %v", ctxErr)
case context.Canceled:
log.Tracef("Tailer forcibly cancelled, %v", ctxErr)
}
log.Tracef("Tailer is done")
return
}
}
}
54 changes: 54 additions & 0 deletions src/core/tailer/tailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,57 @@ T:
os.Remove(accessLogFile.Name())
assert.Equal(t, 1, count)
}

func TestLTSVTailer(t *testing.T) {
accessLogFile, _ := os.CreateTemp(os.TempDir(), "access.log")
logLine := "remote_addr:127.0.0.1\t remote_user:-\t time_local:04/Nov/2020:19:40:38 +0000\t request:GET /500 HTTP/1.1\t status:500\t body_bytes_sent:4\t http_referer:-\t http_user_agent:curl/7.64.1\n"

tailer, err := NewLTSVTailer(accessLogFile.Name())
require.Nil(t, err)

timeoutDuration := time.Millisecond * 300
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
defer cancel()

data := make(chan map[string]string, 100)
go tailer.Tail(ctx, data)

time.Sleep(time.Millisecond * 100)
_, err = accessLogFile.WriteString(logLine)
if err != nil {
t.Fatalf("Error writing data to access log")
}
accessLogFile.Close()

var count int
var res map[string]string
T:
for {
select {
case r := <-data:
res = r
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved
count++
case <-time.After(timeoutDuration):
break T
case <-ctx.Done():
break T
}
}

os.Remove(accessLogFile.Name())
assert.Equal(t, 1, count)
assert.Equal(
t,
map[string]string{
"body_bytes_sent": "4",
"http_referer": "-",
"http_user_agent": "curl/7.64.1",
"remote_addr": "127.0.0.1",
"remote_user": "-",
"request": "GET /500 HTTP/1.1",
"status": "500",
"time_local": "04/Nov/2020:19:40:38 +0000",
},
res,
)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading