2023-04-09 17:50:33 +00:00
|
|
|
<?php
|
2023-10-20 14:12:29 +00:00
|
|
|
|
2023-10-21 07:02:49 +00:00
|
|
|
|
2023-10-20 14:12:29 +00:00
|
|
|
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
|
2023-10-20 20:39:30 +00:00
|
|
|
use OpenTelemetry\API\Trace\SpanKind;
|
2023-10-21 07:02:49 +00:00
|
|
|
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory;
|
|
|
|
use OpenTelemetry\Contrib\Otlp\SpanExporter;
|
2023-10-20 19:39:41 +00:00
|
|
|
use OpenTelemetry\SDK\Common\Attribute\Attributes;
|
2023-10-21 07:02:49 +00:00
|
|
|
use OpenTelemetry\SDK\Resource\ResourceInfo;
|
|
|
|
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
|
2023-10-20 20:39:30 +00:00
|
|
|
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
|
|
|
|
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
|
2023-10-21 07:02:49 +00:00
|
|
|
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
|
|
|
|
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
|
|
|
|
use OpenTelemetry\SDK\Trace\TracerProvider;
|
|
|
|
use OpenTelemetry\SemConv\ResourceAttributes;
|
2023-10-20 19:39:41 +00:00
|
|
|
|
2023-04-09 17:50:33 +00:00
|
|
|
class Tracer {
|
2023-04-09 18:20:35 +00:00
|
|
|
/** @var Tracer $instance */
|
2023-04-09 17:50:33 +00:00
|
|
|
private static $instance;
|
2023-10-20 18:22:03 +00:00
|
|
|
|
2023-10-21 07:02:49 +00:00
|
|
|
/** @var OpenTelemetry\SDK\Trace\TracerProviderInterface $tracerProvider */
|
2023-10-20 20:39:30 +00:00
|
|
|
private $tracerProvider;
|
|
|
|
|
2023-10-20 18:22:03 +00:00
|
|
|
/** @var OpenTelemetry\API\Trace\TracerInterface $tracer */
|
2023-10-20 18:34:36 +00:00
|
|
|
private $tracer;
|
2023-04-09 17:50:33 +00:00
|
|
|
|
|
|
|
public function __construct() {
|
2023-10-20 18:27:10 +00:00
|
|
|
$OPENTELEMETRY_ENDPOINT = Config::get(Config::OPENTELEMETRY_ENDPOINT);
|
2023-10-20 14:12:29 +00:00
|
|
|
|
2023-10-20 18:27:10 +00:00
|
|
|
if ($OPENTELEMETRY_ENDPOINT) {
|
|
|
|
$transport = (new OtlpHttpTransportFactory())->create($OPENTELEMETRY_ENDPOINT, 'application/x-protobuf');
|
2023-10-20 14:12:29 +00:00
|
|
|
$exporter = new SpanExporter($transport);
|
|
|
|
} else {
|
|
|
|
$exporter = new InMemoryExporter();
|
2023-04-09 18:20:35 +00:00
|
|
|
}
|
2023-10-20 14:12:29 +00:00
|
|
|
|
2023-10-20 19:39:41 +00:00
|
|
|
$resource = ResourceInfoFactory::emptyResource()->merge(
|
|
|
|
ResourceInfo::create(Attributes::create(
|
|
|
|
[ResourceAttributes::SERVICE_NAME => Config::get(Config::OPENTELEMETRY_SERVICE)]
|
|
|
|
), ResourceAttributes::SCHEMA_URL),
|
|
|
|
);
|
|
|
|
|
2023-10-20 20:39:30 +00:00
|
|
|
$this->tracerProvider = TracerProvider::builder()
|
|
|
|
->addSpanProcessor(new SimpleSpanProcessor($exporter))
|
|
|
|
->setResource($resource)
|
|
|
|
->setSampler(new ParentBased(new AlwaysOnSampler()))
|
|
|
|
->build();
|
2023-10-20 19:39:41 +00:00
|
|
|
|
2023-10-20 20:39:30 +00:00
|
|
|
$this->tracer = $this->tracerProvider->getTracer('io.opentelemetry.contrib.php');
|
2023-10-20 14:12:29 +00:00
|
|
|
|
|
|
|
$context = TraceContextPropagator::getInstance()->extract(getallheaders());
|
2023-10-20 20:39:30 +00:00
|
|
|
|
2023-10-21 07:25:29 +00:00
|
|
|
$span = $this->tracer->spanBuilder($_SESSION['name'] ?? 'not logged in')
|
2023-10-20 14:12:29 +00:00
|
|
|
->setParent($context)
|
2023-10-20 20:39:30 +00:00
|
|
|
->setSpanKind(SpanKind::KIND_SERVER)
|
2023-10-21 07:25:03 +00:00
|
|
|
->setAttribute('php.request', json_encode($_REQUEST))
|
|
|
|
->setAttribute('php.server', json_encode($_SERVER))
|
2023-10-21 07:24:23 +00:00
|
|
|
->setAttribute('php.session', json_encode($_SESSION ?? []))
|
2023-10-20 14:12:29 +00:00
|
|
|
->startSpan();
|
|
|
|
|
2023-10-20 19:39:41 +00:00
|
|
|
$scope = $span->activate();
|
2023-10-20 14:12:29 +00:00
|
|
|
|
2023-10-20 20:39:30 +00:00
|
|
|
register_shutdown_function(function() use ($span, $scope) {
|
2023-10-20 14:12:29 +00:00
|
|
|
$span->end();
|
2023-10-20 19:39:41 +00:00
|
|
|
$scope->detach();
|
2023-10-20 20:39:30 +00:00
|
|
|
$this->tracerProvider->shutdown();
|
2023-10-20 14:12:29 +00:00
|
|
|
});
|
2023-04-09 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
2023-10-20 18:22:03 +00:00
|
|
|
* @return OpenTelemetry\API\Trace\SpanInterface
|
2023-04-09 18:20:35 +00:00
|
|
|
*/
|
2023-10-20 19:39:41 +00:00
|
|
|
private function _start(string $name) {
|
2023-10-20 20:39:30 +00:00
|
|
|
$span = $this->tracer
|
|
|
|
->spanBuilder($name)
|
|
|
|
->setSpanKind(SpanKind::KIND_SERVER)
|
|
|
|
->startSpan();
|
2023-10-20 14:12:29 +00:00
|
|
|
|
|
|
|
$span->activate();
|
2023-04-09 18:20:35 +00:00
|
|
|
|
2023-10-20 14:12:29 +00:00
|
|
|
return $span;
|
2023-04-09 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
2023-10-20 18:22:03 +00:00
|
|
|
* @return OpenTelemetry\API\Trace\SpanInterface
|
2023-04-09 18:20:35 +00:00
|
|
|
*/
|
2023-10-20 19:39:41 +00:00
|
|
|
public static function start(string $name) {
|
|
|
|
return self::get_instance()->_start($name);
|
2023-04-09 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_instance() : Tracer {
|
|
|
|
if (self::$instance == null)
|
|
|
|
self::$instance = new self();
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
}
|