Skip to content

Commit a4399d4

Browse files
committed
feat(metric): Adds info metrics to give WordPress version
This adds a new wp_info metric that returns WordPress code and database versions. Versions are in the "version" and "db_version" labels. Issue: CodeAtCode#15
1 parent c453b15 commit a4399d4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

includes/classes/Default_Metrics_Loader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WP_Prometheus_Metrics;
44

55
use WP_Prometheus_Metrics\metrics\Database_Size_Metric;
6+
use WP_Prometheus_Metrics\metrics\Info_Metric;
67
use WP_Prometheus_Metrics\metrics\Options_Autoloaded_Count_Metric;
78
use WP_Prometheus_Metrics\metrics\Options_Autoloaded_Size_Metric;
89
use WP_Prometheus_Metrics\metrics\Pending_Updates_Metric;
@@ -29,6 +30,7 @@ function load_default_metrics($metrics = [])
2930
{
3031
if (!$this->metrics_loaded) {
3132
new Database_Size_Metric();
33+
new Info_Metric();
3234
new Options_Autoloaded_Count_Metric();
3335
new Options_Autoloaded_Size_Metric();
3436
new Pending_Updates_Metric();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace WP_Prometheus_Metrics\metrics;
4+
5+
6+
/**
7+
* A metric that gives information about the WordPress environment.
8+
*/
9+
class Info_Metric extends Abstract_Gauge_Metric
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct('info');
14+
}
15+
16+
public function get_metric_labels(): array
17+
{
18+
global $wp_db_version;
19+
20+
return array_merge(
21+
parent::get_metric_labels(),
22+
[
23+
'version' => get_bloginfo('version'),
24+
'db_version' => $wp_db_version,
25+
]
26+
);
27+
}
28+
29+
function get_metric_value()
30+
{
31+
// This metric as no value, the interesting part is in it's labels.
32+
// Returning a standard 1 value here.
33+
return 1;
34+
}
35+
36+
function get_help_text(): string
37+
{
38+
return _x('Information about the WordPress environment', 'Metric Help Text', 'prometheus-metrics-for-wp');
39+
}
40+
}

0 commit comments

Comments
 (0)