Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 64898b5

Browse files
authored
Merge pull request #48 from reactphp-parallel/deferred-calls-should-also-increase-metrics-counters
Deferred calls should also increase metrics counters
2 parents d351600 + 0d8312c commit 64898b5

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

infection.json.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"Continue_": {
3131
"ignore": [
32+
"ReactParallel\\ObjectProxy\\Proxy\\Handler::followChain",
3233
"ReactParallel\\ObjectProxy\\Proxy\\Registry::__construct"
3334
]
3435
},
@@ -55,7 +56,8 @@
5556
"ReactParallel\\ObjectProxy\\Proxy\\Handler::setUpHandlers",
5657
"ReactParallel\\ObjectProxy\\Proxy\\Handler::handleNotify",
5758
"ReactParallel\\ObjectProxy\\Proxy\\Handler::handleCall",
58-
"ReactParallel\\ObjectProxy\\Proxy\\Handler::countDestruct"
59+
"ReactParallel\\ObjectProxy\\Proxy\\Handler::countDestruct",
60+
"ReactParallel\\ObjectProxy\\Proxy\\Handler::countHandledChain"
5961
]
6062
},
6163
"LogicalNot": {

src/Proxy/Handler.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function is_object;
3030
use function WyriHaximus\iteratorOrArrayToArray;
3131

32+
use const PHP_EOL;
3233
use const WyriHaximus\Constants\Boolean\FALSE_;
3334
use const WyriHaximus\Constants\Numeric\ZERO;
3435

@@ -82,12 +83,13 @@ private function create(object $object, string $interface): object
8283
}
8384

8485
if ($this->registry->hasByInterface($interface)) {
85-
return $this->registry->getByInterface($interface)->create();
86+
$instance = $this->registry->getByInterface($interface);
87+
} else {
88+
$instance = $this->registry->create($object, $interface);
8689
}
8790

88-
$instance = $this->registry->create($object, $interface);
89-
9091
if ($this->counterCreate instanceof Counters) {
92+
echo PHP_EOL, __METHOD__, PHP_EOL;
9193
$this->counterCreate->counter(new Label('class', $instance->class()), new Label('interface', $interface))->incr();
9294
}
9395

@@ -143,14 +145,19 @@ private function handleNotify(Notify $notify): void
143145
}
144146

145147
if ($notify->link() instanceof Link) {
146-
$instance = $this->registry->getByHash($notify->link()->rootHash());
147-
$object = $this->followChain($notify->link());
148+
$object = $this->followChain($notify->link());
148149

149150
if ($object === null) {
150151
return;
151152
}
152153

153-
$instance = new Instance($object, $instance->interface(), FALSE_, $this->in);
154+
$interface = $this->getInterfaceForOutcome($object);
155+
156+
if ($interface === null) {
157+
return;
158+
}
159+
160+
$instance = new Instance($object, $interface, FALSE_, $this->in);
154161
} else {
155162
$instance = $this->registry->getByHash($notify->hash());
156163
$instance->reference($notify->objectHash());
@@ -171,14 +178,19 @@ private function handleCall(Call $call): void
171178
}
172179

173180
if ($call->link() instanceof Link) {
174-
$instance = $this->registry->getByHash($call->link()->rootHash());
175-
$object = $this->followChain($call->link());
181+
$object = $this->followChain($call->link());
176182

177183
if ($object === null) {
178184
return;
179185
}
180186

181-
$instance = new Instance($object, $instance->interface(), FALSE_, $this->in);
187+
$interface = $this->getInterfaceForOutcome($object);
188+
189+
if ($interface === null) {
190+
return;
191+
}
192+
193+
$instance = new Instance($object, $interface, FALSE_, $this->in);
182194
} else {
183195
$instance = $this->registry->getByHash($call->hash());
184196
$instance->reference($call->objectHash());
@@ -269,12 +281,36 @@ private function followChain(Link $link): ?object
269281

270282
/** @phpstan-ignore-next-line */
271283
$result = $this->registry->getByHash($link->hash())->object()->{$link->method()}(...$link->args());
284+
$this->countHandledChain($result);
272285
/** @phpstan-ignore-next-line */
273286
foreach ($chain as $link) {
274287
/** @phpstan-ignore-next-line */
275288
$result = $result->{$link->method()}(...$link->args());
289+
$this->countHandledChain($result);
276290
}
277291

278292
return $result;
279293
}
294+
295+
private function countHandledChain(object $object): void
296+
{
297+
$interface = $this->getInterfaceForOutcome($object);
298+
if ($interface === null) {
299+
return;
300+
}
301+
302+
if ($this->counterCreate instanceof Counters) {
303+
$this->counterCreate->counter(new Label('class', get_class($object)), new Label('interface', $interface))->incr();
304+
}
305+
306+
if ($this->counterCall instanceof Counters) {
307+
$this->counterCall->counter(new Label('class', get_class($object)), new Label('interface', $interface))->incr();
308+
}
309+
310+
if (! ($this->counterDestruct instanceof Counters)) {
311+
return;
312+
}
313+
314+
$this->counterDestruct->counter(new Label('class', get_class($object)), new Label('interface', $interface))->incr();
315+
}
280316
}

tests/ProxyTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,16 @@ public function metricsDestructionTesting(): void
185185
$txt = $registry->print(new Prometheus());
186186
self::assertStringContainsString('counter_total{name="value"} 128', $txt);
187187
self::assertStringContainsString('react_parallel_object_proxy_create_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry",interface="WyriHaximus\\\\Metrics\\\\Registry"} 1', $txt);
188-
// self::assertStringContainsString('react_parallel_object_proxy_create_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry\\\\Counters",interface="WyriHaximus\\\\Metrics\\\\Registry\\\\Counters"} 128', $txt);
189-
// self::assertStringContainsString('react_parallel_object_proxy_create_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Counter",interface="WyriHaximus\\\\Metrics\\\\Counter"} 128', $txt);
188+
self::assertStringContainsString('react_parallel_object_proxy_create_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry\\\\Counters",interface="WyriHaximus\\\\Metrics\\\\Registry\\\\Counters"} 128', $txt);
189+
self::assertStringContainsString('react_parallel_object_proxy_create_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Counter",interface="WyriHaximus\\\\Metrics\\\\Counter"} 128', $txt);
190+
191+
self::assertStringContainsString('react_parallel_object_proxy_notify_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Counter",interface="WyriHaximus\\\\Metrics\\\\Counter"} 128', $txt);
192+
190193
self::assertStringContainsString('react_parallel_object_proxy_call_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry",interface="WyriHaximus\\\\Metrics\\\\Registry"} 128', $txt);
191-
// self::assertStringContainsString('react_parallel_object_proxy_call_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry\\\\Counters",interface="WyriHaximus\\\\Metrics\\\\Registry\\\\Counters"} 128', $txt);
192-
// self::assertStringContainsString('react_parallel_object_proxy_notify_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Counter",interface="WyriHaximus\\\\Metrics\\\\Counter"} 128', $txt);
193-
// self::assertStringContainsString('react_parallel_object_proxy_destruct_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry\\\\Counters",interface="WyriHaximus\\\\Metrics\\\\Registry\\\\Counters"} 128', $txt);
194-
// self::assertStringContainsString('react_parallel_object_proxy_destruct_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Counter",interface="WyriHaximus\\\\Metrics\\\\Counter"} 128', $txt);
195-
// self::assertStringContainsString('react_parallel_object_proxy_destruct_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry",interface="WyriHaximus\\\\Metrics\\\\Registry"} 128', $txt);
194+
self::assertStringContainsString('react_parallel_object_proxy_call_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry\\\\Counters",interface="WyriHaximus\\\\Metrics\\\\Registry\\\\Counters"} 128', $txt);
195+
196+
self::assertStringContainsString('react_parallel_object_proxy_destruct_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Registry\\\\Counters",interface="WyriHaximus\\\\Metrics\\\\Registry\\\\Counters"} 128', $txt);
197+
self::assertStringContainsString('react_parallel_object_proxy_destruct_total{class="WyriHaximus\\\\Metrics\\\\InMemory\\\\Counter",interface="WyriHaximus\\\\Metrics\\\\Counter"} 128', $txt);
196198
}
197199

198200
/**

0 commit comments

Comments
 (0)