forked from simplesamlphp/simplesamlphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStats.php
More file actions
113 lines (92 loc) · 2.57 KB
/
Stats.php
File metadata and controls
113 lines (92 loc) · 2.57 KB
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
<?php
declare(strict_types=1);
namespace SimpleSAML;
use SimpleSAML\Assert\Assert;
use function bin2hex;
use function microtime;
use function openssl_random_pseudo_bytes;
use function sprintf;
/**
* Statistics handler class.
*
* This class is responsible for taking a statistics event and logging it.
*
* @package SimpleSAMLphp
*/
class Stats
{
/**
* Whether this class is initialized.
*
* @var boolean
*/
private static bool $initialized = false;
/**
* The statistics output callbacks.
*
* @var \SimpleSAML\Stats\Output[]
*/
private static array $outputs = [];
/**
* Create an output from a configuration object.
*
* @param \SimpleSAML\Configuration $config The configuration.
*
* @return mixed A new instance of the configured class.
* @throws \Exception
*/
private static function createOutput(Configuration $config): mixed
{
$cls = $config->getString('class');
$cls = Module::resolveClass($cls, 'Stats\Output', '\SimpleSAML\Stats\Output');
$output = new $cls($config);
return $output;
}
/**
* Initialize the outputs.
*
* @throws \Exception
*/
private static function initOutputs(): void
{
$config = Configuration::getInstance();
$outputCfgs = $config->getOptionalArray('statistics.out', []);
self::$outputs = [];
foreach ($outputCfgs as $cfg) {
self::$outputs[] = self::createOutput(Configuration::loadFromArray($cfg));
}
}
/**
* Notify about an event.
*
* @param string $event The event.
* @param array $data Event data. Optional.
*
* @return false|null
* @throws \Exception
*/
public static function log(string $event, array $data = []): bool|null
{
Assert::keyNotExists($data, 'op');
Assert::keyNotExists($data, 'time');
Assert::keyNotExists($data, '_id');
if (!self::$initialized) {
self::initOutputs();
self::$initialized = true;
}
if (empty(self::$outputs)) {
// not enabled
return false;
}
$data['op'] = $event;
$data['time'] = microtime(true);
// the ID generation is designed to cluster IDs related in time close together
$int_t = (int) $data['time'];
$hd = openssl_random_pseudo_bytes(16);
$data['_id'] = sprintf('%016x%s', $int_t, bin2hex($hd));
foreach (self::$outputs as $out) {
$out->emit($data);
}
return null;
}
}