opentelemetry-php icon indicating copy to clipboard operation
opentelemetry-php copied to clipboard

[opentelemetry-php-contrib] is there a way to override an auto-instrumentation span name?

Open Azuka opened this issue 6 months ago • 5 comments

I'm currently using the Laravel auto-instrumentation hook. It currently sets the root span name in the post-process, which means for specific routes (e.g. a fallback) there is no way to override the span name:

https://github.com/opentelemetry-php/contrib-auto-laravel/blob/main/src/Hooks/Illuminate/Contracts/Http/Kernel.php#L90

Azuka avatar Jun 03 '25 03:06 Azuka

I don’t know the answer to your question, but here is the explanation of why the span name is changed post-process: https://github.com/open-telemetry/opentelemetry-php-contrib/pull/219

smaddock avatar Jun 03 '25 05:06 smaddock

The current answer is "no". https://opentelemetry.io/docs/specs/semconv/http/http-spans/#name says that:

HTTP span names SHOULD be {method} {target} if there is a (low-cardinality) target available. If there is no (low-cardinality) {target} available, HTTP span names SHOULD be {method}.

Instrumentation MUST NOT default to using URI path as a {target}

Other value MAY be provided through custom hooks at span start time or later.

In your case, does the fallback have a low-cardinality value available (probably a route name) that could be used?

edit: related to and possibly would be resolved by https://github.com/open-telemetry/opentelemetry-php-contrib/pull/305 ?

brettmc avatar Jun 13 '25 04:06 brettmc

@smaddock, @brettmc I may have a somewhat atypical case: I'm working on an application where most of the code is written in the defunct Kohana framework, for which I've been using Laravel for all new features.

There's a double routing issue here where the incoming catch-all route is defined as:

Route::any('{fallbackPlaceholder}', 'KohanaController')
    ->where('fallbackPlaceholder', '.*')->fallback();

and in the Kohana base controller, I have:


    /**
     * Called before execution
     * @throws \Kohana_Exception
     */
    public function before()
    {
        if (extension_loaded('opentelemetry')) {
            $name = sprintf('%s %s / %s legacy', $this->request->method(), get_class($this), $this->request->action());
            $span = $this->request->getRequest()?->attributes?->get(SpanInterface::class) ?? OpenTelemetry\API\Trace\LocalRootSpan::current(); // tried this because I wanted to compare the root and request spans
            $span->setAttribute('request.controller', get_class($this)); // This
            $span->setAttribute('request.action', $this->request->action()); // and this are being used because I can't rename the span
            $span->setAttribute('request.IP', Request::$client_ip);
            $span->updateName($name); // Name update doesn't work unfortunately
        }
    }

Since the Laravel hook executes post request with no way to override, all my requests show up as GET /{fallbackPlaceholder} or POST /{fallbackPlaceholder} which makes it very hard to troubleshoot especially on dynamic routes.

Azuka avatar Jun 17 '25 05:06 Azuka

I’m going to assume rewriting your app to be pure Laravel is out of the question or you would have done it… in which case it sounds like you may be better suited ditching the Laravel HTTP instrumentation and instrumenting the Kohana controller instead? I’m not familiar with either framework, unfortunately.

smaddock avatar Jun 17 '25 05:06 smaddock

I’m going to assume rewriting your app to be pure Laravel is out of the question or you would have done it… in which case it sounds like you may be better suited ditching the Laravel HTTP instrumentation and instrumenting the Kohana controller instead? I’m not familiar with either framework, unfortunately.

Unfortunately that's not an option at the moment. The entire API layer is written in Laravel, so I'd like to keep that instrumentation.

Azuka avatar Jun 17 '25 05:06 Azuka

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 19 '25 05:07 stale[bot]

Closing. I was able to work around this by patching using https://github.com/cweagans/composer-patches

Azuka avatar Aug 10 '25 14:08 Azuka