languages / elixir / usage / tracking-background-jobs

Elixir Usage - Tracking Background Jobs

Note: This tip refers to traditional background jobs backed by a queue. For native Elixir processes we capture the context.runtime.vm_pid automatically. Calls like spawn/1 and Task.async/1 will automatially have their pid included in the context.

For traditional background jobs backed by a queue you'll want to capture relevant job context. This allows you to segement logs by specific jobs, making it easy to debug and monitor your job executions. The most important attribute to capture is the id:

How to use it

1
2
3
4
5
6
%Timber.Contexts.JobContext{queue_name: "my_queue", id: "abcd1234", attempt: 1}
|> Timber.add_context()

Logger.info("Task execution started")
# ...
Logger.info("Task execution completed")

In the console you can search this context with a query like job.id:abcd1234.

How it works

This strategy leverages context. Every log line written contains the job context. When your logs are received by the Timber service, they'll have the context included. The resulting JSON document for an example log will look like:

1
2
3
4
5
6
7
8
9
10
{
  "message": "Task execution started",
  "level": "info",
  "context": {
    "job": {
      "queue_name": "my_queue",
      "id": "abcd1234"
    }
  }
}
Questions?

Chat with us, just click the talk button in the bottom right of this page.

Chat with us
Sign Up

Log better and solve problems faster, today. Install in under a minute.

Sign Up