philkra / elastic-apm-php-agent

PHP Agent for Elastic APM
MIT License
285 stars 95 forks source link

Transactions using "Distributed Tracing" do not show spans #125

Closed juanjobt closed 4 years ago

juanjobt commented 4 years ago

Hello

I'm using "Distributed Tracing" as it says in the documentation.

I need to trace a request. The origin of the request is generated in Laravel using this PHP agent. Laravel will call an external API EndPoint in Java using the Java agent.

Distributed Tracing is generated from my transaction.

$transactionTracing = new DistributedTracing($transaction->getTraceId(), $transaction->getId());

and then I add it to the header of my java API call from Guzzle.

$headers = [ elastic-apm-traceparent' => $request->DistributedTracing ];

Everything is working well and in Kibana I can see that the trace has been done

captura 1

But I have a problem. I can't display the spans that the Java process performs. As you can see in this image, just for the Java process I have 2 Spans that do not appear when I use "Distributed Tracing"

captura 2

I thought that maybe something is missing when sending the Transaction from the PHP-agent or maybe something is missing in the headers when calling the Java API.

Can anyone tell what's going on?

Thank you very much.

juanjobt commented 4 years ago

Hello again.

I've already found the problem

When the request to Laravel is generated from another APM Agent. I get the elastic-apm-traceparent in the header. If I get that parameter directly and use it in my external Java API call. Then it links correctly with included spans.

$headers = [ elastic-apm-traceparent' => $request->header('elastic-apm-traceparent')];

It is possible that the getDistributedTracing() function is not taking the traceFlags from the parent transaction when it is built.

Besides, if laravel is the origin and I use the default flags '00' the Java Agent cannot link the spans. If I use flags '01' everything is fine.

I leave an example of my code in my middleware if it helps you to understand better

$transaction = new Transaction($this->getTransactionName($request), [], $requestStartedOn);

//Distributed Tracing
if($transaction->getParentId() !== null){

  //should recover the elastic-apm-traceparent correctly but fails because traceFlags no init with parent flags
  //$request->merge(['DistributedTracing' => $transaction->getDistributedTracing()]);

  //retrieve from the header
  $request->merge(['DistributedTracing' => $request->header('elastic-apm-traceparent')]);

}else{

  //transaction is the origin of Tracing, generate a DistributedTracing
  $transactionTracing = new DistributedTracing($transaction->getTraceId(), $transaction->getId(), '01');
  $request->merge(['DistributedTracing' => $transactionTracing->__toString()]);

}

Thank you very much.