-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* auto root span creation proof of concept for automatically creating a root span on startup. the obvious deficiencies are: - no idea of response values (status code etc) - does not capture exceptions * deptrac
- Loading branch information
Showing
12 changed files
with
352 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Example; | ||
|
||
use OpenTelemetry\API\Globals; | ||
use OpenTelemetry\API\Logs\LogRecord; | ||
|
||
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true'); | ||
putenv('OTEL_TRACES_EXPORTER=console'); | ||
putenv('OTEL_METRICS_EXPORTER=none'); | ||
putenv('OTEL_LOGS_EXPORTER=console'); | ||
putenv('OTEL_PROPAGATORS=tracecontext'); | ||
putenv('OTEL_PHP_EXPERIMENTAL_AUTO_ROOT_SPAN=true'); | ||
|
||
//Usage: php -S localhost:8080 examples/traces/features/auto_root_span.php | ||
|
||
require dirname(__DIR__, 3) . '/vendor/autoload.php'; | ||
|
||
Globals::loggerProvider()->getLogger('test')->emit(new LogRecord('I processed a request')); | ||
echo 'hello world!' . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Trace; | ||
|
||
use Http\Discovery\Exception\NotFoundException; | ||
use Http\Discovery\Psr17FactoryDiscovery; | ||
use Nyholm\Psr7Server\ServerRequestCreator; | ||
use OpenTelemetry\API\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\API\Globals; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
use OpenTelemetry\SDK\Common\Configuration\Variables; | ||
use OpenTelemetry\SDK\Common\Util\ShutdownHandler; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use OpenTelemetry\SemConv\Version; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class AutoRootSpan | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
public static function isEnabled(): bool | ||
{ | ||
return | ||
!empty($_SERVER['REQUEST_METHOD'] ?? null) | ||
&& Configuration::getBoolean(Variables::OTEL_PHP_EXPERIMENTAL_AUTO_ROOT_SPAN); | ||
} | ||
|
||
/** | ||
* @psalm-suppress ArgumentTypeCoercion | ||
* @internal | ||
*/ | ||
public static function create(ServerRequestInterface $request): void | ||
{ | ||
$tracer = Globals::tracerProvider()->getTracer( | ||
'io.opentelemetry.php.auto-root-span', | ||
null, | ||
Version::VERSION_1_25_0->url(), | ||
); | ||
$parent = Globals::propagator()->extract($request->getHeaders()); | ||
$startTime = array_key_exists('REQUEST_TIME_FLOAT', $request->getServerParams()) | ||
? $request->getServerParams()['REQUEST_TIME_FLOAT'] | ||
: (int) microtime(true); | ||
$span = $tracer->spanBuilder($request->getMethod()) | ||
->setSpanKind(SpanKind::KIND_SERVER) | ||
->setStartTimestamp((int) ($startTime*1_000_000)) | ||
->setParent($parent) | ||
->setAttribute(TraceAttributes::URL_FULL, (string) $request->getUri()) | ||
->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $request->getMethod()) | ||
->setAttribute(TraceAttributes::HTTP_REQUEST_BODY_SIZE, $request->getHeaderLine('Content-Length')) | ||
->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->getHeaderLine('User-Agent')) | ||
->setAttribute(TraceAttributes::SERVER_ADDRESS, $request->getUri()->getHost()) | ||
->setAttribute(TraceAttributes::SERVER_PORT, $request->getUri()->getPort()) | ||
->setAttribute(TraceAttributes::URL_SCHEME, $request->getUri()->getScheme()) | ||
->setAttribute(TraceAttributes::URL_PATH, $request->getUri()->getPath()) | ||
->startSpan(); | ||
Context::storage()->attach($span->storeInContext($parent)); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function createRequest(): ?ServerRequestInterface | ||
{ | ||
assert(array_key_exists('REQUEST_METHOD', $_SERVER) && !empty($_SERVER['REQUEST_METHOD'])); | ||
|
||
try { | ||
$creator = new ServerRequestCreator( | ||
Psr17FactoryDiscovery::findServerRequestFactory(), | ||
Psr17FactoryDiscovery::findUriFactory(), | ||
Psr17FactoryDiscovery::findUploadedFileFactory(), | ||
Psr17FactoryDiscovery::findStreamFactory(), | ||
); | ||
|
||
return $creator->fromGlobals(); | ||
} catch (NotFoundException $e) { | ||
self::logError('Unable to initialize server request creator for auto root span creation', ['exception' => $e]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function registerShutdownHandler(): void | ||
{ | ||
ShutdownHandler::register(self::shutdownHandler(...)); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function shutdownHandler(): void | ||
{ | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
$span->end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
tests/Integration/SDK/Trace/test_auto_root_span_creation.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--TEST-- | ||
Auto root span creation | ||
--ENV-- | ||
OTEL_PHP_AUTOLOAD_ENABLED=true | ||
OTEL_PHP_EXPERIMENTAL_AUTO_ROOT_SPAN=true | ||
OTEL_TRACES_EXPORTER=console | ||
OTEL_METRICS_EXPORTER=none | ||
OTEL_LOGS_EXPORTER=console | ||
REQUEST_METHOD=GET | ||
REQUEST_URI=/foo?bar=baz | ||
REQUEST_SCHEME=https | ||
SERVER_NAME=example.com | ||
SERVER_PORT=8080 | ||
HTTP_HOST=example.com:8080 | ||
HTTP_USER_AGENT=my-user-agent/1.0 | ||
REQUEST_TIME_FLOAT=1721706151.242976 | ||
HTTP_TRACEPARENT=00-ff000000000000000000000000000041-ff00000000000041-01 | ||
--FILE-- | ||
<?php | ||
require_once 'vendor/autoload.php'; | ||
?> | ||
--EXPECTF-- | ||
[ | ||
{ | ||
"name": "GET", | ||
"context": { | ||
"trace_id": "ff000000000000000000000000000041", | ||
"span_id": "%s", | ||
"trace_state": "", | ||
"trace_flags": 1 | ||
}, | ||
"resource": {%A | ||
}, | ||
"parent_span_id": "ff00000000000041", | ||
"kind": "KIND_SERVER", | ||
"start": 1721706151242976, | ||
"end": %d, | ||
"attributes": { | ||
"url.full": "%s", | ||
"http.request.method": "GET", | ||
"http.request.body.size": "", | ||
"user_agent.original": "my-user-agent\/1.0", | ||
"server.address": "%S", | ||
"server.port": %d, | ||
"url.scheme": "https", | ||
"url.path": "\/foo" | ||
}, | ||
"status": { | ||
"code": "Unset", | ||
"description": "" | ||
}, | ||
"events": [], | ||
"links": [], | ||
"schema_url": "%s" | ||
} | ||
] |
Oops, something went wrong.