create($OPENTELEMETRY_ENDPOINT, 'application/x-protobuf'); $exporter = new SpanExporter($transport); } else { $exporter = new InMemoryExporter(); } $resource = ResourceInfoFactory::emptyResource()->merge( ResourceInfo::create(Attributes::create( [ResourceAttributes::SERVICE_NAME => Config::get(Config::OPENTELEMETRY_SERVICE)] ), ResourceAttributes::SCHEMA_URL), ); $this->tracerProvider = TracerProvider::builder() ->addSpanProcessor(new SimpleSpanProcessor($exporter)) ->setResource($resource) ->setSampler(new ParentBased(new AlwaysOnSampler())) ->build(); $this->tracer = $this->tracerProvider->getTracer('io.opentelemetry.contrib.php'); $context = TraceContextPropagator::getInstance()->extract(getallheaders()); $span = $this->tracer->spanBuilder($_SESSION['name'] ?? 'not-logged-in') ->setParent($context) ->setSpanKind(SpanKind::KIND_SERVER) ->setAttribute('php.request', json_encode($_REQUEST)) ->setAttribute('php.server', json_encode($_SERVER)) ->setAttribute('php.session', json_encode($_SESSION)) ->startSpan(); $scope = $span->activate(); register_shutdown_function(function() use ($span, $scope) { $span->end(); $scope->detach(); $this->tracerProvider->shutdown(); }); } /** * @param string $name * @return OpenTelemetry\API\Trace\SpanInterface */ private function _start(string $name) { $span = $this->tracer ->spanBuilder($name) ->setSpanKind(SpanKind::KIND_SERVER) ->startSpan(); $span->activate(); return $span; } /** * @param string $name * @return OpenTelemetry\API\Trace\SpanInterface */ public static function start(string $name) { return self::get_instance()->_start($name); } public static function get_instance() : Tracer { if (self::$instance == null) self::$instance = new self(); return self::$instance; } }