liveBook · Manning

Metric

As explained in Build and Orchestrator in Go (From Scratch)

A metric is a measured value describing the state or performance of a worker machine.

A metric is a measured value describing a worker machine’s current state or workload. A manager polls these values to decide where an incoming task can fit, much like assigning new work to the least constrained available worker. section 1.5.5 section 6.0

Useful worker metrics match the resources tasks request: CPU usage as a percentage; total and available memory; and total and available disk space. The worker also tracks its task count, giving the manager another view of current work. section 6.1 section 6.4

Collect metrics as a small pipeline:

  • Read machine-state data. On Linux, /proc exposes CPU, memory, disk, process, and load information as files; /proc/stat and /proc/meminfo supply relevant inputs. section 6.2
  • Refresh stored statistics regularly. A worker can call GetStats, copy in TaskCount, and pause before the next collection cycle. section 6.4
  • Serve the latest snapshot. A GET /stats handler returns the worker’s stored statistics as JSON, alongside task-operation routes. section 6.4 section 6.6

A polling-friendly worker endpoint

A collector updates the worker’s Stats field every 15 seconds, then GET /stats responds with that snapshot. The interval is intentionally slow enough to observe; a busier production system would need more real-time collection. section 6.4

Listing 6.12 The worker’s new CollectStats() method
func (w *Worker) CollectStats() {
   for {
       log.Println("Collecting stats")
       w.Stats = GetStats()
       w.Stats.TaskCount = w.TaskCount
       time.Sleep(15 * time.Second)
   }
}

The collection loop makes the metric lifecycle explicit: sample machine statistics, add task count, store the snapshot, then wait before sampling again. section 6.4

The low-level Linux parsing can be delegated to goprocinfo, which gathers data from /proc. This keeps the worker focused on collecting, retaining, and exposing a scheduling-oriented summary rather than reproducing filesystem parsing. section 6.6

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Builds a periodic collector and JSON endpoint, while deferring a deep explanation of the Linux /proc filesystem.
Examples
Uses a restaurant seating analogy, Linux /proc file output, a CollectStats loop, and a GET /stats route.
What it assumes
Introduces worker metrics from task resource requests, assuming familiarity with the worker API and task operations.
What you can do after
Apply it via goprocinfo and a GET /stats endpoint to expose a worker’s current resource state.
How it connects
Connects CPU, memory, disk, task count, Docker containers, scheduling, HTTP APIs, and JSON.

Also explained in

Compare how 16 books explain metric →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.