Napp/xray-laravel

Auto hook-up with downstream HTTP client requests?

Closed this issue · 2 comments

I tested using Laravel's recommended Guzzle HTTP client request pattern to a traced back-end API tier. It does not look like the trace ID in Laravel is being handed down to the back-end.

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Http;
use Napp\Xray\Facades\Xray;

class XRayController extends Controller
{
    public function trace()
    {
		Xray::addSegment('XRayController');

		// HTTP call to back-end tier.
		$apiUrl = "http://localhost:8081/api/Tier";
		$response = Http::get($apiUrl);
		$delayTime = $response->body();

		Xray::endSegment('XRayController');
      
		return view('traceapi', ['timing' => $delayTime]);
    }
}

Where can the client get the trace ID to forward into the X-Amzn-Trace-Id header?

Perusing the libraries' code I think I know how to extract the trace ID from the underlying trace/segment object.

    public function trace()
    {
		$selfSegment = 'XRayController';
		$backendSegment = 'XRayBackApi';
		Xray::addSegment($selfSegment);

		// HTTP call to back-end tier.
		$xrayHeader = "X-Amzn-Trace-Id";
		$traceId = Xray::tracer()->getTraceId();
		$traceIdString = "Root=" . $traceId . ";Sampled=1";

		$apiUrl = "http://localhost:8081/api/Tier";

		Xray::addSegment($backendSegment );
		$response = Http::withHeaders([
			$xrayHeader => $traceIdString
		])->get($apiUrl);
		Xray::endSegment($backendSegment );
		$delayTime = $response->body();

		Xray::endSegment($selfSegment);

		return view('traceapi', ['timing' => $delayTime]);
    }

While the traces now show the correlated tiers together, the service map nodes are not shown to be linked though.

Based on Patrick Kerrigan's advice, applied current app's segment ID as parent ID for the back-end tier.

Xray::addSegment($selfSegment);

$xrayHeader = "X-Amzn-Trace-Id";
$traceId = Xray::tracer()->getTraceId();
$selfSegmentId = Xray::current()->getId();
// Hard-coded smapling rate for now.
$samplingRate = 1;
$traceIdString = "Root=" . $traceId . ";Parent=" . $selfSegmentId . ";Sampled=" . $samplingRate;
$apiUrl = "http://localhost:8081/api/Tier";

$response = Http::withHeaders([
	$xrayHeader => $traceIdString
])->get($apiUrl);
$delayTime = $response->body();

Xray::endSegment($selfSegment);
return view('traceapi', ['timing' => $delayTime]);

The X-Ray service map can then tie together the service node tiers.