-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.php
39 lines (30 loc) · 1.1 KB
/
trace.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
<?php
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/vendor/autoload.php';
putenv('OTEL_PHP_FIBERS_ENABLED=true');
$httpTransport = (new OtlpHttpTransportFactory())
->create('http://localhost:4318/v1/traces', 'application/json');
$exporter = new SpanExporter($httpTransport);
$tracerProvider = new TracerProvider(
new SimpleSpanProcessor($exporter)
);
$tracer = $tracerProvider->getTracer('demo');
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response) use ($tracer) {
$span = $tracer
->spanBuilder('manual-span')
->startSpan();
$result = random_int(1,6);
$response->getBody()->write((string)$result);
$span
->addEvent('rolled dice', ['result' => $result])
->end();
return $response;
});
$app->run();