fix(log): keep access log columns stable

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 21:52:13 +00:00
parent 33d35ed063
commit 4c2a0ffab7
3 changed files with 52 additions and 22 deletions

View file

@ -81,16 +81,12 @@ func (l *accessLogSink) writeLine(kind, ip, user, agent string, rx, tx uint64) {
builder.WriteString(ip)
builder.WriteString(" ")
builder.WriteString(user)
if agent != "" {
builder.WriteString(" ")
builder.WriteString(agent)
}
if rx > 0 || tx > 0 {
builder.WriteString(" rx=")
builder.WriteString(formatUint(rx))
builder.WriteString(" tx=")
builder.WriteString(formatUint(tx))
}
builder.WriteString(" ")
builder.WriteString(agent)
builder.WriteString(" rx=")
builder.WriteString(formatUint(rx))
builder.WriteString(" tx=")
builder.WriteString(formatUint(tx))
builder.WriteByte('\n')
_, _ = l.file.WriteString(builder.String())
}

View file

@ -44,14 +44,52 @@ func TestProxy_AccessLog_WritesLifecycleLines(t *testing.T) {
t.Fatalf("read access log: %v", err)
}
text := string(data)
if !strings.Contains(text, "CONNECT 10.0.0.1 WALLET XMRig/6.21.0") {
if !strings.Contains(text, "CONNECT 10.0.0.1 WALLET XMRig/6.21.0 rx=0 tx=0") {
t.Fatalf("expected CONNECT line, got %q", text)
}
if !strings.Contains(text, "CLOSE 10.0.0.1 WALLET rx=512 tx=4096") {
if !strings.Contains(text, "CLOSE 10.0.0.1 WALLET rx=512 tx=4096") {
t.Fatalf("expected CLOSE line, got %q", text)
}
}
func TestProxy_AccessLog_WritesFixedColumns(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "access.log")
cfg := &Config{
Mode: "nicehash",
Workers: WorkersByRigID,
AccessLogFile: path,
Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},
Pools: []PoolConfig{{URL: "pool.example:3333", Enabled: true}},
}
p, result := New(cfg)
if !result.OK {
t.Fatalf("expected valid proxy, got error: %v", result.Error)
}
miner := &Miner{
ip: "10.0.0.1",
user: "WALLET",
conn: noopConn{},
}
p.events.Dispatch(Event{Type: EventLogin, Miner: miner})
p.events.Dispatch(Event{Type: EventClose, Miner: miner})
p.Stop()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read access log: %v", err)
}
text := string(data)
if !strings.Contains(text, "CONNECT 10.0.0.1 WALLET rx=0 tx=0") {
t.Fatalf("expected CONNECT line with zero counters, got %q", text)
}
if !strings.Contains(text, "CLOSE 10.0.0.1 WALLET rx=0 tx=0") {
t.Fatalf("expected CLOSE line with zero counters, got %q", text)
}
}
type noopConn struct{}
func (noopConn) Read([]byte) (int, error) { return 0, os.ErrClosed }

View file

@ -65,16 +65,12 @@ func (l *AccessLog) writeLine(kind, ip, user, agent string, rx, tx uint64) {
builder.WriteString(ip)
builder.WriteString(" ")
builder.WriteString(user)
if agent != "" {
builder.WriteString(" ")
builder.WriteString(agent)
}
if rx > 0 || tx > 0 {
builder.WriteString(" rx=")
builder.WriteString(strconv.FormatUint(rx, 10))
builder.WriteString(" tx=")
builder.WriteString(strconv.FormatUint(tx, 10))
}
builder.WriteString(" ")
builder.WriteString(agent)
builder.WriteString(" rx=")
builder.WriteString(strconv.FormatUint(rx, 10))
builder.WriteString(" tx=")
builder.WriteString(strconv.FormatUint(tx, 10))
builder.WriteByte('\n')
_, _ = l.f.WriteString(builder.String())
}