create() ) ); $tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); //start a root span $rootSpan = $tracer->spanBuilder('root')->startSpan(); //future spans will be parented to the currently active span $rootScope = $rootSpan->activate(); try { $span1 = $tracer->spanBuilder('foo')->startSpan(); $span1Scope = $span1->activate(); try { $span2 = $tracer->spanBuilder('bar')->startSpan(); echo 'OpenTelemetry welcomes PHP' . PHP_EOL; $span2->end(); } finally { $span1Scope->detach(); $span1->end(); } } catch (Throwable $t) { //The library's code shouldn't be throwing unhandled exceptions (it should emit any errors via diagnostic events) //This is intended to illustrate a way you can capture unhandled exceptions coming from your app code $rootSpan->recordException($t); } finally { //ensure span ends and scope is detached $rootScope->detach(); $rootSpan->end(); } $tracerProvider->shutdown();