-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWP_Exporter.php
275 lines (203 loc) · 5.52 KB
/
WP_Exporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* Plugin Name: Prometheus Exporter
* Description: Collects metrics ready to be exported to Prometheus. https://prometheus.io/
* Version: 1.0
* Author: Peter Booker
* Author URI: https://www.peterbooker.com
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Exit if no cache enabled
if ( ! defined( 'WP_CACHE' ) ) {
//return;
}
// What do to?
if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
//return;
}
if ( ! class_exists( 'WP_Exporter' ) ) {
class WP_Exporter {
/**
* Endpoint Regex
*
* @var string
*/
private $endpoint_regex = '^metrics/?';
/**
* Endpoint URL
*
* @var string
*/
private $endpoint_url = 'metrics';
/**
* Query Var
*
* @var string
*/
private $query_var = 'wpe_metrics';
/**
* Execution Time
*
* @var float
*/
private $execution_time = 0.0;
/**
* Metrics
*
* @var array
*/
private $metrics = array(
// General HTTP
'http_request_duration_seconds',
'http_request_size_bytes',
'http_requests_total',
// WordPress Stuff
'wordpress_request_queries', // not sure about name
'wordpress_scrape_duration_seconds',
);
/**
* WP_Exporter constructor.
*/
public function __construct() {
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_action( 'init', array( $this, 'rewrites_init' ) );
add_action( 'template_redirect', array( $this, 'output_endpoint_data' ) );
add_action( 'shutdown', array( $this, 'set_execution_time' ) );
}
/**
* Gets the Endpoint Regex
*
* @return string
*/
public function get_endpoint_regex() {
return $this->endpoint_regex;
}
/**
* Sets the Endpoint Regex
*
* @param $regex string
* @return string
*/
public function set_endpoint_regex( $regex ) {
$this->endpoint_regex = $regex;
return $this->endpoint_regex;
}
/**
* Gets the Endpoint URL
*
* @return string
*/
public function get_endpoint_url() {
return $this->endpoint_url;
}
/**
* Sets the Endpoint URL
*
* @param $url string
* @return string
*/
public function set_endpoint_url( $url ) {
$this->endpoint_url = $url;
return $this->endpoint_url;
}
/**
* Gets the Query Var
*
* @return string
*/
public function get_query_var() {
return $this->query_var;
}
/**
* Sets the Query Var
*
* @param $var string
* @return string
*/
public function set_query_var( $var ) {
$this->query_var = $var;
return $this->query_var;
}
/**
* Get Execution Time
*
* @return float
*/
public function get_execution_time() {
return $this->execution_time;
}
/**
* Set Execution Time
*
* @return float
*/
public function set_execution_time() {
$this->execution_time = microtime( true ) - $_SERVER['REQUEST_TIME'];
return $this->execution_time;
}
/**
* Get a collection of metrics.
*
* @return object
*/
public function get_metrics() {
// Get Metric Data
}
/**
* Add the metrics endpoint used by Prometheus
*
* @return nil
*/
public function rewrites_init() {
add_rewrite_rule(
$this->endpoint_regex,
'index.php?' . $this->query_var . '=true',
'top'
);
}
/**
* Add the query var for our metrics endpoint
*
* @param array $vars
* @return array
*/
public function add_query_vars( $vars ) {
$vars[] = $this->query_var;
return $vars;
}
/**
* Output Metrics
*
* TODO: Dont want to use a template file, need to find an alternative method
*
* @return object
*/
public function output_endpoint_data() {
global $wp_query;
$wpe_metrics = get_query_var( $this->query_var, false );
if ( $wpe_metrics != true ) {
return;
}
status_header( 200 );
header( 'Content-Type: text/plain' );
global $wpe_exporter;
$output = '# Prometheus Exporter for WordPress' . PHP_EOL;
$output .= '# HELP wp_http_request_duration_microseconds The HTTP request latencies in microseconds.' . PHP_EOL;
$output .= '# TYPE wp_http_request_duration_microseconds summary' . PHP_EOL;
$output .= 'Queries: ' . get_num_queries() . ' ' . PHP_EOL;
$output .= 'Time: ' . (float) $wpe_exporter->set_execution_time() . ' ' . PHP_EOL;
$output .= 'Time: ' . $wpe_exporter->get_execution_time() . ' ' . PHP_EOL;
echo $output;
die();
}
private function wp_send_text( $response ) {
@header( 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ) );
echo wp_json_encode( $response ); // replace this
}
}
}
global $wpe_exporter;
$wpe_exporter = new WP_Exporter;