Challenge

Problem

DevOps teams analyze application performance by parsing web server logs to identify slow endpoints. This drill teaches you to extract structured data from semi-structured log files using regular expressions with named groups. You'll parse Heroku router logs containing timestamps, HTTP methods, paths, service times, and status codes, then generate CSV output for analysis. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.

Difficulty: Intermediate

Instructions

  1. Read the provided server log file line by line
  2. Parse each log line using regex to extract:
    • Timestamp (e.g., '2012-05-23T20:52:11+00:00')
    • HTTP method (GET, POST, etc.)
    • Request path (e.g., '/users')
    • Service time in ms (e.g., '20ms' -> 20)
    • Status code (e.g., 200, 404, 500)
  3. Skip lines that don't match the expected format
  4. Sort by service time (slowest first)
  5. Return CSV string with headers: Timestamp,Method,Path,Service_Time_ms,Status
  6. Print summary: 'Parsed X requests, average service time: Y ms'

Files

Editable
Read-only

Hints

Hint 1

Use regex with named groups: /(?<name>pattern)/ for cleaner extraction

Hint 2

Match the timestamp at line start: /^(?<timestamp>\S+)/

Hint 3

Extract method with word characters: /(?<method>\w+)/

Hint 4

Parse service time: /service=(?<service>\d+)ms/ then .to_i

Hint 5

Skip non-matching lines with 'next unless match'

Hint 6

Build CSV manually: lines << "#{val1},#{val2},..."

Ruby 3.4

Provided Files (Read-only)

1. Correct summary output

Input:
parse_logs('server.log')
Expected Output:
Parsed 5 requests, average service time: 60 ms

2. CSV has correct headers

Input:
csv = parse_logs('server.log')
puts csv.lines.first.strip
Expected Output:
Parsed 5 requests, average service time: 60 ms
Timestamp,Method,Path,Service_Time_ms,Status

3. Sorted by service time (slowest first)

Input:
csv = parse_logs('server.log')
puts csv.lines[1..3].map { |l| l.split(',')[3] }.join(', ')
Expected Output:
Parsed 5 requests, average service time: 60 ms
150, 80, 45
+ 2 hidden test cases