Metrics

The tool for tracking service metrics, available at /metrics path.

Introduction

The manager collects and stores metrics as time-series data, which might be used to track service behavior.

The /metrics route and statistics collectors are disabled by default. To enable both, set the LUNA_SERVICE_METRICS.ENABLED parameter to 1 via the configurator or configuration file. Note that all the metrics data is reset on service shutdown.

Metric types

There are 2 default metrics:
  • request_count_total

  • errors_count_total

Each of them has at least 2 labels for sorting:
  • status_code (or error_code for error metrics)

  • path (which consists of the request method and endpoint route)

Note

To add extra labels, specify the pairs of label_name=label_value for config file or {“label_name”: “label_value”} for configurator in LUNA_SERVICE_METRICS.EXTRA_LABELS. Note that the set pair of label_name=label_value will be added to each metric for the running app.

The manager distributes all requests passing through the service among counters using these labels, ensuring that two successful requests sent to different endpoints or to one endpoint but with different status codes will be delivered to different metrics.

Note

Failed requests are distributed to both the request_count_total and request_errors_total metrics.

Metrics format

The only available option (LUNA_SERVICE_METRICS.METRICS_FORMAT property) for collecting and storing metrics at the moment is through the Prometheus toolkit. You might want to refer to its documentation, as it offers a wide range of core functions.

For example:

rate(request_count_total{path="GET:/healthcheck"}[5m])

will get you the average requests per second to “/healthcheck” for the last 5 minutes.

Examples

If you send one request to /healthcheck, followed by three requests to /docs/spec, one of which will be redirected, you will receive the response.text from /metrics route as follow:

# HELP request_count_total Counter of requests
# TYPE request_count_total counter
request_count_total{path="GET:/docs/spec",status_code="200"} 2.0
request_count_total{path="GET:/docs/spec",status_code="301"} 1.0
request_count_total{path="GET:/healthcheck",status_code="200"} 1.0

If you send one invalid POST-request to /handlers route:

# HELP request_count_total Counter of requests
# TYPE request_count_total counter
request_count_total{path="POST:/handlers",status_code="401"} 1.0
# HELP request_errors_total Counter of request errors
# TYPE request_errors_total counter
request_errors_total{error_code="12010",path="POST:/handlers"} 1.0