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);
$method = (string)clean($method);
$span = Tracer::start(__FILE__);
register_shutdown_function(function() use ($span) {
$span->end();
});
startup_gettext();
$script_started = microtime(true);
@ -44,6 +38,8 @@
return;
}
$span = OpenTelemetry\API\Trace\Span::getCurrent();
header("Content-Type: text/json; charset=utf-8");
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\TracerProvider;
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\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
class Tracer {
/** @var Tracer $instance */
private static $instance;
private $tracerProvider;
/** @var OpenTelemetry\API\Trace\TracerInterface $tracer */
private $tracer;
@ -36,23 +41,29 @@ class Tracer {
), 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());
$span = $this->tracer->spanBuilder('root')
->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();
$scope = $span->activate();
register_shutdown_function(function() use ($span, $tracerProvider, $scope) {
register_shutdown_function(function() use ($span, $scope) {
$span->end();
$scope->detach();
$tracerProvider->shutdown();
$this->tracerProvider->shutdown();
});
}
@ -61,7 +72,10 @@ class Tracer {
* @return OpenTelemetry\API\Trace\SpanInterface
*/
private function _start(string $name) {
$span = $this->tracer->spanBuilder($name)->startSpan();
$span = $this->tracer
->spanBuilder($name)
->setSpanKind(SpanKind::KIND_SERVER)
->startSpan();
$span->activate();

View File

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

View File

@ -14,6 +14,8 @@
if (!init_plugins()) return;
$span = OpenTelemetry\API\Trace\Span::getCurrent();
$method = (string)clean($_REQUEST["op"]);
// 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);
header("Content-Type: text/json");
print Errors::to_json(Errors::E_UNAUTHORIZED);
$span->setAttribute('error', Errors::E_UNAUTHORIZED);
return;
}
if (implements_interface($handler, "IHandler") && $handler->before($method)) {
$span->addEvent("construct/$method");
if ($method && method_exists($handler, $method)) {
$reflection = new ReflectionMethod($handler, $method);
if ($reflection->getNumberOfRequiredParameters() == 0) {
$span->addEvent("invoke/$method");
$handler->$method();
} else {
user_error("Refusing to invoke method $method which has required parameters.", E_USER_WARNING);
header("Content-Type: text/json");
print Errors::to_json(Errors::E_UNAUTHORIZED);
$span->setAttribute('error', Errors::E_UNAUTHORIZED);
}
} else if (method_exists($handler, 'index')) {
$span->addEvent("index");
$handler->index();
}
$span->addEvent("after/$method");
$handler->after();
return;
}
header("Content-Type: text/plain");
print Errors::to_json(Errors::E_UNKNOWN_METHOD);
?>
$span->setAttribute('error', Errors::E_UNKNOWN_METHOD);