Saturday, 9 January 2021

AWS X-ray Send Traces Lumen

AWS X-ray Test Script in PHP Framework Lumen


I have tested sending traces to AWS X-Ray through API Gateway through Lumen.

I have used below PHP package:

https://packagist.org/packages/pkerrigan/xray

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Support\Response;
use Pkerrigan\Xray\Trace;
use Pkerrigan\Xray\Submission\DaemonSegmentSubmitter;
use Pkerrigan\Xray\Segment;

class TestController extends Controller
{
    /**
     * construct
     */
    public function __construct(Response $response) {
        $this->response = $response;
    }

    /**
     * test xray
     */
    public function xray() {    
      try {  
        if(env('ENABLE_XRAY')) {

              //echo "----Tracing Start 14 ----";

              Trace::getInstance()
                ->setTraceHeader($_SERVER['HTTP_X_AMZN_TRACE_ID'] ?? null)
                ->setName('api-1')
                ->setUrl("[API-1-URL]")
                ->setMethod('GET')
                ->begin();

              Trace::getInstance()
                ->setTraceHeader($_SERVER['HTTP_X_AMZN_TRACE_ID'] ?? null)
                ->setName('api-2')
                ->setUrl("[API-2-URL]")
                ->setMethod('GET')
                ->begin();
                
              Trace::getInstance()
                ->setTraceHeader($_SERVER['HTTP_X_AMZN_TRACE_ID'] ?? null)
                ->setName('api-3')
                ->setUrl("[API-3-URL]")
                ->setMethod('GET')
                ->begin();    
                  
              Trace::getInstance()
              ->getCurrentSegment()
              ->end()
              ->setResponseCode(http_response_code())
              ->submit(new DaemonSegmentSubmitter());
              return $this->response->success(array('xray-res' => 'success'));
          } else {
            return $this->response->success(array('xray-res' => 'no-set'));
          }
        } catch(Exception $e) {
          return $this->response->success(array('xray-res' => $e->getMessage()));
        }
    }
}

No comments:

Post a Comment

Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody. Please find below the comple...