uwsgi_metrics is a port of the Dropwizard Metrics package to the uWSGI stack. It allows you to use functions such as uwsgi_metrics.timing() to gather application-level metrics:
with timing(__name__, 'my_timer'):
do_some_operation()
You can then invoke the uwsgi_metrics.view() function to get a dictionary of metrics information:
{
"version": "1.1.1",
"counters": {},
"gauges": {},
"histograms": {},
"meters": {},
"timers": {
"my_module.my_timer": {
"count": 22,
"p98": 4.8198699951171875,
"m15_rate": 1.0033118138834103,
"p75": 1.9915103912353516,
"p99": 4.8198699951171875,
"min": 1.4159679412841797,
"max": 4.8198699951171875,
"m5_rate": 1.0098078505715211,
"p95": 4.7961950302124023,
"m1_rate": 1.0454161929696191,
"duration_units": "milliseconds",
"stddev": 0.92399302814991413,
"mean_rate": 1.2074971885928811,
"rate_units": "calls/second",
"p999": 4.8198699951171875,
"p50": 1.649022102355957,
"mean": 1.9796761599454014
}
}
}
You can wire up uwsgi_metrics.view() to an HTTP endpoint so that you can interactively monitor the performance of your production code.
There are a couple of steps required before you can use uwsgi_metrics:
It takes approximately 30us to log a metric on a 2.3GHz Xeon E5. The uwsgi_metrics.timing() context manager adds a further 20us, to give a total of approximately 50us.
As a very rough guideline, you’re probably not going to notice the overhead of logging 10 metrics (0.5ms) during a service call, but you will start to notice the overhead of logging 100 metrics (5ms).
Initialize metrics, must be invoked at least once prior to invoking any other method.
Get a dictionary representation of current metrics.
Record an event’s occurence in a counter:
counter(__name__, 'my_counter')
Record a value in a histogram:
histogram(__name__, 'my_histogram', len(queue))
Record an event rate:
meter(__name__, 'my_meter', 'event_type')
Record a timing delta:
start_time_s = time.time()
do_some_operation()
end_time_s = time.time()
delta_s = end_time_s - start_time_s
delta_ms = delta_s * 1000
timer(__name__, 'my_timer', delta_ms)
Context manager to time a section of code:
with timing(__name__, 'my_timer'):
do_some_operation()