create("http://$opentelemetry_host/v1/traces", 'application/x-protobuf'); $exporter = new SpanExporter($transport); } else { $exporter = new InMemoryExporter(); } $tracerProvider = new TracerProvider(new SimpleSpanProcessor($exporter)); $this->tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); $context = TraceContextPropagator::getInstance()->extract(getallheaders()); $span = $this->tracer->spanBuilder(Config::get(Config::OPENTELEMETRY_SERVICE)) ->setParent($context) ->startSpan(); $span->activate(); register_shutdown_function(function() use ($span, $tracerProvider) { $span->end(); $tracerProvider->shutdown(); }); } /** * @param string $name * @param array|array> $tags * @param array $args * @return OpenTelemetry\API\Trace\SpanInterface */ private function _start(string $name, array $tags = [], array $args = []) { $span = $this->tracer->spanBuilder($name)->startSpan(); foreach ($tags as $k => $v) { $span->setAttribute($k, $v); } $span->setAttribute("func.args", json_encode($args)); $span->activate(); return $span; } /** * @param string $name * @param array|array> $tags * @param array $args * @return OpenTelemetry\API\Trace\SpanInterface */ public static function start(string $name, array $tags = [], array $args = []) { return self::get_instance()->_start($name, $tags, $args); } public static function get_instance() : Tracer { if (self::$instance == null) self::$instance = new self(); return self::$instance; } }