|
1
|
|
|
<?php |
|
2
|
|
|
namespace xmarcos\Carbon; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use ErrorException; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class Client |
|
9
|
|
|
{ |
|
10
|
|
|
protected $stream; |
|
11
|
|
|
protected $namespace; |
|
12
|
|
|
protected $throw_exceptions; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Creates an instance of the Carbon Client |
|
16
|
|
|
* |
|
17
|
|
|
* @param resource $stream A php stream that knows how to talk to Carbon. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct($stream) |
|
20
|
|
|
{ |
|
21
|
|
|
if (!is_resource($stream)) { |
|
22
|
|
|
throw new InvalidArgumentException('Stream must be a resource.'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$this->stream = $stream; |
|
26
|
|
|
$this->throwExceptions(false); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Controls whether failed calls to Carbon will throw an Exception. |
|
31
|
|
|
* |
|
32
|
|
|
* @see send() |
|
33
|
|
|
* |
|
34
|
|
|
* @param boolean $throw |
|
35
|
|
|
* |
|
36
|
|
|
* @return self |
|
37
|
|
|
*/ |
|
38
|
|
|
public function throwExceptions($throw = true) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->throw_exceptions = (bool) $throw; |
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets the namespace used to prepend metric's paths |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $namespace |
|
49
|
|
|
* |
|
50
|
|
|
* @return self |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setNamespace($namespace) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->namespace = $this->sanitizePath($namespace); |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the current namespace. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getNamespace() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->namespace; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sends a metric to Carbon. |
|
71
|
|
|
* |
|
72
|
|
|
* @see http://graphite.readthedocs.org/en/latest/feeding-carbon.html |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $path Metric Path |
|
75
|
|
|
* @param int|float $value Metric Value |
|
76
|
|
|
* @param int|null $timestamp Metric Timestamp |
|
77
|
|
|
* |
|
78
|
|
|
* @throws ErrorException If $this->throw_exceptions is true |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function send($path, $value, $timestamp = null) |
|
82
|
|
|
{ |
|
83
|
|
|
$result = false; |
|
84
|
|
|
$exception = null; |
|
85
|
|
|
|
|
86
|
|
|
set_error_handler(function ($code, $message, $file = null, $line = 0) { |
|
87
|
|
|
throw new ErrorException($message, $code, null, $file, $line); |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
if (!is_string($path) || empty($path)) { |
|
92
|
|
|
throw new InvalidArgumentException('$path must be a non-empty string'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (!is_numeric($value)) { |
|
96
|
|
|
throw new InvalidArgumentException( |
|
97
|
|
|
sprintf('$value must be of type int|float, %s given.', gettype($value)) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$value = (float) $value; |
|
102
|
|
|
$timestamp = is_numeric($timestamp) ? (int) $timestamp : time(); |
|
103
|
|
|
$full_path = $this->sanitizePath( |
|
104
|
|
|
sprintf('%s.%s', $this->getNamespace(), $path) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
$data = sprintf("%s %f %d\n", $full_path, $value, $timestamp); |
|
108
|
|
|
$sent = fwrite($this->stream, $data); |
|
109
|
|
|
$result = is_int($sent) && $sent === strlen($data); |
|
110
|
|
|
} catch (Exception $e) { |
|
111
|
|
|
$exception = $e; |
|
112
|
|
|
} |
|
113
|
|
|
restore_error_handler(); |
|
114
|
|
|
|
|
115
|
|
|
if (!empty($exception) && $this->throw_exceptions) { |
|
116
|
|
|
throw $exception; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $result; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Sanitizes a path string |
|
124
|
|
|
* |
|
125
|
|
|
* Carbon stores metrics using dot delimited paths |
|
126
|
|
|
* {@link http://graphite.readthedocs.org/en/latest/feeding-carbon.html} |
|
127
|
|
|
* |
|
128
|
|
|
* Replaces: |
|
129
|
|
|
* - whitespace with undercores |
|
130
|
|
|
* - consecutive dots with a single dot. |
|
131
|
|
|
* |
|
132
|
|
|
* Removes: |
|
133
|
|
|
* - the wildcard character (used by graphite) |
|
134
|
|
|
* - leading and trailing dots |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $path the path string to sanitize |
|
|
|
|
|
|
137
|
|
|
* |
|
138
|
|
|
* @return string The sanitized path string or an empty one. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function sanitizePath($string) |
|
141
|
|
|
{ |
|
142
|
|
|
if (!is_string($string) || empty($string)) { |
|
143
|
|
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$replace = [ |
|
147
|
|
|
'/\s+/' => '_', |
|
148
|
|
|
'/\*{1,}/' => '', |
|
149
|
|
|
'/\.{2,}/' => '.', |
|
150
|
|
|
'/^\./' => '', |
|
151
|
|
|
'/\.$/' => '', |
|
152
|
|
|
]; |
|
153
|
|
|
|
|
154
|
|
|
return preg_replace( |
|
155
|
|
|
array_keys($replace), |
|
156
|
|
|
array_values($replace), |
|
157
|
|
|
trim($string) |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Closes the stream when the object is destructed |
|
163
|
|
|
*/ |
|
164
|
|
|
public function __destruct() |
|
165
|
|
|
{ |
|
166
|
|
|
if (is_resource($this->stream)) { |
|
167
|
|
|
fclose($this->stream); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.