Skip to content

Commit 46c34da

Browse files
rubysclaude
andcommitted
Fix JSON log attribute consistency and double encoding
- Change tenant_name to tenant in AccessLogEntry for consistency - Improve JSONLogWriter to detect and pass through Rails JSON logs directly - Prevent double JSON encoding of application logs - Maintain consistency across navigator access logs and Rails app logs The JSONLogWriter now checks if incoming logs are already JSON formatted with @timestamp and severity fields (indicating Rails JSON output) and passes them through directly instead of wrapping them again. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f93aa6d commit 46c34da

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

cmd/navigator/main.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ type LogEntry struct {
400400
}
401401

402402
// JSONLogWriter writes structured JSON log entries
403+
// It can pass through JSON from the application or wrap plain text in JSON
403404
type JSONLogWriter struct {
404405
source string
405406
stream string
@@ -414,6 +415,24 @@ func (w *JSONLogWriter) Write(p []byte) (n int, err error) {
414415
if len(line) == 0 {
415416
continue
416417
}
418+
419+
// Check if the line is already valid JSON from the Rails app
420+
// Rails JSON logs should have @timestamp, severity, message, source, tenant fields
421+
var existingEntry map[string]interface{}
422+
if err := json.Unmarshal(line, &existingEntry); err == nil {
423+
// It's valid JSON - check if it looks like our Rails JSON format
424+
if _, hasTimestamp := existingEntry["@timestamp"]; hasTimestamp {
425+
if _, hasSeverity := existingEntry["severity"]; hasSeverity {
426+
// This looks like Rails JSON output - pass it through directly
427+
// Just ensure it has proper newline
428+
w.output.Write(line)
429+
w.output.Write([]byte("\n"))
430+
continue
431+
}
432+
}
433+
}
434+
435+
// Not JSON or not Rails JSON format - wrap it in our JSON structure
417436
entry := LogEntry{
418437
Timestamp: time.Now().Format(time.RFC3339),
419438
Source: w.source,
@@ -503,7 +522,7 @@ type AccessLogEntry struct {
503522
Referer string `json:"referer"`
504523
UserAgent string `json:"user_agent"`
505524
FlyRequestID string `json:"fly_request_id"`
506-
TenantName string `json:"tenant_name,omitempty"`
525+
Tenant string `json:"tenant,omitempty"`
507526
}
508527

509528
// logTenantRequest logs a tenant request in JSON format matching nginx log format
@@ -551,7 +570,7 @@ func logTenantRequest(r *http.Request, recorder *responseRecorder, tenantName st
551570
Referer: referer,
552571
UserAgent: userAgent,
553572
FlyRequestID: r.Header.Get("Fly-Request-Id"),
554-
TenantName: tenantName,
573+
Tenant: tenantName,
555574
}
556575

557576
data, _ := json.Marshal(entry)

0 commit comments

Comments
 (0)