-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters