This module provides remote monitoring of a running process over HTTP. It can be used to run an HTTP server that provides both a web-based user interface and a machine-readable API (e.g. JSON). The former can be used by a human to get an overview of what the program is doing and the latter can be used be automated monitoring tools.
Typical usage is to start the monitoring server at program startup
main = do forkServer "localhost" 8000 ...
and then periodically check the stats using a web browser or a command line tool (e.g. curl)
$ curl -H "Accept: application/json" http://localhost:8000/
To use this module you must first enable GC statistics collection in the run-time system. To enable GC statistics collection, either run your program with
or compile it with
The runtime overhead of
-T is very small so it's safe to always
leave it enabled.
To use the machine-readable REST API, send an HTTP GET request to
the host and port passed to
forkServer. The following resources
(i.e. URLs) are available:
- JSON object containing all counters and gauges. Counters and
gauges are stored as nested objects under the
gaugesattributes, respectively. Content types: "text/html" (default), "application/json"
- Flattened JSON object containing all counters and gauges. Content types: "application/json"
- JSON object containing all counters. Content types: "application/json"
- /counters/<counter name>
- Value of a single counter, as a string. The name should be UTF-8 encoded. Content types: "text/plain"
- JSON object containing all gauges. Content types: "application/json"
- /gauges/<gauge name>
- Value of a single gauge, as a string. The name should be UTF-8 encoded. Content types: "text/plain"
Counters and gauges are stored as attributes of the returned JSON
objects, one attribute per counter or gauge. In addition to
user-defined counters and gauges, the below built-in counters and
gauges are also returned. Furthermore, the top-level JSON object
of any resource contains the
which indicates the server time, in milliseconds, when the sample
- Total number of bytes allocated
- Number of garbage collections performed
- Number of byte usage samples taken
- Sum of all byte usage samples, can be
numByteUsageSamplesto calculate averages with arbitrary weighting (if you are sampling this record multiple times).
- Number of bytes copied during GC
- CPU time spent running mutator threads. This does not include any profiling overhead or initialization.
- Wall clock time spent running mutator threads. This does not include initialization.
- CPU time spent running GC
- Wall clock time spent running GC
- Total CPU time elapsed since program start
- Total wall clock time elapsed since start
- Maximum number of live bytes seen so far
- Current number of live bytes
- Current number of bytes lost to slop
- Maximum number of bytes lost to slop at any one time so far
- Maximum number of megabytes allocated
- Number of bytes copied during GC, minus
space held by mutable lists held by the capabilities. Can be used
parMaxBytesCopiedto determine how well parallel GC utilized all cores.
- Sum of number of bytes copied each GC by
the most active GC thread each GC. The ratio of
parMaxBytesCopiedapproaches 1 for a maximally sequential run and approaches the number of threads (set by the RTS flag
-N) for a maximally parallel run.
The monitoring server
A handle that can be used to control the monitoring server.
The thread ID of the server. You can kill the server by killing this thread (i.e. by throwing it an asynchronous exception.)
Start an HTTP server in a new thread. The server replies to GET requests to the given host and port. The host argument can be either a numeric network address (dotted quad for IPv4, colon-separated hex for IPv6) or a hostname (e.g. "localhost"). The client can control the Content-Type used in responses by setting the Accept header. At the moment three content types are available: "application/json", "text/html", and "text/plain".
User-defined counters and gauges
The monitoring server can store and serve user-defined, integer-valued counters and gauges. A counter is a monotonically increasing value (e.g. TCP connections established since program start). A gauge is a variable value (e.g. the current number of concurrent connections.) Each counter or gauge is associated with a name, which is used when the counter or gauge is displayed in the UI or returned in a JSON object.
Even though it's technically possible to have a counter and a gauge with the same name, associated with the same server, it's not recommended as it might make it harder for clients to distinguish the two.
main = do handle <- forkServer "localhost" 8000 counter <- getCounter "iterations" handle let loop n = do inc counter loop loop