Repository navigation
Expand file tree
/
Copy pathInMemoryMetrics.php
More file actions
147 lines (119 loc) · 4.12 KB
/
Copy pathInMemoryMetrics.php
File metadata and controls
147 lines (119 loc) · 4.12 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
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
<?php
declare(strict_types=1);
namespace CrazyGoat\TiKV\Client\Observability;
/**
* In-memory, thread-unsafe (single-process PHP) implementation of
* MetricsInterface used for tests and as a reference for users who
* want to roll their own backend.
*
* Counters are stored as `[tag => value]` maps. The recorder also
* retains per-call RPC latency samples so assertions can be made on
* exact counts, success/failure ratios, and mean latency.
*
* This class is NOT intended for production use — values are not
* thread-safe across PHP-FPM workers and memory grows unbounded.
* For production, implement MetricsInterface on top of Prometheus,
* StatsD, OpenTelemetry or your backend of choice.
*/
final class InMemoryMetrics implements MetricsInterface
{
/** @var array<string, int> */
private array $rpcStarted = [];
/** @var array<string, int> */
private array $rpcSucceeded = [];
/** @var array<string, int> */
private array $rpcFailed = [];
/** @var array<string, int> */
private array $retries = [];
/** @var array<string, int> */
private array $cacheHits = [];
/** @var array<string, int> */
private array $cacheMisses = [];
/** @var array<string, int> */
private array $invalidations = [];
/** @var array<string, float> */
private array $latencySumMs = [];
/** @var array<string, int> */
private array $latencyCount = [];
public function rpcStarted(string $operation): void
{
$this->rpcStarted[$operation] = ($this->rpcStarted[$operation] ?? 0) + 1;
}
public function rpcCompleted(string $operation, float $durationMs, bool $success): void
{
if ($success) {
$this->rpcSucceeded[$operation] = ($this->rpcSucceeded[$operation] ?? 0) + 1;
} else {
$this->rpcFailed[$operation] = ($this->rpcFailed[$operation] ?? 0) + 1;
}
$this->latencySumMs[$operation] = ($this->latencySumMs[$operation] ?? 0.0) + $durationMs;
$this->latencyCount[$operation] = ($this->latencyCount[$operation] ?? 0) + 1;
}
public function retryAttempted(string $operation): void
{
$this->retries[$operation] = ($this->retries[$operation] ?? 0) + 1;
}
public function regionCacheHit(string $operation): void
{
$this->cacheHits[$operation] = ($this->cacheHits[$operation] ?? 0) + 1;
}
public function regionCacheMiss(string $operation): void
{
$this->cacheMisses[$operation] = ($this->cacheMisses[$operation] ?? 0) + 1;
}
public function regionInvalidated(string $reason): void
{
$this->invalidations[$reason] = ($this->invalidations[$reason] ?? 0) + 1;
}
/**
* Reset all counters. Intended for tests.
*/
public function reset(): void
{
$this->rpcStarted = [];
$this->rpcSucceeded = [];
$this->rpcFailed = [];
$this->retries = [];
$this->cacheHits = [];
$this->cacheMisses = [];
$this->invalidations = [];
$this->latencySumMs = [];
$this->latencyCount = [];
}
public function getRpcStarted(string $operation): int
{
return $this->rpcStarted[$operation] ?? 0;
}
public function getRpcSucceeded(string $operation): int
{
return $this->rpcSucceeded[$operation] ?? 0;
}
public function getRpcFailed(string $operation): int
{
return $this->rpcFailed[$operation] ?? 0;
}
public function getRetries(string $operation): int
{
return $this->retries[$operation] ?? 0;
}
public function getCacheHits(string $operation): int
{
return $this->cacheHits[$operation] ?? 0;
}
public function getCacheMisses(string $operation): int
{
return $this->cacheMisses[$operation] ?? 0;
}
public function getInvalidations(string $reason): int
{
return $this->invalidations[$reason] ?? 0;
}
public function getMeanLatencyMs(string $operation): float
{
$count = $this->latencyCount[$operation] ?? 0;
if ($count === 0) {
return 0.0;
}
return ($this->latencySumMs[$operation] ?? 0.0) / $count;
}
}