-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Metrics | ||
|
||
## Introduction | ||
|
||
In the context of ngx_wasm_module, in accordance with Proxy-Wasm, a metric is | ||
either a counter, a gauge or a histogram. | ||
|
||
A counter is an unsigned 64-bit int that can only be incremented. | ||
A gauge is an unsigned 64-bit int that can take arbitrary values. | ||
|
||
## Histograms | ||
|
||
A histogram, used to represent ranges frequency of a variable, can be defined | ||
as a set of pairs of range and counter. For example, the distribution of | ||
response time of HTTP requests, can be represented as a histogram with ranges | ||
`[0, 16]`, `(16, 128]`, `(128, 1024]` and `(1024, Inf]`. The 1st range's | ||
counter, would be the number of requests whose response time <= 16ms; the 2nd | ||
range's counter, requests whose 16ms < response time <= l28ms; the 3rd range's | ||
counter, requests whose 128ms < response time <= 1024ms; and the last range's | ||
counter, requests whose response time > 1024ms. | ||
|
||
### Binning | ||
|
||
The above example demonstrates a histogram with ranges, or bins, whose upper | ||
bound grows in powers of 2, i.e. 2^4, 2^7 and 2^10. This is usually called | ||
logarithmic binning and is indeed how histograms are implemented in the | ||
ngx_wasm_module. This binning strategy implicates that when a value is recorded, | ||
it's matched with the smalllest upper bound bigger than itself and the paired | ||
counter is incremented. | ||
|
||
### Update and expansion | ||
|
||
Histograms are created with 5 bins, 1 initialized and 4 uninitialized. When a | ||
value is recorded into a histogram, if one of its initialized bins has the upper | ||
bound associated with `v`, then its counter is simply incremented; if not, and | ||
the histogram has uninitialized bins, one of them is initialized with the upper | ||
bound associated with `v` and its counter is incremented; if the histogram's out | ||
of uninitialized bins, it can be expanded, up to 18 bins, to accommodate the | ||
additional bin. If a histogram's reached the limit of bins and it doesn't have | ||
the bin associated with a value, the bin with the smallest upper bound bigger | ||
than `v` have the counter incremented. The bin initialized on histogram creation | ||
has upper bound 2^32 and its counter is incremented if it's the only bin whose | ||
upper bound is bigger than the recorded value. | ||
|
||
|
||
## Memory consumption | ||
|
||
The space in memory occupied by a metric contains its name, value and the | ||
underlying structure representing them in the key-value store. While the | ||
key-value structure has a fixed size of 96 bytes, the sizes of name and value | ||
vary. | ||
|
||
The size in memory of counters and gauges is 8 bytes plus 16 bytes per worker | ||
process. The size of a metric's value grows following the number of workers | ||
because it's segmented across them. Each worker has its own portion of the value | ||
that's only ever updated by itself. When a metric is retrieved, the segments are | ||
consolidated and returned as single metric. This storage strategy allows metric | ||
updates to be performed without the aid of locks at the cost of 16 bytes per | ||
worker. | ||
|
||
Histograms also have a baseline size of 8 bytes, plus 16 bytes per worker. | ||
However, histograms need extra space per worker for bins storage. Each bin takes | ||
8 bytes in memory, so a 5-bin histogram takes 8 bytes plus (16 + 5*8), 56, bytes | ||
per worker. | ||
|
||
As such, in a 4-workers setup, a counter or gauge whose name is 64 chars long | ||
takes 168 bytes, a 5-bin histogram with the same name takes 328 bytes and a | ||
17-bin histogram with the same name takes 712 bytes. | ||
|
||
### Shared memory allocation | ||
|
||
Explain how memory is allocated by ngx shm allocator and how this impacts the | ||
number of metrics that can be represented in memory. | ||
|
||
## Prefixing | ||
|
||
## Nginx Reconfiguration |