Skip to content

Commit 270a3ae

Browse files
committed
docs: document how to write a custom exporter (close #28)
1 parent cb386ea commit 270a3ae

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,33 @@ It should show the default values we defined for all options.
443443

444444
🚧 TODO
445445

446+
### Writing a custom exporter sub-plugin (optional)
447+
448+
Out of the box `tool_monitoring` only comes with a Prometheus [exporter](#exporter).
449+
If you are using a different monitoring backend, you will need to provide a custom [exporter sub-plugin](#exporter-sub-plugins).
450+
451+
How exactly such a plugin should look depends heavily on the specifics of your monitoring backend.
452+
For example, since Prometheus is a pull-based monitoring backend, the exporter provides a route/endpoint for Prometheus to periodically query.
453+
454+
In general, the only requirements for an exporter sub-plugin are
455+
- the `version.php` as well as a minimal language file (as with any other plugin),
456+
- and that the plugin is placed in the `exporter/` directory of `tool_monitoring`.
457+
458+
There is no common exporter interface, so you have maximum flexibility in the rest of the setup.
459+
If you have admin settings to configure, a `settings.php` script placed in the plugin directory will have access to a dedicated `admin_settingpage` via the `$settings` variable.
460+
That page will be added under _Plugins_ > _Admin tools_ > _Monitoring_ > _Available Exporters_ and named the same as the sub-plugin.
461+
462+
Since any exporter will need access to the actual metrics available in the system, at some point it should probably make use of the [`metrics_manager`][. metrics_manager].
463+
Instantiating it and calling its `fetch` method will be enough in most cases.
464+
That will store all [`registered_metric`][. registered_metric] instances in its `metrics` property.
465+
466+
To calculate and retrieve the current value(s) of a given metric, the associated `registered_metric` instance just needs to be iterated over.
467+
Iteration will yield the [`metric_value`][. metric_value] objects.
468+
469+
> [!TIP]
470+
> You can look at how the Prometheus exporter does this in its [`exporter`][. exporter] class.
471+
> Its `export` method receives the `metrics_manager::$metrics` array as an argument after the route controller called `fetch` on the manager.
472+
446473
## Terminology
447474

448475
### Metric
@@ -544,7 +571,8 @@ For convenience, the static `metric::collect` method can be used as the [hook ca
544571

545572
To allow all metrics to be individually enabled/disabled and more [advanced metrics](#configurable-metrics-advanced) to have their own persistent configuration, each concrete metric is associated with a row in the `tool_monitoring_metrics` database table.
546573

547-
The `registered_metric` class is an internal wrapper for metrics managed by `tool_monitoring` and maps instances to rows in the database table.
574+
The [`registered_metric`][. registered_metric] class is a wrapper for metrics managed by `tool_monitoring` and maps instances to rows in the database table.
575+
It implements the `IteratorAggregate` interface and iterating over an instance will call the `calculate` method of the underlying `metric` and pass through the value(s).
548576

549577
### Central `metrics_manager`
550578

@@ -590,13 +618,15 @@ You should have received a copy of the GNU General Public License along with `to
590618

591619
**Code, tests, and documentation written by and for humans.** 🚫🤖
592620

621+
[. exporter]: exporter/prometheus/classes/exporter.php
593622
[. hook/metric_collection]: classes/hook/metric_collection.php
594623
[. metric]: classes/metric.php
595624
[. metric_config]: classes/metric_config.php
596625
[. metric_type]: classes/metric_type.php
597626
[. metric_value]: classes/metric_value.php
598627
[. metric_with_config]: classes/metric_with_config.php
599628
[. metrics_manager]: classes/metrics_manager.php
629+
[. registered_metric]: classes/registered_metric.php
600630
[. simple_metric_config]: classes/simple_metric_config.php
601631
[grafana oss home]: https://grafana.com/oss/grafana
602632
[moodle docs blocks]: https://docs.moodle.org/en/Blocks

0 commit comments

Comments
 (0)