opentelemetry-php/examples/autoload_sdk.php

27 lines
887 B
PHP

<?php
declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Logs\LogRecord;
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
putenv('OTEL_TRACES_EXPORTER=console');
putenv('OTEL_METRICS_EXPORTER=console');
putenv('OTEL_LOGS_EXPORTER=console');
putenv('OTEL_PROPAGATORS=b3,baggage,tracecontext');
echo 'autoloading SDK example starting...' . PHP_EOL;
// Composer autoloader will execute SDK/_autoload.php which will register global instrumentation from environment configuration
require dirname(__DIR__) . '/vendor/autoload.php';
$instrumentation = new \OpenTelemetry\API\Instrumentation\CachedInstrumentation('demo');
$instrumentation->tracer()->spanBuilder('root')->startSpan()->end();
$instrumentation->meter()->createCounter('cnt')->add(1);
$instrumentation->logger()->emit((new LogRecord('hello, otel'))->setEventName('foo'));
echo 'Finished!' . PHP_EOL;