-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtideways.php
91 lines (88 loc) · 3.08 KB
/
tideways.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
<?php
/**
* Tideways Profiling for MODX Revolution
* Copyright (C) 2022-2023 Maple Design Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* This plugin needs to be enabled for the following System Events:
* OnLoadWebDocument
* OnWebPagePrerender
* OnMODXInit
* OnPageNotFound
*/
if (!class_exists('\Tideways\Profiler')) {
return;
}
\Tideways\Profiler::watchCallback(
'modScript::process',
function($context) {
$span = \Tideways\Profiler::createSpan(substr($context['object']->_class, 3));
$span->annotate([
'title' => $context['object']->get('name'),
'tag' => $context['object']->_tag,
]);
return $span;
}
);
foreach (['modResponse', 'modX'] as $class) {
\Tideways\Profiler::watchCallback(
"$class::sendRedirect",
function($context) {
// Split as my eyes can't parse it on a single line
$urlWithoutQs = explode('?', $_SERVER['REQUEST_URI'])[0];
\Tideways\Profiler::setTransactionName(ltrim($urlWithoutQs, '/'));
}
);
}
/** @var modX $modx */
switch ($modx->event->name) {
case 'OnMODXInit':
/**
* on MODX Init we get
* @var string $contextKey
*/
if ($contextKey === 'mgr') {
\Tideways\Profiler::ignoreTransaction();
return;
}
break;
case 'OnPageNotFound':
\Tideways\Profiler::ignoreTransaction();
break;
case 'OnLoadWebDocument':
// TODO add a mapping file that people can use instead of this
//$tName = ltrim($modx->makeUrl($modx->resource->id), '/');
$tName = ltrim($_SERVER['REQUEST_URI'], '/');
$tNameNew = null;
// if (strpos($tName, 'whatson/display') === 0) {
// $tNameNew = 'whatson/display';
// }
// if (!$tNameNew) {
// if (strpos($tName, '.php') !== false) {
// $tNameNew = 'a page ending .php';
// }
// }
$modx->resource && \Tideways\Profiler::setTransactionName($tNameNew ? $tNameNew : $tName);
break;
case 'OnWebPagePrerender':
if ($modx->user instanceof modUser) {
if ($modx->user->hasSessionContext('mgr')) {
$headers = $modx->response->contentType->get('headers');
$headers[] = "Server-Timing: " . \Tideways\Profiler::generateServerTimingHeaderValue();
$modx->response->contentType->set('headers', $headers);
}
}
break;
}