PHP code example of stacktracer / stacktracer-symfony
1. Go to this page and download the library: Download stacktracer/stacktracer-symfony library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stacktracer / stacktracer-symfony example snippets
use Stacktracer\SymfonyBundle\Service\TracingService;
use Stacktracer\SymfonyBundle\Model\Trace;
use Stacktracer\SymfonyBundle\Model\Span;
class MyController
{
public function __construct(
private TracingService $stacktracer
) {}
public function someAction()
{
// Add breadcrumbs for debugging (linked to current span)
$this->stacktracer->addBreadcrumb('user', 'User clicked checkout');
// Create a span for a unit of work
$this->stacktracer->withSpan('process-order', function(Span $span) {
$span->setAttribute('order.id', $orderId);
// Nested span for database query
$this->stacktracer->withSpan('db.query', function($dbSpan) {
$dbSpan->setAttribute('db.statement', 'SELECT * FROM orders');
// ... database work
}, Span::KIND_CLIENT);
// ... process order
});
try {
// Your code
} catch (\Exception $e) {
// Capture exception (auto-linked to current span)
$this->stacktracer->captureException($e, [
'order_id' => $orderId,
]);
}
// Capture a custom message
$this->stacktracer->captureMessage(
'Order completed',
Trace::LEVEL_INFO,
['order_id' => $orderId]
);
}
}
use Stacktracer\SymfonyBundle\Model\FeatureFlag;
class MyController
{
public function __construct(
private TracingService $stacktracer
) {}
public function checkout()
{
// Declare a single feature flag with variant
$this->stacktracer->addFeatureFlag('Checkout button color', 'Blue');
$this->stacktracer->addFeatureFlag('New checkout flow');
// Declare multiple feature flags at once
$this->stacktracer->addFeatureFlags([
new FeatureFlag('Special offer', 'Free Coffee'),
new FeatureFlag('Premium shipping'),
]);
// Remove flags when features are deactivated
$this->stacktracer->clearFeatureFlag('Checkout button color');
// Clear all flags
$this->stacktracer->clearFeatureFlags();
}
}
// Use an impression listener to automatically track experiments
class StacktracerImpressionListener implements \SplitIO\Sdk\ImpressionListener
{
public function __construct(private TracingService $stacktracer) {}
public function logImpression($data)
{
$this->stacktracer->addFeatureFlag(
$data['impression']['feature'],
$data['impression']['treatment']
);
}
}