diff --git a/composer.json b/composer.json index 1058f36..57c72be 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "require": { - "symfony/polyfill-php80": "^v1.23.1" + "symfony/polyfill-php80": "^v1.24.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 023cd74..956c26e 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "symfony/polyfill-php80", - "version": "v1.23.1", + "version": "v1.24.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be" + "reference": "57b712b08eddb97c762a8caa32c84e037892d2e9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/1100343ed1a92e3a38f9ae122fc0eb21602547be", - "reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/57b712b08eddb97c762a8caa32c84e037892d2e9", + "reference": "57b712b08eddb97c762a8caa32c84e037892d2e9", "shasum": "" }, "require": { @@ -34,12 +34,12 @@ } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Php80\\": "" - }, "files": [ "bootstrap.php" ], + "psr-4": { + "Symfony\\Polyfill\\Php80\\": "" + }, "classmap": [ "Resources/stubs" ] @@ -71,7 +71,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.23.1" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.24.0" }, "funding": [ { @@ -87,7 +87,7 @@ "type": "tidelift" } ], - "time": "2021-07-28T13:41:28+00:00" + "time": "2021-09-13T13:58:33+00:00" } ], "packages-dev": [], diff --git a/includes/classes/Default_Metrics_Loader.php b/includes/classes/Default_Metrics_Loader.php index 158d509..839c719 100644 --- a/includes/classes/Default_Metrics_Loader.php +++ b/includes/classes/Default_Metrics_Loader.php @@ -7,6 +7,7 @@ use WP_Prometheus_Metrics\metrics\Options_Autoloaded_Size_Metric; use WP_Prometheus_Metrics\metrics\Pending_Updates_Metric; use WP_Prometheus_Metrics\metrics\Performance_Count_Posts_Metric; +use WP_Prometheus_Metrics\metrics\Performance_Write_File_To_WP_Upload_Dir_Metric; use WP_Prometheus_Metrics\metrics\Performance_Write_Temp_File_Metric; use WP_Prometheus_Metrics\metrics\Post_Types_Count_Metric; use WP_Prometheus_Metrics\metrics\Posts_Without_Content_Metric; @@ -38,7 +39,8 @@ function load_default_metrics( $metrics = [] ) { new Transients_Autoloaded_Count_Metric(); new Performance_Count_Posts_Metric(); new Performance_Write_Temp_File_Metric(); - + new Performance_Write_File_To_WP_Upload_Dir_Metric(); + $this->metrics_loaded = true; } diff --git a/includes/classes/metrics/Abstract_Metric.php b/includes/classes/metrics/Abstract_Metric.php index fa63093..dc68504 100644 --- a/includes/classes/metrics/Abstract_Metric.php +++ b/includes/classes/metrics/Abstract_Metric.php @@ -2,96 +2,114 @@ namespace WP_Prometheus_Metrics\metrics; -abstract class Abstract_Metric { - - public $metric_name; - public $type; - - public $legacy_get_param; - - /** - * Metric constructor. - * - * @param $metric_name String Name of the metric - * @param $type String The metrics type, defaults to "gauge" - * @param $legacy_get_param String (Deprecated) The legacy GET parameter, which is checked. Use metric_name instead. - */ - public function __construct( string $metric_name, string $type = 'gauge', $legacy_get_param = false ) { - $this->metric_name = str_replace( '-', '_', $metric_name ); - - // For legacy reasons, this is still supported. Just use the metric name instead - $this->legacy_get_param = $legacy_get_param; - - // @deprecated PROMETHEUS_LEGACY_TYPE -> will be removed in a future release - $this->type = defined( 'PROMETHEUS_LEGACY_TYPE' ) && PROMETHEUS_LEGACY_TYPE ? 'counter' : $type; - - add_filter( 'prometheus_get_metrics', [ $this, 'get_metric' ], 10, 1 ); - } - - public function get_metric( $metrics = [] ) { - $metrics[] = $this; - - return $metrics; - } - - public function print_metric( $measure_all = false ) { - if ( ! $this->is_enabled( $measure_all ) ) { - return; - } - echo "# HELP $this->metric_name {$this->get_help_text()}\n"; - echo "# TYPE $this->metric_name $this->type\n"; - echo $this->metric_name . '{' . $this->get_metric_labels() . '} ' . $this->get_cached_metric_value() . "\n"; - } - - public function get_metric_labels(): string { - $labels = [ 'host="' . get_site_url() . '"' ]; - foreach ( $_GET as $label => $value ) { - if ( str_starts_with( $label, 'label_' ) ) { - $label = sanitize_title( str_replace( [ 'label_', '-' ], [ '', '_' ], $label ) ); - $labels[] = $label . '="' . esc_attr( $value ) . '"'; - } - } - $labels = apply_filters( 'prometheus-metrics-for-wp/labels', $labels, $this->metric_name ); - - return join( ',', $labels ); - } - - public function is_enabled( $measure_all ) { - if ( $measure_all && filter_input( INPUT_GET, $this->metric_name, FILTER_SANITIZE_STRING ) !== 'no' ) { - return $measure_all; - } - - if ( filter_input( INPUT_GET, $this->metric_name, FILTER_SANITIZE_STRING ) === 'yes' ) { - return true; - } - - if ( $this->legacy_get_param && filter_input( INPUT_GET, $this->legacy_get_param, FILTER_SANITIZE_STRING ) === 'yes' ) { - _deprecated_argument( __FUNCTION__, '2.0', "Usage of legacy parameter $this->legacy_get_param is deprecated. Please use metric name instead: $this->metric_name" ); - - return true; - } - - return false; - } - - abstract function get_metric_value(); - - /** - * @return String Must be a function so i18n is supported - */ - abstract function get_help_text(): string; - - private function get_cached_metric_value() { - $transientKey = 'prometheus-metrics-for-wp/' . $this->metric_name; - $value = get_transient( $transientKey ); - if ( ! $value ) { - $value = $this->get_metric_value(); - $value = ( $value != 0 && empty( $value ) ? '-1' : $value ) . " " . ( time() * 1000 ); - $timeout = apply_filters( 'prometheus-metrics-for-wp/timeout', 3600, $this->metric_name ); // 1h by default - set_transient( $transientKey, $value, $timeout ); - } - - return $value; - } +abstract class Abstract_Metric +{ + + public $metric_name; + public $type; + + public $scheme; + public $host; + public $port; + + public $legacy_get_param; + + /** + * Metric constructor. + * + * @param $metric_name String Name of the metric + * @param $type String The metrics type, defaults to "gauge" + * @param $legacy_get_param String (Deprecated) The legacy GET parameter, which is checked. Use metric_name instead. + */ + public function __construct(string $metric_name, string $type = 'gauge', $legacy_get_param = false) + { + $this->metric_name = str_replace('-', '_', $metric_name); + + // For legacy reasons, this is still supported. Just use the metric name instead + $this->legacy_get_param = $legacy_get_param; + + // @deprecated PROMETHEUS_LEGACY_TYPE -> will be removed in a future release + $this->type = defined('PROMETHEUS_LEGACY_TYPE') && PROMETHEUS_LEGACY_TYPE ? 'counter' : $type; + + add_filter('prometheus_get_metrics', [$this, 'get_metric'], 10, 1); + + $this->scheme = parse_url(get_site_url(), PHP_URL_SCHEME); + $this->host = parse_url(get_site_url(), PHP_URL_HOST); + $this->port = parse_url(get_site_url(), PHP_URL_PORT); + } + + public function get_metric($metrics = []) + { + $metrics[] = $this; + + return $metrics; + } + + public function print_metric($measure_all = false) + { + if (!$this->is_enabled($measure_all)) { + return; + } + echo "# HELP $this->metric_name {$this->get_help_text()}\n"; + echo "# TYPE $this->metric_name $this->type\n"; + echo $this->metric_name . '{' . $this->get_metric_labels() . '} ' . $this->get_cached_metric_value() . "\n"; + } + + public function get_metric_labels(): string + { + $labels = ['host="' . $this->host . '"', 'scheme="' . $this->scheme . '"']; + if (!empty($this->port)) { + $labels[] = 'port="' . $this->port . '"'; + } + foreach ($_GET as $label => $value) { + if (str_starts_with($label, 'label_')) { + $label = sanitize_title(str_replace(['label_', '-'], ['', '_'], $label)); + $labels[] = $label . '="' . esc_attr($value) . '"'; + } + } + $labels = apply_filters('prometheus-metrics-for-wp/labels', $labels, $this->metric_name); + + return join(',', $labels); + } + + public function is_enabled($measure_all) + { + if ($measure_all && filter_input(INPUT_GET, $this->metric_name, FILTER_SANITIZE_STRING) !== 'no') { + return $measure_all; + } + + if (filter_input(INPUT_GET, $this->metric_name, FILTER_SANITIZE_STRING) === 'yes') { + return true; + } + + if ($this->legacy_get_param && filter_input(INPUT_GET, $this->legacy_get_param, FILTER_SANITIZE_STRING) === 'yes') { + _deprecated_argument(__FUNCTION__, '2.0', "Usage of legacy parameter $this->legacy_get_param is deprecated. Please use metric name instead: $this->metric_name"); + + return true; + } + + return false; + } + + abstract function get_metric_value(); + + /** + * @return String Must be a function so i18n is supported + */ + abstract function get_help_text(): string; + + private function get_cached_metric_value() + { + $transientKey = 'prometheus-metrics-for-wp/' . $this->metric_name; + $value = get_transient($transientKey); + if (!$value) { + $value = $this->get_metric_value(); + $value = ($value != 0 && empty($value) ? '-1' : $value) . " " . (time() * 1000); + $timeout = apply_filters('prometheus-metrics-for-wp/timeout', 3600, $this->metric_name); // 1h by default + set_transient($transientKey, $value, $timeout); + } + + return $value; + } } \ No newline at end of file diff --git a/includes/classes/metrics/Performance_Write_File_To_WP_Upload_Dir_Metric.php b/includes/classes/metrics/Performance_Write_File_To_WP_Upload_Dir_Metric.php new file mode 100644 index 0000000..4c3a85a --- /dev/null +++ b/includes/classes/metrics/Performance_Write_File_To_WP_Upload_Dir_Metric.php @@ -0,0 +1,35 @@ +print_metric( $measure_all ); + $metric->print_metric( $measure_all ); } return true; diff --git a/readme.md b/readme.md index 2f68531..06b5b80 100644 --- a/readme.md +++ b/readme.md @@ -60,6 +60,13 @@ See the included page at `Tools` -> `Site Health` -> `Prometheus` with specific ## Changelog +### 2.1 ### + +* Seperated host and schema (and port) +* Added metric for writing temporary file into uploads folder +* Disabled legacy metrics by default (to avoid warnings in the logs) + * Enable it, by adding `define('PROMETHEUS_INCLUDE_LEGACY_METRICS', true);` to `wp-config.php` + ### 2.0 ### * **Major rewrite, which may break your current metrics!**