Skip to content

Instantly share code, notes, and snippets.

@ekkinox
Created October 9, 2024 13:26
Show Gist options
  • Save ekkinox/8a4788888453760b165439e3c93e57b4 to your computer and use it in GitHub Desktop.
Save ekkinox/8a4788888453760b165439e3c93e57b4 to your computer and use it in GitHub Desktop.
OTEL Noop
{
"name": "jonathan/sandbox",
"require": {
"slim/slim": "^4.14",
"slim/psr7": "^1.7",
"monolog/monolog": "^3.7",
"open-telemetry/opentelemetry-logger-monolog": "^1.0",
"open-telemetry/sdk": "^1.1",
"open-telemetry/exporter-otlp": "^1.1",
"symfony/http-client": "^7.1",
"open-telemetry/api": "^1.1",
"open-telemetry/sem-conv": "^1.27",
"guzzlehttp/guzzle": "^7.9"
},
"autoload": {
"psr-4": {
"Jonathan\\Sandbox\\": "src/"
}
},
"authors": [
{
"name": "Jonathan Vuillemin",
"email": "[email protected]"
}
],
"config": {
"allow-plugins": {
"php-http/discovery": true,
"tbachert/spi": true
}
}
}
<?php
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SDK\Trace\NoopTracerProvider;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/vendor/autoload.php';
$tracerProvider = TracerProvider::builder()
->setSampler(new ParentBased(new AlwaysOnSampler()))
->addSpanProcessor(
new SimpleSpanProcessor(
new SpanExporter(
(new StreamTransportFactory())->create('php://stdout', 'application/json')
)
)
)
->build();
// uncomment here for noop
//$tracerProvider = new NoopTracerProvider();
$tracer = $tracerProvider->getTracer(
'demo',
'0.1.0',
'https://opentelemetry.io/schemas/1.24.0'
);
Sdk::builder()
->setTracerProvider($tracerProvider)
->setPropagator(TraceContextPropagator::getInstance())
->setAutoShutdown(true)
->buildAndRegisterGlobal();
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response) use ($tracer) {
$span = $tracer->spanBuilder("example")->startSpan();
$span->end();
return $response;
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment