generated from dunglas/symfony-docker
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHelloController.php
65 lines (54 loc) · 2.04 KB
/
HelloController.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
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Controller;
use OpenTelemetry\API\Trace\SpanInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use OpenTelemetry\SDK\Trace\Tracer;
class HelloController extends AbstractController
{
private const TEMPLATE = 'hello/index.html.twig';
private Tracer $tracer;
private string $jaegerGuiUrl;
private string $zipkinGuiUrl;
public function __construct(Tracer $tracer, string $jaegerGuiUrl, string $zipkinGuiUrl)
{
$this->tracer = $tracer;
$this->jaegerGuiUrl = $jaegerGuiUrl;
$this->zipkinGuiUrl = $zipkinGuiUrl;
}
/**
* @Route("/hello", name="hello")
*/
public function index(): Response
{
$controllerSpan = $this->startSpan(__METHOD__);
$controllerSpan->addEvent('Start doing stuff');
// simulate some computation
usleep(50000);
$controllerSpan->addEvent('Finished doing stuff');
$templateSpan = $this->startSpan('render:'.self::TEMPLATE);
// render HTML
$result = $this->render(self::TEMPLATE, [
'jaeger_gui_url' => $this->jaegerGuiUrl,
'zipkin_gui_url' => $this->zipkinGuiUrl,
'trace_id' => $controllerSpan->getContext()->getTraceId(),
'controller_span_id' => $controllerSpan->getContext()->getSpanId(),
'template_span_id' => $templateSpan->getContext()->getSpanId(),
'controller_sampling_decision' => $controllerSpan->isRecording() ? 'recording' : 'non-recording',
'template_sampling_decision' => $templateSpan->isRecording() ? 'recording' : 'non-recording',
]);
// end spans
foreach ([$templateSpan, $controllerSpan] as $span) {
$span->end();
}
// return rendered HTML
return $result;
}
private function startSpan(string $name): SpanInterface
{
return $this->tracer
->spanBuilder($name)
->startSpan();
}
}