From 3d5308a6e5f2b3bbfc307716292d1cc98e424edb Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 24 Oct 2023 17:50:00 +0300 Subject: [PATCH] add stub opentelemetry classes in case it is disabled --- .vscode/tasks.json | 2 +- classes/tracer.php | 206 ++++++++++++++++++++++++++++++--------- utils/phpstan-watcher.sh | 0 3 files changed, 162 insertions(+), 46 deletions(-) mode change 100644 => 100755 utils/phpstan-watcher.sh diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 69fd3f3f4..d59e3ff10 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -21,7 +21,7 @@ } }, - "command": "${workspaceRoot}/utils/phpstan-watcher.sh", + "command": "chmod +x ${workspaceRoot}/utils/phpstan-watcher.sh && ${workspaceRoot}/utils/phpstan-watcher.sh", }, { "type": "gulp", diff --git a/classes/tracer.php b/classes/tracer.php index 034d85619..7163adb90 100644 --- a/classes/tracer.php +++ b/classes/tracer.php @@ -1,8 +1,16 @@ 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(); + }); } - - $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(); - }); } /** @@ -73,12 +185,16 @@ class Tracer { * @return OpenTelemetry\API\Trace\SpanInterface */ private function _start(string $name) { - $span = $this->tracer - ->spanBuilder($name) - ->setSpanKind(SpanKind::KIND_SERVER) - ->startSpan(); + if ($this->tracer != null) { + $span = $this->tracer + ->spanBuilder($name) + ->setSpanKind(SpanKind::KIND_SERVER) + ->startSpan(); - $span->activate(); + $span->activate(); + } else { + $span = new DummySpanInterface(); + } return $span; } diff --git a/utils/phpstan-watcher.sh b/utils/phpstan-watcher.sh old mode 100644 new mode 100755