census-instrumentation / opencensus-php

A stats collection and distributed tracing framework
Apache License 2.0
202 stars 83 forks source link

Expose rootSpanId for ability to set it as parentSpanId #199

Open iamacarpet opened 6 years ago

iamacarpet commented 6 years ago

Is it possible to expose the rootSpan->spanId() from OpenCensus\Trace\RequestHandler?

Currently it is private private $rootSpan; and I can't see any methods that expose it.

I'm adding tracing for Laravel to our shared library, to run on App Engine Standard (PHP 7.2).

The examples provided creates a service provider which uses the LARAVEL_START constant set at the top of public/index.php to create a span with the startTime backdated to when the application started.

<?php

namespace A1comms\GaeSupportLaravel\Trace;

use OpenCensus\Trace\Tracer;
use Illuminate\Support\ServiceProvider;

class TraceServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if ((!is_gae()) || (php_sapi_name() == 'cli')) {
            return;
        }

        // Create a span that starts from when Laravel first boots (public/index.php)
        Tracer::inSpan(['name' => 'laravel/bootstrap', 'startTime' => LARAVEL_START], function () {});
    }
}

At the moment, this is firing after some other trace events have already taken place, so the tree looks a little bit weird (see screenshow below).

screenshot 1535969804

I'd ideally like to be able to pass rootSpan->spanId() into that spans options as parentSpanId, which should fix the formatting.

iamacarpet commented 6 years ago

Looking around at this a little more, I wanted to check that even if the parentSpanId is passed through, it would be applied.

Does this imply that it won't be?

As if there is a parent as detected internally by the extension, it'll always prefer that to anything in the span options?

https://github.com/census-instrumentation/opencensus-php/blob/master/ext/opencensus_trace_span.c#L640

I saw in some code comments that setting "parentSpanId" to null in the span options would make it default to the rootSpan->spanId(), but this didn't work either :(.