|
| 1 | +===================== |
| 2 | +Open Metrics exporter |
| 3 | +===================== |
| 4 | + |
| 5 | +.. versionadded:: 33.0 |
| 6 | + |
| 7 | +Nextcloud allows to export metrics using `OpenMetrics <https://openmetrics.io/>`_ format. |
| 8 | + |
| 9 | +The data is available on the ``/metrics`` endpoint and can then be imported into any OpenMetrics (formerly Prometheus) enabled tool. |
| 10 | + |
| 11 | + |
| 12 | +Register a new exporter |
| 13 | +----------------------- |
| 14 | + |
| 15 | +Each exporter must be registered inside **<myapp>/appinfo/info.xml**: |
| 16 | + |
| 17 | +.. code-block:: xml |
| 18 | +
|
| 19 | + <openmetrics> |
| 20 | + <exporter>OCA\MyApp\OpenMetrics\CustomExporter</exporter> |
| 21 | + <exporter>OCA\MyApp\OpenMetrics\AnotherExporter</exporter> |
| 22 | + </openmetrics> |
| 23 | +
|
| 24 | +
|
| 25 | +Implement a new exporter |
| 26 | +------------------------ |
| 27 | + |
| 28 | +Then you need to implement it: |
| 29 | + |
| 30 | +.. code-block:: php |
| 31 | +
|
| 32 | + <?php |
| 33 | +
|
| 34 | + declare(strict_types=1) |
| 35 | +
|
| 36 | + namespace OCA\MyApp\OpenMetrics; |
| 37 | +
|
| 38 | + use OCP\OpenMetrics\IMetricFamily; |
| 39 | + use OCP\OpenMetrics\MetricType; |
| 40 | +
|
| 41 | + class CustomExporter implements IMetricFamily { |
| 42 | + public function __construct( |
| 43 | + // Add you dependencies here |
| 44 | + ) { |
| 45 | + } |
| 46 | +
|
| 47 | + #[Override] |
| 48 | + public function name(): string { |
| 49 | + return 'myapp_metric'; |
| 50 | + } |
| 51 | +
|
| 52 | + #[Override] |
| 53 | + public function type(): MetricType { |
| 54 | + // One MetricType::* |
| 55 | + return MetricType::gauge; |
| 56 | + } |
| 57 | +
|
| 58 | + #[Override] |
| 59 | + public function unit(): string { |
| 60 | + return 'units'; |
| 61 | + } |
| 62 | +
|
| 63 | + #[Override] |
| 64 | + public function help(): string { |
| 65 | + return 'Description of metric'; |
| 66 | + } |
| 67 | +
|
| 68 | + #[Override] |
| 69 | + public function metrics(): Generator { |
| 70 | + yield new Metric( |
| 71 | + 42, |
| 72 | + ['label' => 'one value'], |
| 73 | + ); |
| 74 | + yield new Metric( |
| 75 | + 1337, |
| 76 | + ['label' => 'another value'], |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | +
|
| 81 | +This exporter will add something like this on the ``/metrics`` endpoint: |
| 82 | + |
| 83 | +.. code-block:: |
| 84 | +
|
| 85 | + # TYPE nextcloud_myapp_metric gauge |
| 86 | + # UNIT nextcloud_myapp_metric units |
| 87 | + # HELP nextcloud_myapp_metric Description of metric |
| 88 | + nextcloud_myapp_metric{label="one value"} 42 |
| 89 | + nextcloud_myapp_metric{backend="another value"} 1337 |
| 90 | +
|
0 commit comments