|
| 1 | +--- |
| 2 | +title: Open Telemetry |
| 3 | +tags: |
| 4 | + - telemetry |
| 5 | +--- |
| 6 | + |
| 7 | +<Since version="5.2" /> |
| 8 | + |
| 9 | +Moodle added support for Open Telemetry in version 5.2. This allows Moodle to collect telemetry data and send it to a telemetry backend for analysis and monitoring. |
| 10 | + |
| 11 | +The Moodle Open Telemetry integration sits outside of Moodle core and follows the same pattern as other Open Telemetry integrations for PHP. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +To install the Open Telemetry integration for Moodle, you can use Composer to require the `moodlehq/moodle-package-otel` package. |
| 16 | + |
| 17 | +In addition to the Moodle Open Telemetry integration, you will also need to set up the following components: |
| 18 | + |
| 19 | +- The [PHP Open Telemetry Extension](https://github.com/open-telemetry/opentelemetry-php-instrumentation), which provides the necessary instrumentation for collecting telemetry data from Moodle. |
| 20 | +- An Open Telemetry Transport, such as HTTP (included) or [open-telemetry/transport-grpc](https://packagist.org/packages/open-telemetry/transport-grpc) |
| 21 | +- An Open Telemetry Exporter, such as [open-telemetry/exporter-otlp](https://packagist.org/packages/open-telemetry/exporter-otlp) |
| 22 | +- An Open Telemetry Collector, which is responsible for receiving telemetry data from Moodle and forwarding it to the telemetry backend. The Open Telemetry Collector can be deployed as a standalone service or as part of a larger monitoring infrastructure. |
| 23 | +- A telemetry backend that supports the Open Telemetry protocol, such as [SigNoz](https://signoz.io/), [Jaeger](https://www.jaegertracing.io/), [Zipkin](https://zipkin.io/), or [Prometheus](https://prometheus.io/), to receive and analyze the telemetry data collected from Moodle. |
| 24 | + |
| 25 | +## What is included |
| 26 | + |
| 27 | +The Moodle Open Telemetry integration includes the following features: |
| 28 | + |
| 29 | +- The creation of a root span for all: |
| 30 | + - web requests |
| 31 | + - CLI commands |
| 32 | +- Specific instrumentation for: |
| 33 | + - the Moodle Tasks API (Scheduled and Adhoc tasks) (`moodlelms.cronlistener`); |
| 34 | + - Moodle events (`moodlelms.eventlistener`); |
| 35 | + - Web Service function calls (`moodlelms.externalapilistener`). |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +The Moodle Open Telemetry integration can be configured using the PHP configuration file (php.ini) or by setting environment variables (for example in your web server). |
| 40 | + |
| 41 | +The current Moodle Open Telemetry integration can be disabled by setting the `OTEL_PHP_DISABLED_INSTRUMENTATIONS` setting to `moodlelms`, for example: |
| 42 | + |
| 43 | +```ini |
| 44 | +OTEL_PHP_DISABLED_INSTRUMENTATIONS=moodlelms |
| 45 | +``` |
| 46 | + |
| 47 | +This will disable the Moodle Open Telemetry integration, whilst allowing other Open Telemetry integrations to continue collecting telemetry data and sending it to the telemetry backend. |
| 48 | + |
| 49 | +Individual Moodle instrumentations can be disabled by setting the `OTEL_PHP_DISABLED_INSTRUMENTATIONS` setting to the specific instrumentation name, for example: |
| 50 | + |
| 51 | +```ini |
| 52 | +OTEL_PHP_DISABLED_INSTRUMENTATIONS=moodlelms.cronlistener |
| 53 | +``` |
| 54 | + |
| 55 | +Multiple instrumentations can be disabled by separating the instrumentation names with a comma, for example: |
| 56 | + |
| 57 | +```ini |
| 58 | +OTEL_PHP_DISABLED_INSTRUMENTATIONS=moodlelms.cronlistener,moodlelms.eventlistener |
| 59 | +``` |
| 60 | + |
| 61 | +## Writing additional instrumentation |
| 62 | + |
| 63 | +When writing additional instrumentation for Moodle there are two options: |
| 64 | + |
| 65 | +- write a standard Open Telemetry instrumentation package for PHP, which can be used in any PHP application, including Moodle. This would involve creating a new package that follows the Open Telemetry instrumentation guidelines and can be installed via Composer. |
| 66 | +- write a Moodle-specific instrumentation package that extends the base class provided by the Moodle Open Telemetry integration. This would involve creating a new package that extends the base class provided by the Moodle Open Telemetry integration and can be installed via Composer. |
| 67 | + |
| 68 | +### Moodle-specific instrumentation |
| 69 | + |
| 70 | +To create your own Moodle-specific instrumentation, you should: |
| 71 | + |
| 72 | +- specify a package type of `moodle-package-otelhook`; |
| 73 | +- define your listeners with each listener implementing the `Moodlehq\MoodlePackageOtel\Instrumentation\MoodleListenerInterface` interface and optionally using the `Moodlehq\MoodlePackageOtel\Instrumentation\MoodleListenerTrait` trait |
| 74 | +- define a `\Namespace\Instrumentation\ListenersDescriber` class which implements `Moodlehq\MoodlePackageOtel\Instrumentation\ListenersDescriberInterface` |
| 75 | +- define your listeners in the `ListenersDescriber` class |
0 commit comments