run-as-root/magento2-prometheus-exporter

Magento2 Prometheus Exporter

3.2.2 2024-11-12 11:41 UTC

This package is auto-updated.

Last update: 2024-11-12 11:42:31 UTC


README

Branch stable codecov pipeline status

This Magento 2 Module exposes a new route under /metrics with Magento 2 specific metrics in the format of prometheus. The different metrics are grouped into modules and can be enabled/disabled via the Magento Backend.

Installation

Install the Module via composer by running:

composer require run-as-root/magento2-prometheus-exporter
php bin/magento setup:upgrade

Module Configuration

The modules system configuration is located under Stores -> Configuration -> Prometheus -> Metric Configuration. You can enable or disable specific metrics by using the multiselect.

Prometheus Configuration

After installing the Magento Module, your Prometheus needs to get pointed to your Magento Metrics endpoint. To do so, add the following lines to your prometheus.yml under scrape_configs:

- job_name: 'Magento 2 Exporter'
  scrape_interval: 5m
  scrape_timeout: 60s
  metrics_path: /metrics
  # The bearer token can be generated in the Magento Admin. Using the auth is optional.
  # bearer_token: YOUR_BEARER_TOKEN
  static_configs:
  - targets: 
    - your-magento-url

Authorization

Your metrics endpoint should not be available to everyone, so the use of authorization is recommended. The module provides support for authorization tokens via Magento Backend.

For Basic or Bearer authorization, the scrape job should be extended like this:

- job_name: 'Magento 2 Exporter'
  [..]
  authorization:
    type: 'Bearer'
    credentials: "{{ magento2_metrics_password }}"

Module functionality

The module registers a cron job that runs every minute. The cronjob is responsible for aggregating the metric data. The aggregated data is stored in the table run_as_root_prometheus_metrics. The added controller collects the data stored in the table and renders the correct response for prometheus.

Metrics

The following metrics will be collected:

Add your own Metric

To add a new metric, you need to implement the \RunAsRoot\PrometheusExporter\Api\MetricAggregatorInterface. The metric aggregator object is responsible for collecting the necessary information for the specific metric from magento and then add a new metric record. New records can be easily added via \RunAsRoot\PrometheusExporter\Service\UpdateMetricService.

In addition to the implementation of the MetricAggregatorInterface, you have to add your specific Aggregator to the MetricAggregatorPool defined in the di.xml. For example:

<type name="RunAsRoot\PrometheusExporter\Metric\MetricAggregatorPool">
    <arguments>
        <argument name="items" xsi:type="array">
            <item name="OrderAmountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\Order\OrderAmountAggregator</item>
            <item name="OrderCountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\Order\OrderCountAggregator</item>
            <item name="OrderItemAmountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\Order\OrderItemAmountAggregator</item>
            <item name="OrderItemCountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\Order\OrderItemCountAggregator</item>
        </argument>
    </arguments>
</type>

Contribution

If you have something to contribute, weither it's a feature, a feature request, an issue or something else, feel free to. There are no contribution guidelines.