Skip to content

Commit 32e301e

Browse files
authored
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)
1 parent 7aa3e02 commit 32e301e

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Example;
6+
7+
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
8+
9+
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
10+
putenv('OTEL_TRACES_EXPORTER=otlp/stdout');
11+
putenv('OTEL_LOGS_EXPORTER=otlp/stdout');
12+
putenv('OTEL_METRICS_EXPORTER=otlp/stdout');
13+
14+
require __DIR__ . '/../../../vendor/autoload.php';
15+
16+
$instrumentation = new CachedInstrumentation('demo');
17+
18+
$instrumentation->tracer()->spanBuilder('root')->startSpan()->end();
19+
$instrumentation->meter()->createCounter('cnt')->add(1);
20+
$instrumentation->eventLogger()->emit('foo', 'hello, otel');
21+
22+
echo PHP_EOL . 'OTLP/stdout autoload example complete!';
23+
echo PHP_EOL;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Example;
6+
7+
require __DIR__ . '/../../../vendor/autoload.php';
8+
9+
use OpenTelemetry\SDK\Trace\TracerProviderFactory;
10+
11+
putenv('OTEL_TRACES_EXPORTER=otlp/stdout');
12+
$factory = new TracerProviderFactory();
13+
$tracerProvider = $factory->create();
14+
15+
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');
16+
17+
$root = $span = $tracer->spanBuilder('root')->startSpan();
18+
$scope = $span->activate();
19+
20+
for ($i = 0; $i < 3; $i++) {
21+
// start a span, register some events
22+
$span = $tracer->spanBuilder('loop-' . $i)->startSpan();
23+
24+
$span->setAttribute('remote_ip', '1.2.3.4')
25+
->setAttribute('country', 'USA');
26+
27+
$span->addEvent('found_login' . $i, [
28+
'id' => $i,
29+
'username' => 'otuser' . $i,
30+
]);
31+
$span->addEvent('generated_session', [
32+
'id' => md5((string) microtime(true)),
33+
]);
34+
35+
$span->end();
36+
}
37+
$root->end();
38+
$scope->detach();
39+
echo PHP_EOL . 'OTLP/stdout example complete!';
40+
41+
echo PHP_EOL;
42+
$tracerProvider->shutdown();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Otlp;
6+
7+
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
8+
use OpenTelemetry\SDK\Logs\LogRecordExporterFactoryInterface;
9+
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
10+
11+
class StdoutLogsExporterFactory implements LogRecordExporterFactoryInterface
12+
{
13+
public function create(): LogRecordExporterInterface
14+
{
15+
$transport = (new StreamTransportFactory())->create('php://stdout', ContentTypes::NDJSON);
16+
17+
return new LogsExporter($transport);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Otlp;
6+
7+
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
8+
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
9+
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
10+
11+
class StdoutMetricExporterFactory implements MetricExporterFactoryInterface
12+
{
13+
public function create(): MetricExporterInterface
14+
{
15+
$transport = (new StreamTransportFactory())->create('php://stdout', ContentTypes::NDJSON);
16+
17+
return new MetricExporter($transport);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Otlp;
6+
7+
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
8+
use OpenTelemetry\SDK\Trace\SpanExporter\SpanExporterFactoryInterface;
9+
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
10+
11+
class StdoutSpanExporterFactory implements SpanExporterFactoryInterface
12+
{
13+
public function create(): SpanExporterInterface
14+
{
15+
$transport = (new StreamTransportFactory())->create('php://stdout', ContentTypes::NDJSON);
16+
17+
return new SpanExporter($transport);
18+
}
19+
}

src/Contrib/Otlp/_register.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
declare(strict_types=1);
44
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\SpanExporterFactory::class);
5+
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('otlp/stdout', \OpenTelemetry\Contrib\Otlp\StdoutSpanExporterFactory::class);
6+
57
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\MetricExporterFactory::class);
8+
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('otlp/stdout', \OpenTelemetry\Contrib\Otlp\StdoutMetricExporterFactory::class);
69

710
\OpenTelemetry\SDK\Registry::registerTransportFactory('http', \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class);
811

912
\OpenTelemetry\SDK\Registry::registerLogRecordExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\LogsExporterFactory::class);
13+
\OpenTelemetry\SDK\Registry::registerLogRecordExporterFactory('otlp/stdout', \OpenTelemetry\Contrib\Otlp\StdoutLogsExporterFactory::class);

src/SDK/Common/Configuration/KnownValues.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface KnownValues
4343
public const VALUE_HTTP_JSON = 'http/json';
4444
public const VALUE_HTTP_NDJSON = 'http/ndjson';
4545
public const VALUE_OTLP = 'otlp';
46+
public const VALUE_OTLP_STDOUT = 'otlp/stdout';
4647
public const VALUE_ZIPKIN = 'zipkin';
4748
public const VALUE_PROMETHEUS = 'prometheus';
4849
public const VALUE_WITH_SAMPLED_TRACE = 'with_sampled_trace';
@@ -148,16 +149,19 @@ interface KnownValues
148149
*/
149150
public const OTEL_TRACES_EXPORTER = [
150151
self::VALUE_OTLP,
152+
self::VALUE_OTLP_STDOUT,
151153
self::VALUE_ZIPKIN,
152154
self::VALUE_NONE,
153155
];
154156
public const OTEL_METRICS_EXPORTER = [
155157
self::VALUE_OTLP,
158+
self::VALUE_OTLP_STDOUT,
156159
self::VALUE_PROMETHEUS,
157160
self::VALUE_NONE,
158161
];
159162
public const OTEL_LOGS_EXPORTER = [
160163
self::VALUE_OTLP,
164+
self::VALUE_OTLP_STDOUT,
161165
self::VALUE_NONE,
162166
];
163167
/**

0 commit comments

Comments
 (0)