This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetrix.php
175 lines (152 loc) · 4.51 KB
/
Metrix.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
<?php
require_once realpath(__DIR__.'/Metrix/Version.php');
require_once realpath(__DIR__.'/Metrix/Exception.php');
require_once realpath(__DIR__.'/Metrix/ConnectionInterface.php');
require_once realpath(__DIR__.'/Metrix/BackendInterface.php');
use \Metrix\Exception;
use \Metrix\BackendInterface;
class Metrix {
/**
* Instance of metrics backend class
*/
protected $backend;
/**
* Prefix to add to key names
*/
protected $prefix;
/**
* Delimiter used when attaching $prefix
*/
protected $delimeter = ".";
/**
* @param array $conf configuration hash:
* string backend the metrics service you want to communicate with
* array opts options to pass backend
* string prefix key prefix to attach to individual keys before reporting
*/
public function __construct(array $conf = null) {
if (isset($conf)) {
$this->config($conf);
}
}
/**
* @param array $conf configuration hash:
* string backend the metrics service you want to communicate with
* array opts options to pass backend
* string prefix key prefix to attach to individual keys before reporting
*/
public function config(array $config) {
$options = (isset($config['opts']) ? $config['opts'] : array());
$class = "Metrix\\Backend\\" . ucfirst($config['backend']);
if (isset($config['prefix'])) {
$this->prefix = $config['prefix'];
$this->delimeter = ((isset($config['prefix_delimeter'])) ? $config['prefix_delimeter'] : ".");
}
if (class_exists($class)) {
$this->backend = new $class($options);
} else {
throw new Exception("Backend `" . $config['backend'] . "` doesn't exist");
}
}
/**
* @param array|string $metrics
* @param integer $delta
*/
public function increment($metrics, $value = 1) {
$normalized = $this->normalize($metrics);
$prefixed = $this->prefixKeyNames($normalized);
$this->backend->increment($prefixed);
}
/**
* @param array|string $metrics
* @param integer $delta
*/
public function decrement($metrics, $value = 1) {
$normalized = $this->normalize($metrics);
$prefixed = $this->prefixKeyNames($normalized);
$this->backend->decrement($prefixed);
}
/**
* @param array|string $metric
* @param integer $value
*/
public function count($metrics, $value = null) {
$normalized = $this->normalize($metrics, $value);
$prefixed = $this->prefixKeyNames($normalized);
$this->backend->count($prefixed);
}
/**
* @param string $metric
* @param integer $value
*/
public function gauge($metrics, $value = null) {
$normalized = $this->normalize($metrics, $value);
$prefixed = $this->prefixKeyNames($normalized);
$this->backend->gauge($prefixed);
}
/**
* Accessor method for $backend
*/
public function getBackend() {
return $this->backend;
}
/**
* Setter method for $backend
*/
public function setBackend(BackendInterface $backend) {
$this->backend = $backend;
}
/**
* Set prefix for key
*
* @param string $prefix
*/
public function setPrefix($prefix) {
$this->prefix = $prefix;
}
/**
* Accessor method for $prefix
*/
public function getPrefix() {
return $this->prefix;
}
////
// Private Methods
/**
* Normalize Parameters
*
* Changes
* array('key1', ...)
* array('key1' => 1, ...)
* 'key1'
*
* To this
* array('key1' => 1, ...)
*/
private function normalize($metrics, $value = 1) {
if (is_string($metrics)) {
return array($metrics => $value);
}
// Check if $metrics is an associative array
if ( array_keys($metrics) !== range(0, count($metrics) - 1) ) {
return $metrics;
}
$normalized = array();
foreach ($metrics as $metric) {
$normalized[$metric] = $value;
}
return $normalized;
}
/**
* Prefixes keys in a hash with given $prefix
*/
private function prefixKeyNames($metrics) {
if ($this->prefix == null)
return $metrics;
$prefixed = array();
foreach($metrics as $key => $value) {
$prefixed[$this->prefix . $this->delimeter . $key] = $value;
}
return $prefixed;
}
}