subzerobo/elastic-apm-php-agent

Example

Pidz-b opened this issue · 3 comments

Hello,

Your project seems promising for our system.
Could you maybe incorporate an example in the README of how to use your code?

Or is the project not completed yet?

Best regards

Hello,

Project is completed, couple of examples will be updated in next 3 days

Kind Regards

Hello,

I'm searching for the same thing as Pdz-b. Your project seems very promising, can you please keep in touch when the examples would be updated ?

Regards

Hello

We successfully use this library like this in Symfony for HTTP request tracing

<?php


namespace App\Performance;


use Subzerobo\ElasticApmPhpAgent\ApmAgent;
use Subzerobo\ElasticApmPhpAgent\Wrappers\TransactionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;


class HttpTransactionListener implements EventSubscriberInterface
{
    private $apmAgent;

    /** @var TransactionEvent */
    private $transactionEvent;

    /**
     * HTTPTransactionListener constructor.
     */
    public function __construct(ApmAgent $apmAgent)
    {
        $this->apmAgent = $apmAgent;
    }

    public static function getSubscribedEvents()
    {
        return [
            RequestEvent::class => ['onKernelRequest', 4096],
            TerminateEvent::class => ['onKernelTerminate', 4096]
        ];
    }

    public function onKernelRequest(RequestEvent $event)
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $this->transactionEvent = $this->apmAgent->startTransaction();
    }

    public function onKernelTerminate(TerminateEvent $event)
    {
        if (!$this->transactionEvent) {
            return;
        }

        $response = $event->getResponse();

        $this->transactionEvent->setResult("HTTP " . $response->getStatusCode());
        $this->transactionEvent->setType("request");
        $this->transactionEvent->setIsSampled(true);
        $this->transactionEvent->setResponseFromArray([
                'finished'     => true,
                'headers_sent' => true,
                'status_code'  => $response->getStatusCode(),]
        );
        $this->transactionEvent->stop();

        $this->apmAgent->send();
    }
}

What are you trying to achieve ?