-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
74 lines (63 loc) · 1.96 KB
/
main.php
File metadata and controls
74 lines (63 loc) · 1.96 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
<?php
declare(strict_types=1);
/*
* subscriptions — three Observer clients on one producing session,
* three filters, three sinks. None of them ever issue a command.
*
* RFC §5 (roles), §13 (subscriptions, filters, backfill).
*/
require __DIR__ . '/../../vendor/autoload.php';
use Arcp\Client\ARCPClient;
use Arcp\Envelope\Envelope;
use Arcp\Samples\Subscriptions\OtlpSink;
use Arcp\Samples\Subscriptions\SqliteSink;
use Arcp\Samples\Subscriptions\StdoutSink;
require __DIR__ . '/sinks/StdoutSink.php';
require __DIR__ . '/sinks/SqliteSink.php';
require __DIR__ . '/sinks/OtlpSink.php';
const STDOUT_TYPES = [
'log',
'job.started',
'job.progress',
'job.completed',
'job.failed',
'tool.error',
];
const OTLP_TYPES = ['metric', 'trace.span'];
/**
* @param list<string>|null $types
* @param callable(Envelope): void $handler
*/
function attach(string $sessionId, ?array $types, callable $handler): void
{
/** @var ARCPClient $client */
$client = elided(); // transport, identity, auth elided
$filter = ['session_id' => [$sessionId]];
if ($types !== null) {
$filter['types'] = $types;
}
$sub = $client->subscribe($filter, static function (Envelope $env) use ($handler): void {
$handler($env);
});
// Run for the producing session's lifetime; in production the
// observer client lives in its own process.
register_shutdown_function(static function () use ($client, $sub): void {
$client->unsubscribe($sub);
$client->close();
});
}
function main(): void
{
$targetSession = '...';
$stdout = new StdoutSink();
$otlp = new OtlpSink('https://otlp.internal:4318');
$sqlite = new SqliteSink('replay.sqlite');
attach($targetSession, STDOUT_TYPES, [$stdout, 'handle']);
attach($targetSession, null, [$sqlite, 'handle']);
attach($targetSession, OTLP_TYPES, [$otlp, 'handle']);
}
function elided(): ARCPClient
{
throw new \RuntimeException('not implemented');
}
main();