Type
magento2-module
Magento2 Prometheus Exporter
magento2-module
MIT
None
None
None
None
A comprehensive Magento 2 module that exposes essential Magento metrics in Prometheus format, enabling powerful monitoring and observability for your e-commerce platform.
composer require run-as-root/magento2-prometheus-exporter
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
After installation, your metrics endpoint will be available at:
https://your-magento-store.com/metrics
Navigate to Stores → Configuration → Prometheus → Metric Configuration to:
The module automatically registers a cron job that runs every minute to aggregate metrics. The job uses a dedicated cron group: run_as_root_prometheus_metrics_aggregator.
Add the following scrape configuration to your prometheus.yml:
scrape_configs:
- job_name: 'magento-2'
scrape_interval: 5m
scrape_timeout: 60s
metrics_path: /metrics
static_configs:
- targets:
- 'your-magento-store.com'
For production environments, secure your metrics endpoint:
scrape_configs:
- job_name: 'magento-2'
scrape_interval: 5m
scrape_timeout: 60s
metrics_path: /metrics
authorization:
type: 'Bearer'
credentials: 'your-bearer-token-here'
static_configs:
- targets:
- 'your-magento-store.com'
scrape_configs:
- job_name: 'magento-2'
scrape_interval: 5m
scrape_timeout: 60s
metrics_path: /metrics
authorization:
type: 'Bearer'
credentials: 'your-bearer-token-here'
static_configs:
- targets:
- 'your-magento-store.com'
relabel_configs:
- source_labels: [__address__]
target_label: instance
replacement: 'magento-production'
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_orders_count_total |
gauge | status, store_code |
Total count of orders by status and store |
magento_orders_amount_total |
gauge | status, store_code |
Total revenue by order status and store |
magento_order_items_count_total |
gauge | status, store_code |
Total count of order items |
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_products_by_type_count_total |
gauge | product_type |
Count of products by type (simple, configurable, etc.) |
magento_catalog_category_count_total |
gauge | status, menu_status, store_code |
Count of categories by status |
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_eav_attribute_count_total |
gauge | - | Total count of EAV attributes |
magento_eav_attribute_options_above_recommended_level_total |
gauge | - | Count of attributes with more than 100 options (performance risk) |
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_customer_count_total |
gauge | store_code |
Total count of registered customers |
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_cms_block_count_total |
gauge | store_code |
Count of CMS blocks |
magento_cms_page_count_total |
gauge | store_code |
Count of CMS pages |
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_cronjob_count_total |
gauge | status, job_code |
Count of cron jobs by status |
magento_cronjob_broken_count_total |
gauge | - | Count of broken cron jobs |
magento_indexer_backlog_count_total |
gauge | title |
Count of items in indexer backlog |
| Metric | Type | Labels | Description |
|---|---|---|---|
magento_store_count_total |
gauge | status |
Count of stores by status |
magento_website_count_total |
gauge | - | Total count of websites |
magento_shipments_count_total |
counter | source, store_code |
Count of shipments created |
Consider restricting access to your Prometheus server IPs:
location /metrics {
allow 10.0.0.0/8; # Your Prometheus server
allow 172.16.0.0/12; # Internal network
deny all;
try_files $uri $uri/ /index.php?$args;
}
<?php
namespace YourNamespace\YourModule\Aggregator;
use RunAsRoot\PrometheusExporter\Api\MetricAggregatorInterface;
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService;
class CustomMetricAggregator implements MetricAggregatorInterface
{
private UpdateMetricService $updateMetricService;
public function __construct(UpdateMetricService $updateMetricService)
{
$this->updateMetricService = $updateMetricService;
}
public function aggregate(): void
{
// Your metric collection logic here
$value = $this->calculateCustomMetric();
$this->updateMetricService->update(
'your_custom_metric_total',
(string) $value,
'gauge',
'Description of your custom metric',
['label1' => 'value1', 'label2' => 'value2']
);
}
private function calculateCustomMetric(): int
{
// Implement your metric calculation
return 42;
}
}
etc/di.xml):<type name="RunAsRoot\PrometheusExporter\Metric\MetricAggregatorPool">
<arguments>
<argument name="items" xsi:type="array">
<item name="YourCustomAggregator" xsi:type="object">YourNamespace\YourModule\Aggregator\CustomMetricAggregator</item>
</argument>
</arguments>
</type>
<?php
namespace YourNamespace\YourModule\Aggregator;
use Magento\Review\Model\ResourceModel\Review\CollectionFactory;
use RunAsRoot\PrometheusExporter\Api\MetricAggregatorInterface;
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService;
class ProductRatingAggregator implements MetricAggregatorInterface
{
private CollectionFactory $reviewCollectionFactory;
private UpdateMetricService $updateMetricService;
public function __construct(
CollectionFactory $reviewCollectionFactory,
UpdateMetricService $updateMetricService
) {
$this->reviewCollectionFactory = $reviewCollectionFactory;
$this->updateMetricService = $updateMetricService;
}
public function aggregate(): void
{
$collection = $this->reviewCollectionFactory->create()
->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED);
$averageRating = $collection->getConnection()
->fetchOne('SELECT AVG(rating_value) FROM rating_option_vote');
$this->updateMetricService->update(
'magento_product_average_rating',
(string) round($averageRating, 2),
'gauge',
'Average product rating across all approved reviews'
);
}
}
php bin/magento run_as_root:metrics:collect
php bin/magento run_as_root:metrics:show
php bin/magento run_as_root:metrics:clear
run_as_root_prometheus_metrics_aggregator cron group executesrun_as_root_prometheus_metrics table/metrics controller serves data in Prometheus formatThe module creates a dedicated table run_as_root_prometheus_metrics with the following structure:
metric_id: Primary keymetric_code: Unique metric identifiermetric_value: Numeric valuemetric_type: Type (gauge, counter, histogram)metric_help: Description textmetric_labels: JSON-encoded labelsupdated_at: Last update timestamp# Check cron status
php bin/magento cron:status
# Run metrics collection manually
php bin/magento run_as_root:metrics:collect
# Check system logs
tail -f var/log/system.log | grep prometheus
find . -type f -exec chmod 644 {} \;chown -R www-data:www-data .Enable debug logging by adding to app/etc/env.php:
'system' => [
'default' => [
'prometheus_exporter' => [
'debug' => '1'
]
]
]
Monitor the impact of metric collection:
# Check metric collection execution time
grep "prometheus.*aggregator" var/log/system.log
# Monitor database table size
SELECT COUNT(*) as total_metrics,
MAX(updated_at) as last_update
FROM run_as_root_prometheus_metrics;
We welcome contributions! Here's how you can help:
git clone https://gitlab.com/your-username/magento2-prometheus-exporter.gitcomposer installgit checkout -b feature/your-feature-namevendor/bin/phpunitPlease include: - Magento version - PHP version - Module version - Detailed error messages - Steps to reproduce
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by run_as_root