By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
relay analyzeRelay's first deliverable is a command-line tool that reads a JSONL event log and prints a summary. A JSONL file is one JSON object per line: no wrapping array, no trailing commas, easy to stream and easy to append. Your tool must parse it defensively, aggregate it usefully and fail with a meaningful exit code when the data is bad.
internal/analyze with an Event struct and a JSON decoder for one line.internal/report with deterministic aggregation: counts by service, by level and by status class (2xx, 3xx, 4xx, 5xx).cmd/relay with flags --file, --top and --json; wire the analyzer to os.Stdout and errors to os.Stderr.-race test for a concurrent parse variant.The listing below is a trimmed, self-contained version of the aggregation path. It reads from a string instead of a file, but the decoder, the error accounting, the deterministic sort and the exit-code decision are the ones your project needs.
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"slices"
"strings"
)
type event struct {
Service string `json:"service"`
Level string `json:"level"`
Status int `json:"status"`
}
func main() {
const input = `{"service":"api","level":"info","status":200}
{"service":"api","level":"error","status":500}
{"service":"worker","level":"warn","status":429}
not json
`
byService := map[string]int{}
invalid := 0
total := 0
scanner := bufio.NewScanner(strings.NewReader(input))
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
total++
var e event
if err := json.Unmarshal([]byte(line), &e); err != nil || e.Service == "" {
invalid++
continue
}
byService[e.Service]++
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "read:", err)
os.Exit(2)
}
services := make([]string, 0, len(byService))
for name := range byService {
services = append(services, name)
}
slices.Sort(services)
for _, name := range services {
fmt.Printf("%s %d\n", name, byService[name])
}
fmt.Printf("total %d invalid %d\n", total, invalid)
if total > 0 && invalid*100 > total*5 {
os.Exit(1)
}
}# The fixture the project ships with.
cat > testdata/events.jsonl <<'EOF'
{"service":"api","level":"info","status":200}
{"service":"api","level":"error","status":500}
{"service":"worker","level":"warn","status":429}
EOF
# Text report, top three services.
go run ./cmd/relay analyze --file testdata/events.jsonl --top 3
# The same data as JSON for a dashboard to consume.
go run ./cmd/relay analyze --file testdata/events.jsonl --json
# The checks you must keep green.
go vet ./...
go test ./... -race| Deliverable | Acceptance check |
|---|---|
internal/analyze | ParseLine rejects invalid JSON and empty services with wrapped errors |
internal/report | Output is sorted; the same input always produces the same bytes |
cmd/relay | Exit codes 0, 1 and 2 are covered by tests and documented in --help |
| Tests | go test ./... -race passes; parser table tests cover empty, valid and broken lines |
| Golden file | testdata/report.golden is compared byte for byte |
| Docs | A short README shows the three commands above |
Key takeaways
The CLI is a thin main package over testable internal/ packages.
Stream JSONL with bufio.Scanner and raise the buffer limit for long lines.
Count invalid lines instead of aborting on the first bad record; report them in the summary.
Choose deterministic ordering before writing the report so golden tests are stable.
Exit codes are part of the interface: 0 success, 1 data problem, 2 usage error.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.