Application Metrics

RoadRunner server includes an embedded metrics server based on Prometheus.

Enable Metrics

To enable metrics add metrics section to your configuration:

version: "2.7"

metrics:
  address: localhost:2112

Once complete you can access Prometheus metrics using http://localhost:2112/metrics url.

Make sure to install metrics extension:

composer require spiral/roadrunner-metrics

HTTP metrics

To enable HTTP metrics, add the http_metrics middleware as the left-most middleware.

# for metrics rr_http_request_duration_seconds_bucket, rr_http_request_duration_seconds_sum,
# rr_http_request_duration_seconds_count you can enable middleware http_metrics
http:
  middleware: ["http_metrics"]  
  
metrics:
  address: localhost:2112
  

Application metrics

You can also publish application-specific metrics using an RPC connection to the server. First, you have to register a metric in your configuration file:

version: "2.7"
  
metrics:
  address: localhost:2112
  collect:
    app_metric_counter:
      type: counter
      help: "Application counter."

To send metric from the application:

$metrics = new Spiral\RoadRunner\Metrics\Metrics(
    Spiral\Goridge\RPC\RPC::create(Spiral\RoadRunner\Environment::fromGlobals()->getRPCAddress())
);

$metrics->add('app_metric_counter', 1);

Supported types: gauge, counter, summary, histogram.

Tagged metrics

You can use tagged (labels) metrics to group values:

version: "2.7"

metrics:
  address: localhost:2112
  collect:
    app_type_duration:
      type: histogram
      help: "Application counter."
      labels: ["label_1", "label_2"]

You should specify values for your labels while pushing the metric:

$metrics = new Spiral\RoadRunner\Metrics\Metrics(
    Spiral\Goridge\RPC\RPC::create(Spiral\RoadRunner\Environment::fromGlobals()->getRPCAddress())
);
/**
 * @var array<array-key, string>
 */
$labels = ['label_1_value', 'label_2_value'];

$metrics->add('app_type_duration', 0.5, $labels);

Declare metrics

You can declare metric from PHP application itself:

$metrics->declare(
    'test',
    Spiral\RoadRunner\Metrics\Collector::counter()->withHelp('Test counter')
);
Edit this page