adding otlp/file exporter (#1465)

the spec has added in-development otlp file/stdout exporter. We already supported this programmatically, so
add some factories and sdk config to allow configuration from environment (only for stdout, per spec)
This commit is contained in:
Brett McBride 2025-01-09 10:50:03 +11:00 committed by GitHub
parent 7aa3e02eb0
commit 32e301ea6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
putenv('OTEL_TRACES_EXPORTER=otlp/stdout');
putenv('OTEL_LOGS_EXPORTER=otlp/stdout');
putenv('OTEL_METRICS_EXPORTER=otlp/stdout');
require __DIR__ . '/../../../vendor/autoload.php';
$instrumentation = new CachedInstrumentation('demo');
$instrumentation->tracer()->spanBuilder('root')->startSpan()->end();
$instrumentation->meter()->createCounter('cnt')->add(1);
$instrumentation->eventLogger()->emit('foo', 'hello, otel');
echo PHP_EOL . 'OTLP/stdout autoload example complete!';
echo PHP_EOL;

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Example;
require __DIR__ . '/../../../vendor/autoload.php';
use OpenTelemetry\SDK\Trace\TracerProviderFactory;
putenv('OTEL_TRACES_EXPORTER=otlp/stdout');
$factory = new TracerProviderFactory();
$tracerProvider = $factory->create();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');
$root = $span = $tracer->spanBuilder('root')->startSpan();
$scope = $span->activate();
for ($i = 0; $i < 3; $i++) {
// start a span, register some events
$span = $tracer->spanBuilder('loop-' . $i)->startSpan();
$span->setAttribute('remote_ip', '1.2.3.4')
->setAttribute('country', 'USA');
$span->addEvent('found_login' . $i, [
'id' => $i,
'username' => 'otuser' . $i,
]);
$span->addEvent('generated_session', [
'id' => md5((string) microtime(true)),
]);
$span->end();
}
$root->end();
$scope->detach();
echo PHP_EOL . 'OTLP/stdout example complete!';
echo PHP_EOL;
$tracerProvider->shutdown();

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Otlp;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Logs\LogRecordExporterFactoryInterface;
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
class StdoutLogsExporterFactory implements LogRecordExporterFactoryInterface
{
public function create(): LogRecordExporterInterface
{
$transport = (new StreamTransportFactory())->create('php://stdout', ContentTypes::NDJSON);
return new LogsExporter($transport);
}
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Otlp;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
class StdoutMetricExporterFactory implements MetricExporterFactoryInterface
{
public function create(): MetricExporterInterface
{
$transport = (new StreamTransportFactory())->create('php://stdout', ContentTypes::NDJSON);
return new MetricExporter($transport);
}
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Otlp;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Trace\SpanExporter\SpanExporterFactoryInterface;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
class StdoutSpanExporterFactory implements SpanExporterFactoryInterface
{
public function create(): SpanExporterInterface
{
$transport = (new StreamTransportFactory())->create('php://stdout', ContentTypes::NDJSON);
return new SpanExporter($transport);
}
}

View File

@ -2,8 +2,12 @@
declare(strict_types=1);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\SpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('otlp/stdout', \OpenTelemetry\Contrib\Otlp\StdoutSpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\MetricExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('otlp/stdout', \OpenTelemetry\Contrib\Otlp\StdoutMetricExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerTransportFactory('http', \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class);
\OpenTelemetry\SDK\Registry::registerLogRecordExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\LogsExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerLogRecordExporterFactory('otlp/stdout', \OpenTelemetry\Contrib\Otlp\StdoutLogsExporterFactory::class);

View File

@ -43,6 +43,7 @@ interface KnownValues
public const VALUE_HTTP_JSON = 'http/json';
public const VALUE_HTTP_NDJSON = 'http/ndjson';
public const VALUE_OTLP = 'otlp';
public const VALUE_OTLP_STDOUT = 'otlp/stdout';
public const VALUE_ZIPKIN = 'zipkin';
public const VALUE_PROMETHEUS = 'prometheus';
public const VALUE_WITH_SAMPLED_TRACE = 'with_sampled_trace';
@ -148,16 +149,19 @@ interface KnownValues
*/
public const OTEL_TRACES_EXPORTER = [
self::VALUE_OTLP,
self::VALUE_OTLP_STDOUT,
self::VALUE_ZIPKIN,
self::VALUE_NONE,
];
public const OTEL_METRICS_EXPORTER = [
self::VALUE_OTLP,
self::VALUE_OTLP_STDOUT,
self::VALUE_PROMETHEUS,
self::VALUE_NONE,
];
public const OTEL_LOGS_EXPORTER = [
self::VALUE_OTLP,
self::VALUE_OTLP_STDOUT,
self::VALUE_NONE,
];
/**