Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Simplify quickstart (#142)
Browse files Browse the repository at this point in the history
* Simplify silex example for use as quickstart

* Update silex README with updates
  • Loading branch information
chingor13 committed Mar 22, 2018
1 parent b94682d commit 842bbc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/silex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ beginning of our application. In `web/index.php`:
require_once __DIR__ . '/../vendor/autoload.php';

// Configure and start the OpenCensus Tracer
$exporter = new OpenCensus\Trace\Exporter\StackdriverExporter();
$exporter = new OpenCensus\Trace\Exporter\EchoExporter();
OpenCensus\Trace\Tracer::start($exporter);

$app = new Silex\Application();
// ... rest of the application
```

In this example, we configured `StackdriverExporter`, but you can configure
In this example, we configured `EchoExporter`, but you can configure
any exporter here. You can also enable any other integrations here.
25 changes: 14 additions & 11 deletions examples/silex/web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@

// Configure and start the OpenCensus Tracer
use OpenCensus\Trace\Tracer;
$exporter = new OpenCensus\Trace\Exporter\StackdriverExporter();
$exporter = new OpenCensus\Trace\Exporter\EchoExporter();
Tracer::start($exporter);

function fib($n)
{
if ($n < 3) {
return $n;
}
return fib($n - 1) + fib($n - 2);
return Tracer::inSpan([
'name' => 'fib',
'attributes' => [
'n' => $n
]
], function () use ($n) {
if ($n < 3) {
return $n;
}
return fib($n - 1) + fib($n - 2);
});
}

$app = new Silex\Application();
Expand All @@ -21,14 +28,10 @@ function fib($n)
return 'Hello World!';
});

$app->get('/hello/{name}', function ($name) use ($app) {
return 'Hello ' . $app->escape($name);
});

$app->get('/fib/{n}', function ($n) use ($app) {
$n = (int) $n;
$fib = Tracer::inSpan(['name' => 'recursiveFib'], 'fib', [$n]);
return sprintf('The %dth Fibonacci number is %d', $n, $fib);
$fib = fib($n);
return sprintf('The %dth Fibonacci number is %d.', $n, $fib);
});

$app->run();

0 comments on commit 842bbc5

Please sign in to comment.