-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pinba.php
195 lines (173 loc) · 5.38 KB
/
Pinba.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
<?php
/**
* Simple class that just incapsulates Pinba configuration and methods for Yii
* Just load it on start of application and prepare configurations
* (default one would be goo enought)
*
* @author Last G <last-g@skbkontur.ru>
*
*/
/**
* Simple internal class that incapsulates pinba core functions
*
* @author Last G <last-g@skbkontur.ru>
*/
class PinbaApi extends CComponent {
/**
* Creates and starts new timer
*
* @param array $tags for timer
* @param array $data for timer
* @return int timer resurse id
*/
public function timerStart(array $tags, array $data=array())
{
return pinba_timer_start($tags, $data);
}
/**
* Stops timer
*
* @param int $timer resourse id
* @return bool
*/
public function timerStop($timer)
{
return pinba_timer_stop($timer);
}
public function timerAdd(array $tags, $value, array $data=array())
{
return pinba_timer_add($tags, $value, $data);
}
public function timerDelete($timer)
{
return pinba_timer_delete($timer);
}
public function timerTagsMerge($timer, array $tags) {
return pinba_timer_tags_merge($timer, $tags);
}
public function timerTagsReplace($timer, array $tags) {
return pinba_timer_tags_replace($timer, $tags);
}
public function timerDataMerge($timer, array $data) {
return pinba_timer_data_merge($timer, $data);
}
public function timerDataReplace($timer, array $data) {
return pinba_timer_data_replace($timer, $data);
}
public function timerGetInfo($timer) {
return pinba_timer_get_info($timer);
}
/**
* Stops all timers
* @return bool
*/
public function timersStop() {
return pinba_timers_stop();
}
/**
* Gets info about pinba
*
* @return array
*/
public function getInfo() {
return pinba_get_info();
}
public function setSchema($schema) {
return pinba_schema_set($schema);
}
public function setScriptName($scriptName) {
return pinba_script_name_set($scriptName);
}
public function setServerName($serverName) {
return pinba_server_name_set($serverName);
}
public function setRequestTime($requestTime) {
return pinba_request_time_set($requestTime);
}
public function setHostname($hostname) {
return pinba_hostname_set($hostname);
}
}
/**
* Pinba extenstion component
* You should use it for configuring Pinba and accessing API
* Do not use PinbaAPI class to invoke method, coz not all systems have pinba installed
*/
class Pinba extends CApplicationComponent {
private $_internalCaller;
public $on = true;
public $fixScriptName = true;
public $scriptName = null;
public $hostName = null;
public $serverName = null;
public $schema = null;
/**
* Check if Pinba module and extension ebabled
*
* @return bool true if enabled
*/
public function getEnabled() {
return (extension_loaded("pinba") && $this->on);
}
/**
* Initianizes Pinba component class
*/
public function init() {
parent::init();
$this->_internalCaller = new PinbaApi();
if(! is_null($this->scriptName) ) {
$this->setScriptName($this->scriptName);
} elseif ($this->fixScriptName) {
if(php_sapi_name() === 'cli') {
$argv = implode(' ', $_SERVER['argv']);
$this->setScriptName($argv);
}
else {
$path = Yii::app()->urlManager->parseUrl(Yii::app()->request);
$this->setScriptName('/' . $path);
}
}
if(! is_null($this->hostName))
{
$this->setHostname($this->hostName);
}
if(! is_null($this->serverName))
{
$this->setServerName($this->serverName);
}
if(! is_null($this->schema))
{
$this->setSchema($this->schema);
}
}
/**
* Proxing all calls to Pinba class to the PinbaAPI class
*
* @param string $name of function that was called
* @param array $parameters of function call
* @return type
*/
public function __call($name, $parameters) {
// Translating all to real API if enabled
if(method_exists($this->_internalCaller, $name)) {
Yii::trace("Calling to Pinba API: $name with " . print_r($parameters, true), 'ext.yii-pinba');
if($this->enabled) {
try {
// Calling real function from PinbaAPI class
return call_user_func_array(array($this->_internalCaller, $name),$parameters);
} catch (CException $ex) {
// Pass (that was no such method)
}
}
else {
// Return null if we just emulating call to api
return null;
}
}
else
{
return parent::__call($name, $parameters);
}
}
/// Callers for pinba functions
}