AlgoViz

Inside a logging service

medium 30 min

A logging service carries the log lines of all programs to one place where people can search them.

See it run

Follow a line from the program to the search page, see how a word is found, what a queue saves, and how lines grow old.

Operations

The pipeline

Seven steps from three services to an alert.

Step 1 of 7

Services write

Loginwrites a lineOrderswrites a lineAPIwrites a lineAgent 1Agent 2Agent 3Kafka queueProcessorElasticsearchSearch pageAlerts

levels

INFO · ERROR · WARN

Three lines on three machines

Login: user logged in, info. Orders: payment failed, error. API: rate limit approaching, warning. Each line has a time, a level and an id.

Step 1: Services write

line  = service.write()batch = agent.collect(100, seconds=1)queue.append(batch)for e in queue.read():    index.write(mask(enrich(parse(e))))hits = index.search("level:ERROR")if rate(hits) > limit: alert()
Gives you
One place to search
Costs
Seconds of delay
Log in to 200 machines
not possible in time

Each arrow may be late. None of them may stop the service.

In the real world

The same inside a logging service doing real jobs. Pick one and step through it.

Somebody turns on debug in one service. It writes 20 times more.

Step 1 of 4

Before

Orders1 MB/sOther services5 MB/sLimit3 MB/s eachKafka queue6 MB/s
Queue load
15%

Calm

Orders writes 1 MB a second, all others 5.

Step 1: Before

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Waiting for the log

    critical

    A slow logging service makes every request slow.

    Wrong
    await log_service.send(line)return answer
    Right
    buffer.offer(line)   # never waitsreturn answer
  • Logging everything in a request

    critical

    Passwords and tokens end up in index, archive and queue.

    Wrong
    log("request", body=request.body)
    Right
    log("request", route="/login", user_id=uid)
  • A new trace id per line

    major

    Nothing can be joined.

    Wrong
    log(message, trace_id=uuid4())
    Right
    log(message, trace_id=current_trace())
  • Committing before the write

    major

    A crash in between loses the batch.

    Wrong
    queue.commit(batch)index.write(batch)
    Right
    index.write(batch)queue.commit(batch)

Interview tips

  • Do

    • Structured JSON lines.
    • Trace and span ids in each.
    • Buffer and batch.
    • Levels used as meant.
    • Retention per kind of log.
  • Do not

    • Log secrets or personal data.
    • Use free text with values inside.
    • Write to the network in the request.
    • Forget what volume costs.
    • Leave out error details and stack traces.
  • LOGS

    • Layers: collect, process, store, query.
    • Ordered: every line has a time.
    • Grouped: service, trace, level.
    • Searchable: inverted index.
  • Numbers

    • 100 lines or 1 second.
    • 50 MB a second is 4.3 TB a day.
    • 180 GB for one hour of buffer.
    • Fresh in 5 s, search in 1 s: goals to discuss.