-
Notifications
You must be signed in to change notification settings - Fork 26
/
frontend.php
53 lines (38 loc) · 1.42 KB
/
frontend.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
<?php
use Zipkin\Timestamp;
use Zipkin\Propagation\Map;
use Zipkin\Propagation\DefaultSamplingFlags;
use GuzzleHttp\Client;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/functions.php';
$tracing = create_tracing('frontend', '127.0.0.1');
$tracer = $tracing->getTracer();
/* Always sample traces */
$defaultSamplingFlags = DefaultSamplingFlags::createAsSampled();
/* Creates the main span */
$span = $tracer->newTrace($defaultSamplingFlags);
$span->start(Timestamp\now());
$span->setName('parse_request');
$span->setKind(Zipkin\Kind\SERVER);
usleep(100 * mt_rand(1, 3));
/* Creates the span for getting the users list */
$childSpan = $tracer->newChild($span->getContext());
$childSpan->start();
$childSpan->setKind(Zipkin\Kind\CLIENT);
$childSpan->setName('users:get_list');
$headers = [];
/* Injects the context into the wire */
$injector = $tracing->getPropagation()->getInjector(new Map());
$injector($childSpan->getContext(), $headers);
/* HTTP Request to the backend */
$httpClient = new Client();
$request = new \GuzzleHttp\Psr7\Request('POST', 'localhost:9000', $headers);
$childSpan->annotate('request_started', Timestamp\now());
$response = $httpClient->send($request);
$childSpan->annotate('request_finished', Timestamp\now());
$childSpan->finish();
$span->finish();
/* Sends the trace to zipkin once the response is served */
register_shutdown_function(function () use ($tracer) {
$tracer->flush();
});