Inside a logging service
medium 30 minA 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
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
- 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
criticalA slow logging service makes every request slow.
Wrongawait log_service.send(line)return answerRightbuffer.offer(line) # never waitsreturn answerLogging everything in a request
criticalPasswords and tokens end up in index, archive and queue.
Wronglog("request", body=request.body)Rightlog("request", route="/login", user_id=uid)A new trace id per line
majorNothing can be joined.
Wronglog(message, trace_id=uuid4())Rightlog(message, trace_id=current_trace())Committing before the write
majorA crash in between loses the batch.
Wrongqueue.commit(batch)index.write(batch)Rightindex.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.