add placeholder instrumentation for public

This commit is contained in:
Andrew Dolgov 2023-10-20 23:39:30 +03:00
parent d3fadc0bd0
commit 350177df39
No known key found for this signature in database
GPG Key ID: 1A56B4FA25D4AF2A
4 changed files with 34 additions and 16 deletions

View File

@ -30,12 +30,6 @@
$op = (string)clean($op); $op = (string)clean($op);
$method = (string)clean($method); $method = (string)clean($method);
$span = Tracer::start(__FILE__);
register_shutdown_function(function() use ($span) {
$span->end();
});
startup_gettext(); startup_gettext();
$script_started = microtime(true); $script_started = microtime(true);
@ -44,6 +38,8 @@
return; return;
} }
$span = OpenTelemetry\API\Trace\Span::getCurrent();
header("Content-Type: text/json; charset=utf-8"); header("Content-Type: text/json; charset=utf-8");
if (Config::get(Config::SINGLE_USER_MODE)) { if (Config::get(Config::SINGLE_USER_MODE)) {

View File

@ -5,18 +5,23 @@ use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider; use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory; use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Common\Attribute\Attributes; use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SemConv\ResourceAttributes; use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
class Tracer { class Tracer {
/** @var Tracer $instance */ /** @var Tracer $instance */
private static $instance; private static $instance;
private $tracerProvider;
/** @var OpenTelemetry\API\Trace\TracerInterface $tracer */ /** @var OpenTelemetry\API\Trace\TracerInterface $tracer */
private $tracer; private $tracer;
@ -36,23 +41,29 @@ class Tracer {
), ResourceAttributes::SCHEMA_URL), ), ResourceAttributes::SCHEMA_URL),
); );
$tracerProvider = new TracerProvider(new SimpleSpanProcessor($exporter), null, $resource); $this->tracerProvider = TracerProvider::builder()
->addSpanProcessor(new SimpleSpanProcessor($exporter))
->setResource($resource)
->setSampler(new ParentBased(new AlwaysOnSampler()))
->build();
$this->tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); $this->tracer = $this->tracerProvider->getTracer('io.opentelemetry.contrib.php');
$context = TraceContextPropagator::getInstance()->extract(getallheaders()); $context = TraceContextPropagator::getInstance()->extract(getallheaders());
$span = $this->tracer->spanBuilder('root') $span = $this->tracer->spanBuilder('root')
->setParent($context) ->setParent($context)
->setAttribute('http.request', json_encode($_REQUEST)) ->setSpanKind(SpanKind::KIND_SERVER)
->setAttribute('php.request', json_encode($_REQUEST))
->setAttribute('php.server', json_encode($_SERVER))
->startSpan(); ->startSpan();
$scope = $span->activate(); $scope = $span->activate();
register_shutdown_function(function() use ($span, $tracerProvider, $scope) { register_shutdown_function(function() use ($span, $scope) {
$span->end(); $span->end();
$scope->detach(); $scope->detach();
$this->tracerProvider->shutdown();
$tracerProvider->shutdown();
}); });
} }
@ -61,7 +72,10 @@ class Tracer {
* @return OpenTelemetry\API\Trace\SpanInterface * @return OpenTelemetry\API\Trace\SpanInterface
*/ */
private function _start(string $name) { private function _start(string $name) {
$span = $this->tracer->spanBuilder($name)->startSpan(); $span = $this->tracer
->spanBuilder($name)
->setSpanKind(SpanKind::KIND_SERVER)
->startSpan();
$span->activate(); $span->activate();

View File

@ -16,7 +16,6 @@
Config::sanity_check(); Config::sanity_check();
if (!init_plugins()) return; if (!init_plugins()) return;
UserHelper::login_sequence(); UserHelper::login_sequence();

View File

@ -14,6 +14,8 @@
if (!init_plugins()) return; if (!init_plugins()) return;
$span = OpenTelemetry\API\Trace\Span::getCurrent();
$method = (string)clean($_REQUEST["op"]); $method = (string)clean($_REQUEST["op"]);
// shortcut syntax for public (exposed) methods (?op=plugin--pmethod&...params) // shortcut syntax for public (exposed) methods (?op=plugin--pmethod&...params)
@ -39,27 +41,34 @@
user_error("Refusing to invoke method $method which starts with underscore.", E_USER_WARNING); user_error("Refusing to invoke method $method which starts with underscore.", E_USER_WARNING);
header("Content-Type: text/json"); header("Content-Type: text/json");
print Errors::to_json(Errors::E_UNAUTHORIZED); print Errors::to_json(Errors::E_UNAUTHORIZED);
$span->setAttribute('error', Errors::E_UNAUTHORIZED);
return; return;
} }
if (implements_interface($handler, "IHandler") && $handler->before($method)) { if (implements_interface($handler, "IHandler") && $handler->before($method)) {
$span->addEvent("construct/$method");
if ($method && method_exists($handler, $method)) { if ($method && method_exists($handler, $method)) {
$reflection = new ReflectionMethod($handler, $method); $reflection = new ReflectionMethod($handler, $method);
if ($reflection->getNumberOfRequiredParameters() == 0) { if ($reflection->getNumberOfRequiredParameters() == 0) {
$span->addEvent("invoke/$method");
$handler->$method(); $handler->$method();
} else { } else {
user_error("Refusing to invoke method $method which has required parameters.", E_USER_WARNING); user_error("Refusing to invoke method $method which has required parameters.", E_USER_WARNING);
header("Content-Type: text/json"); header("Content-Type: text/json");
print Errors::to_json(Errors::E_UNAUTHORIZED); print Errors::to_json(Errors::E_UNAUTHORIZED);
$span->setAttribute('error', Errors::E_UNAUTHORIZED);
} }
} else if (method_exists($handler, 'index')) { } else if (method_exists($handler, 'index')) {
$span->addEvent("index");
$handler->index(); $handler->index();
} }
$span->addEvent("after/$method");
$handler->after(); $handler->after();
return; return;
} }
header("Content-Type: text/plain"); header("Content-Type: text/plain");
print Errors::to_json(Errors::E_UNKNOWN_METHOD); print Errors::to_json(Errors::E_UNKNOWN_METHOD);
?> $span->setAttribute('error', Errors::E_UNKNOWN_METHOD);