Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Api Profiler Bundle Laravel Package

chrisjohnson00/api-profiler-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require chrisjohnson00/api-profiler-bundle:dev-master
    

    Add to AppKernel.php:

    new ChrisJohnson00\ApiProfilerBundle\ChrisJohnson00ApiProfilerBundle(),
    
  2. First Use Case:

    • Enable the profiler in config.yml:
      framework:
          profiler: { only_exceptions: false }
      
    • Access /_profiler/api to view API-specific metrics (e.g., request/response times, payload sizes, headers).

Implementation Patterns

Core Workflows

  1. Debugging API Endpoints:

    • Use the toolbar to inspect:
      • Request Metrics: HTTP method, URI, headers, body.
      • Response Metrics: Status code, headers, body, execution time.
      • Performance: Color-coded thresholds (configurable via config.yml under api_profiler).
  2. Integration with Laravel (Symfony Bridge):

    • Symfony Profiler Compatibility:
      • Wrap Laravel’s Request/Response in Symfony’s equivalents (e.g., Symfony\Component\HttpFoundation\Request) for full profiling.
      • Example middleware to convert requests:
        use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
        
        $symfonyRequest = SymfonyRequest::createFromGlobals();
        $profiler->collect($symfonyRequest, $response);
        
  3. Custom Data Collection:

    • Extend the profiler by overriding the collect() method in a custom service:
      // src/Service/ApiProfilerCollector.php
      class ApiProfilerCollector extends \ChrisJohnson00\ApiProfilerBundle\Collector\ApiProfilerCollector
      {
          public function collect(Request $request, Response $response, DataCollector $data)
          {
              parent::collect($request, $response, $data);
              $data->addCustomMetric('business_logic_time', $this->measureBusinessLogic());
          }
      }
      
    • Register the collector in services.yml:
      services:
          api_profiler.collector:
              class: Service\ApiProfilerCollector
              tags:
                  - { name: 'api_profiler.collector' }
      
  4. Conditional Profiling:

    • Disable for non-debug environments or specific routes:
      if (!$this->app->environment('local')) {
          $profiler->disable();
      }
      

Gotchas and Tips

Pitfalls

  1. Symfony Dependency:

    • Laravel-Specific Issue: The bundle assumes Symfony’s Request/Response objects. Use a bridge (e.g., symfony/http-foundation) to convert Laravel objects:
      use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
      
      $symfonyRequest = SymfonyRequest::create(
          $request->path(),
          $request->method(),
          $request->query->all(),
          $request->request->all(),
          $request->server->all(),
          $request->cookies->all(),
          $request->files->all()
      );
      
  2. Deprecated Bundle:

    • Last Release (2013): Expect compatibility issues with modern Symfony/Laravel. Test thoroughly or fork the repo for updates.
  3. Threshold Configuration:

    • Default thresholds (e.g., 500ms for warnings) may not suit Laravel’s performance characteristics. Adjust in config.yml:
      api_profiler:
          warning_threshold: 1000  # 1 second
          error_threshold:   2000  # 2 seconds
      

Debugging Tips

  1. Toolbar Not Showing:

    • Ensure profiler: { only_exceptions: false } is set in config.yml.
    • Verify the bundle is registered in AppKernel.php.
  2. Missing API Data:

    • Confirm the profiler is collecting data for API routes. Add a ProfilerListener to force collection:
      use Symfony\Component\HttpKernel\Event\GetResponseEvent;
      
      $profiler->collect($event->getRequest(), $event->getResponse());
      
  3. Performance Overhead:

    • Disable in production:
      if (app()->environment('production')) {
          $profiler->disable();
      }
      

Extension Points

  1. Custom Data Panels:

    • Override the ApiProfilerCollector to add tabs (e.g., database queries, cache hits):
      public function getData()
      {
          return array_merge(parent::getData(), [
              'cache_stats' => $this->getCacheStats(),
          ]);
      }
      
  2. API-Specific Filters:

    • Filter profiled data by route name or API version:
      if (strpos($request->getPathInfo(), '/api/v1') === 0) {
          $profiler->collect($request, $response);
      }
      
  3. Export Profiler Data:

    • Extend the bundle to export data to JSON/CSV for monitoring:
      $profilerData = $profiler->getData();
      file_put_contents(
          storage_path('logs/api_profiler.json'),
          json_encode($profilerData)
      );
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator