Skip to content

Conversation

@HazAT
Copy link
Member

@HazAT HazAT commented Sep 3, 2020

Installation

Read
https://docs.sentry.io/performance-monitoring/performance/
to understand the concept behind Tracing/Performance in Sentry.

Make sure you have following in you composer.json

"require": {
        "sentry/sentry-laravel": "2.0.0-beta1"
},
"minimum-stability": "dev",
"prefer-stable": true,

After that run composer update.

After that add 'traces_sample_rate' => 1.0, to the file in config/sentry.php.
Note that the value should be larger than 0.0 and smaller or equal than 1.0 (to send everything).
More info can be found here: https://docs.sentry.io/performance-monitoring/getting-started/

Connecting your frontend

To setup tracing for your Vue frontend, follow: https://docs.sentry.io/performance-monitoring/getting-started/?platform=vue

An additional step you should take is, add this to your blade template rendering the <head> tag {!! Sentry\Laravel\Integration::sentryTracingMeta() !!}
This adds a meta tag similar to <meta name="sentry-trace" content="ae02a316231d490c870b14f27aba8d29-c4a72ffd39444c84-1"/> to your header and tells the frontend code to pick up the trace.

screencapture-sentry-io-organizations-sentry-test-performance-php-7a3111c6c9f742b2a389052fb486aa7e-2020-09-03-14_38_12

* feat: Add Tracing Middleware

Record sql queries as spans

* feat: Auto prepend middleware

Instrument view renders

* ref: Change code comments

* ref: Use terminate to send transaction

* Rename view engine decorator

* Improve transaction context name/description

* Prevent crashes when using missing defines

* Do not remove all leading `/` keep 1

* Add fallback autoload + bootstrap span

* Set the transaction name from the event handler

* Cleanup query span

* Prevent errors on Laravel 5.3 and below

* CS

* feat: Use correct route and add data

* ref: Add name

* fix: Route naming

* feat: Start transaction in serviceProvider

Move all renders as a child

* ref: Rename to view.render

* ref: Move back to starting transaction in middleware

Use fromTraceparent function to start a trace

* ref: Small refactor

* feat: Update composer.json

* ref: Add traces_sample_rate

* Refactor active span retrieval

* Guard against the active span not being set

* Docblock updates

* Correctly return rendered view without span

* Move tracing related code to the Tracing namespace

* Improve the route name detection order

* Do not use route names ending with a `.`

* Fix not wrapping the view enines when resolver is already resolved

* feat: Rework code to use transaction on the hub

Co-authored-by: Alex Bouma <[email protected]>
@HazAT HazAT self-assigned this Sep 3, 2020
@arcdigital
Copy link

Could you merge Laravel 8 support into this branch too?

# Conflicts:
#	CHANGELOG.md
#	composer.json
#	src/Sentry/Laravel/Version.php
@stayallive
Copy link
Collaborator

@arcdigital yep, done.

@arcdigital
Copy link

Thanks!

Co-authored-by: Alex Bouma <[email protected]>
@danijelk
Copy link

Any ETA on this PR?

@danijelk
Copy link

Problem 1
- Can only install one of: sentry/sdk[3.0.0-beta1, 2.2.0].
- Can only install one of: sentry/sdk[3.0.0-beta1, 2.2.0].
- Can only install one of: sentry/sdk[3.0.0-beta1, 2.2.0].
- sentry/sentry-laravel 2.0.0-beta1 requires sentry/sdk 3.0.0-beta1 -> satisfiable by sentry/sdk[3.0.0-beta1].
- Installation request for sentry/sentry-laravel 2.0.0-beta1 -> satisfiable by sentry/sentry-laravel[2.0.0-beta1].
- Installation request for sentry/sdk (locked at 2.2.0) -> satisfiable by sentry/sdk[2.2.0].

@stayallive
Copy link
Collaborator

@danijelk do you have sentry/sdk in listed in your own composer.json? The error says sentry/sdk (locked at 2.2.0), if you have remove sentry/sdk from your direct dependencies that should fix it. If not please share your composer.json so we can check it out.

@danijelk
Copy link

@stayallive thanks for reply

Indeed it's not in the list that's why the error is so weird.

composer remove sentry/sdk
sentry/sdk is not required in your composer.json and has not been removed
Loading composer repositories with package information
Updating dependencies (including require-dev)
...

but since 1.9 was installed that's why it couldn't upgrade, so had to composer remove old sentry/sentry-laravel, and then add it manually to composer.json - and first then upgrade it :) whups 👍

@georgeboot
Copy link

georgeboot commented Sep 21, 2020

@HazAT what is the current roadmap for this PR?

If you're willing to accept a feature requests:

  • it would be super powerful to enable tracing per route using middleware or so. That way I can only perform traces of parts of the system that I'm interested at, instead of setting a global trace rate.
  • Laravel Livewire's background calls all show up as the same route name, as they are technically the same route. How would one go and customise the transaction name from inside a controller? Or should we add code to this package to handle Livewire?

* feat: Add publish command

* fix: Typos
@HazAT
Copy link
Member Author

HazAT commented Sep 22, 2020

@georgeboot
Hey, of course!
Thanks for the feedback and those are two great suggestions.
I will make sure we at least have an example, how to for example say, use a middleware to do custom sampling for routes.

wrt to Transaction names, so this is probably only a matter of documentation. You can get the active transaction by calling and then change the name.

$transaction = SentrySdk::getCurrentHub()->getTransaction();
        
if (null !== $transaction) {
    $transaction->setName('your name instead');
}

Hope this helps.

@HazAT
Copy link
Member Author

HazAT commented Sep 24, 2020

@georgeboot Here is a middleware that you can use in some of your controllers to achieve what you want.

<?php

namespace App\Http\Middleware;

use Closure;
use Sentry\SentrySdk;
use Sentry\Tracing\Transaction;

class SentryExcludeTransactionMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (app()->bound('sentry')) {
            $transaction = SentrySdk::getCurrentHub()->getTransaction();
            
            // Do additional checks here on $request->route() or whatever to decide
            // if you want to send the transaction or not
            
            if ($transaction instanceof Transaction) {
                // $transaction->setSampled(false); // __DONT__ SEND TRANSACTION
                // $transaction->setSampled(true); // __DO__ SEND TRANSACTION  
                $transaction->setSampled(false);
            }
        }

        return $next($request);
    }
}

@HazAT
Copy link
Member Author

HazAT commented Sep 24, 2020

EDIT: getsentry/sentry-php#1096

How to instrument Guzzle HTTP Calls:

<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\RequestInterface;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
use GuzzleHttp\Handler\CurlHandler;

/**
 * Class HomeController.
 */
class HomeController extends Controller
{
    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        function addSentrySpans() {
            return function (callable $handler) {
                return function (RequestInterface $request, array $options) use ($handler) {
                    $transaction = SentrySdk::getCurrentHub()->getTransaction();
                    $span = null;
                    if ($transaction instanceof Transaction) {
                        $spanContext = new SpanContext();
                        $spanContext->setOp('http.guzzle');
                        $spanContext->setDescription($request->getMethod() . ' ' . $request->getUri());
                        $span = $transaction->startChild($spanContext);
                    }
                    $result = $handler($request, $options);
                    if (null !== $span) {
                        $span->finish();
                    }
                    return $result;
                };
            };
        }

        $stack = new HandlerStack();
        $stack->setHandler(new CurlHandler());
        $stack->push(addSentrySpans());

        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => 'http://httpbin.org',
            // You can set any number of default request options.
            'timeout'  => 2.0,
            'handler' => $stack
        ]);
        $response = $client->request('GET', '/get');

.....

@HazAT HazAT merged commit 95c1ab2 into master Sep 28, 2020
@HazAT HazAT deleted the 2.x branch September 28, 2020 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants