Swoole Server stats

Latest version: pecl install openswoole-4.11.1

Declaration

<?php Swoole\Server->stats(int $mode = \OPENSWOOLE_STATS_DEFAULT): string|array|false

Parameters

mode

The output format

Return

success

If success, it returns an array of data, otherwise it returns false.

Description

Notice: the metircs outputs since v4.9.0 has largely changed and much more metrics are supported.

Get the Open Swoole server metrics within your PHP framework or application.

Moving forward to support the Cloud Native ecosystem, OpenSwoole v4.9.0 started to support metrics in JSON and OpenMetrics formats.

You can observer and check the internal metrics of Open Swoole servers with open source Grafana and Prometheus.

There are three metrics output format supported:

  • \OPENSWOOLE_STATS_DEFAULT - PHP array
  • \OPENSWOOLE_STATS_JSON - JSON format
  • \OPENSWOOLE_STATS_OPENMETRICS - OpenMetrics format

Open Swoole Metrics output example

The array of metrics of the current Open Swoole server:

<?php
Array
(
    [up] => 1
    [version] => OpenSwoole-4.9.0
    [master_pid] => 61790
    [manager_pid] => 61791
    [worker_id] => 0
    [reactor_threads_num] => 4
    [workers_total] => 4
    [workers_idle] => 3
    [task_workers_total] => 4
    [task_workers_idle] => 4
    [tasking_num] => 0
    [user_workers_total] => 1
    [dispatch_total] => 3
    [requests_total] => 2
    [start_time] => 1639922630
    [start_seconds] => 2
    [max_conn] => 256
    [connections_accepted] => 3
    [connections_active] => 3
    [connections_closed] => 0
    [reload_count] => 0
    [reload_last_time] => 1639922630
    [worker_memory_usage] => 2097152
    [worker_vm_object_num] => 13
    [worker_vm_resource_num] => 3
    [coroutine_num] => 1
    [event_workers] => Array
        (
            [0] => Array
                (
                    [worker_id] => 0
                    [pid] => 61796
                    [start_time] => 1639922630
                    [start_seconds] => 2
                    [request_count] => 0
                    [dispatch_count] => 1
                )

            [1] => Array
                (
                    [worker_id] => 1
                    [pid] => 61797
                    [start_time] => 1639922630
                    [start_seconds] => 2
                    [request_count] => 0
                    [dispatch_count] => 0
                )

            [2] => Array
                (
                    [worker_id] => 2
                    [pid] => 61798
                    [start_time] => 1639922630
                    [start_seconds] => 2
                    [request_count] => 2
                    [dispatch_count] => 2
                )

            [3] => Array
                (
                    [worker_id] => 3
                    [pid] => 61799
                    [start_time] => 1639922630
                    [start_seconds] => 2
                    [request_count] => 0
                    [dispatch_count] => 0
                )

        )

    [task_workers] => Array
        (
            [0] => Array
                (
                    [worker_id] => 4
                    [pid] => 61792
                    [start_time] => 1639922630
                    [start_seconds] => 2
                )

            [1] => Array
                (
                    [worker_id] => 5
                    [pid] => 61793
                    [start_time] => 1639922630
                    [start_seconds] => 2
                )

            [2] => Array
                (
                    [worker_id] => 6
                    [pid] => 61794
                    [start_time] => 1639922630
                    [start_seconds] => 2
                )

            [3] => Array
                (
                    [worker_id] => 7
                    [pid] => 61795
                    [start_time] => 1639922630
                    [start_seconds] => 2
                )

        )

    [user_workers] => Array
        (
            [0] => Array
                (
                    [worker_id] => 8
                    [pid] => 61800
                    [start_time] => 1639922630
                    [start_seconds] => 2
                )

        )

    [top_classes] => Array
        (
            [Closure] => 4
            [Swoole\Connection\Iterator] => 2
            [Swoole\Table] => 2
            [Swoole\Process] => 1
            [Swoole\Http\Request] => 1
            [Swoole\Http\Server] => 1
            [Swoole\Server\Port] => 1
            [Swoole\Http\Response] => 1
        )

)

Open Swoole Metrics for OpenMetrics and Prometheus

# TYPE openswoole_info gauge
openswoole_info{version="OpenSwoole-4.9.0"} 1
# TYPE openswoole_up gauge
openswoole_up 1
# TYPE openswoole_reactor_num gauge
openswoole_reactor_threads_num 4
# TYPE openswoole_requests counter
openswoole_requests_total 0
# TYPE openswoole_start_time gauge
openswoole_start_time 1639922085
# TYPE openswoole_max_conn gauge
openswoole_max_conn 256
# TYPE openswoole_coroutine_num gauge
openswoole_coroutine_num 1
# TYPE openswoole_start_seconds gauge
openswoole_start_seconds 2
# TYPE openswoole_workers_total gauge
openswoole_workers_total 4
# TYPE openswoole_workers_idle gauge
openswoole_workers_idle 3
# TYPE openswoole_task_workers_total gauge
openswoole_task_workers_total 4
# TYPE openswoole_task_workers_idle gauge
openswoole_task_workers_idle 4
# TYPE openswoole_user_workers_total gauge
openswoole_user_workers_total 1
# TYPE openswoole_dispatch_total gauge
openswoole_dispatch_total 1
# TYPE openswoole_connections_accepted gauge
openswoole_connections_accepted 2
# TYPE openswoole_connections_active gauge
openswoole_connections_active 2
# TYPE openswoole_connections_closed gauge
openswoole_connections_closed 0
# TYPE openswoole_reload_count gauge
openswoole_reload_count 0
# TYPE openswoole_reload_last_time gauge
openswoole_reload_last_time 1639922085
# TYPE openswoole_worker_vm_object_num gauge
openswoole_worker_vm_object_num 13
# TYPE openswoole_worker_vm_resource_num gauge
openswoole_worker_vm_resource_num 3
# TYPE openswoole_worker_memory_usage gauge
openswoole_worker_memory_usage 2097152
# TYPE openswoole_event_workers_start_time gauge
openswoole_event_workers_start_time{worker_id="0"} 1639922085
openswoole_event_workers_start_time{worker_id="1"} 1639922085
openswoole_event_workers_start_time{worker_id="2"} 1639922085
openswoole_event_workers_start_time{worker_id="3"} 1639922085
# TYPE openswoole_event_workers_start_seconds gauge
openswoole_event_workers_start_seconds{worker_id="0"} 2
openswoole_event_workers_start_seconds{worker_id="1"} 2
openswoole_event_workers_start_seconds{worker_id="2"} 2
openswoole_event_workers_start_seconds{worker_id="3"} 2
# TYPE openswoole_event_workers_dispatch_count gauge
openswoole_event_workers_dispatch_count{worker_id="0"} 0
openswoole_event_workers_dispatch_count{worker_id="1"} 0
openswoole_event_workers_dispatch_count{worker_id="2"} 1
openswoole_event_workers_dispatch_count{worker_id="3"} 0
# TYPE openswoole_event_workers_request_count gauge
openswoole_event_workers_request_count{worker_id="0"} 0
openswoole_event_workers_request_count{worker_id="1"} 0
openswoole_event_workers_request_count{worker_id="2"} 0
openswoole_event_workers_request_count{worker_id="3"} 0
# TYPE openswoole_task_workers_start_time gauge
openswoole_task_workers_start_time{worker_id="4"} 1639922085
openswoole_task_workers_start_time{worker_id="5"} 1639922085
openswoole_task_workers_start_time{worker_id="6"} 1639922085
openswoole_task_workers_start_time{worker_id="7"} 1639922085
# TYPE openswoole_task_workers_start_seconds gauge
openswoole_task_workers_start_seconds{worker_id="4"} 2
openswoole_task_workers_start_seconds{worker_id="5"} 2
openswoole_task_workers_start_seconds{worker_id="6"} 2
openswoole_task_workers_start_seconds{worker_id="7"} 2
# TYPE openswoole_user_workers_start_time gauge
openswoole_user_workers_start_time{worker_id="8"} 1639922085
# TYPE openswoole_user_workers_start_seconds gauge
openswoole_user_workers_start_seconds{worker_id="8"} 2
# EOF
Last updated on March 31, 2022