-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sdk autoloading * use instrumentation initializers * basic sdk + autoloading documentation * exampler filter from config * configuration default types
- Loading branch information
Showing
28 changed files
with
625 additions
and
22 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
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
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true'); | ||
putenv('OTEL_TRACES_EXPORTER=otlp'); | ||
putenv('OTEL_EXPORTER_OTLP_PROTOCOL=grpc'); | ||
putenv('OTEL_METRICS_EXPORTER=otlp'); | ||
putenv('OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=grpc'); | ||
putenv('OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4317'); | ||
putenv('OTEL_PHP_TRACES_PROCESSOR=batch'); | ||
|
||
// 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\Common\Instrumentation\CachedInstrumentation('demo'); | ||
|
||
$instrumentation->tracer()->spanBuilder('root')->startSpan()->end(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Otlp; | ||
|
||
interface ContentTypes | ||
{ | ||
public const PROTOBUF = 'application/x-protobuf'; | ||
public const JSON = 'application/json'; | ||
public const NDJSON = 'application/x-ndjson'; | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Otlp; | ||
|
||
use OpenTelemetry\API\Common\Signal\Signals; | ||
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory; | ||
use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
use OpenTelemetry\SDK\Common\Configuration\KnownValues; | ||
use OpenTelemetry\SDK\Common\Configuration\Variables; | ||
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory; | ||
use OpenTelemetry\SDK\Common\Export\TransportInterface; | ||
use OpenTelemetry\SDK\Metrics\MetricExporterInterface; | ||
use UnexpectedValueException; | ||
|
||
class MetricExporterFactory | ||
{ | ||
/** | ||
* @psalm-suppress ArgumentTypeCoercion | ||
*/ | ||
public function create(): MetricExporterInterface | ||
{ | ||
$protocol = Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_PROTOCOL) | ||
? Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_METRICS_PROTOCOL) | ||
: Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_PROTOCOL); | ||
|
||
return new MetricExporter($this->buildTransport($protocol)); | ||
} | ||
|
||
private function buildTransport(string $protocol): TransportInterface | ||
{ | ||
/** | ||
* @todo (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#periodic-exporting-metricreader) | ||
* - OTEL_METRIC_EXPORT_INTERVAL | ||
* - OTEL_METRIC_EXPORT_TIMEOUT | ||
*/ | ||
$endpoint = Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_ENDPOINT) | ||
? Configuration::getString(Variables::OTEL_EXPORTER_OTLP_METRICS_ENDPOINT) | ||
: Configuration::getString(Variables::OTEL_EXPORTER_OTLP_ENDPOINT); | ||
|
||
$headers = Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS) | ||
? Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS) | ||
: Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS); | ||
$headers += OtlpUtil::getUserAgentHeader(); | ||
|
||
switch ($protocol) { | ||
case KnownValues::VALUE_GRPC: | ||
return (new GrpcTransportFactory())->create( | ||
$endpoint . OtlpUtil::method(Signals::METRICS), | ||
ContentTypes::PROTOBUF, | ||
$headers, | ||
); | ||
case KnownValues::VALUE_HTTP_PROTOBUF: | ||
case KnownValues::VALUE_HTTP_JSON: | ||
return PsrTransportFactory::discover()->create( | ||
$endpoint, | ||
Protocols::contentType($protocol), | ||
$headers, | ||
); | ||
default: | ||
throw new UnexpectedValueException('Unknown otlp protocol: ' . $protocol); | ||
} | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Metrics; | ||
|
||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
use OpenTelemetry\SDK\Common\Configuration\KnownValues; | ||
use OpenTelemetry\SDK\Common\Configuration\Variables; | ||
use OpenTelemetry\SDK\Common\Time\ClockFactory; | ||
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\AllExemplarFilter; | ||
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\NoneExemplarFilter; | ||
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter; | ||
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface; | ||
use OpenTelemetry\SDK\Metrics\MetricExporter\NoopMetricExporter; | ||
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader; | ||
use OpenTelemetry\SDK\Resource\ResourceInfoFactory; | ||
use OpenTelemetry\SDK\Sdk; | ||
|
||
class MeterProviderFactory | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
private const KNOWN_EXPORTER_FACTORIES = [ | ||
KnownValues::VALUE_OTLP => '\OpenTelemetry\Contrib\Otlp\MetricExporterFactory', | ||
]; | ||
|
||
public function create(): MeterProviderInterface | ||
{ | ||
if (Sdk::isDisabled()) { | ||
return new NoopMeterProvider(); | ||
} | ||
$exporterName = Configuration::getString(Variables::OTEL_METRICS_EXPORTER); | ||
if ($exporterName === KnownValues::VALUE_NONE) { | ||
$exporter = new NoopMetricExporter(); | ||
} elseif (!array_key_exists($exporterName, self::KNOWN_EXPORTER_FACTORIES)) { | ||
self::logError('Factory cannot create exporter: ' . $exporterName); | ||
$exporter = new NoopMetricExporter(); | ||
} else { | ||
$factoryClass = self::KNOWN_EXPORTER_FACTORIES[$exporterName]; | ||
$factory = new $factoryClass(); | ||
$exporter = $factory->create(); | ||
} | ||
$reader = new ExportingReader($exporter, ClockFactory::getDefault()); | ||
$resource = ResourceInfoFactory::defaultResource(); | ||
$exemplarFilter = $this->createExemplarFilter(Configuration::getEnum(Variables::OTEL_METRICS_EXEMPLAR_FILTER)); | ||
|
||
return MeterProvider::builder() | ||
->setResource($resource) | ||
->addReader($reader) | ||
->setExemplarFilter($exemplarFilter) | ||
->build(); | ||
} | ||
|
||
private function createExemplarFilter(string $name): ExemplarFilterInterface | ||
{ | ||
switch ($name) { | ||
case KnownValues::VALUE_WITH_SAMPLED_TRACE: | ||
return new WithSampledTraceExemplarFilter(); | ||
case KnownValues::VALUE_ALL: | ||
return new AllExemplarFilter(); | ||
case KnownValues::VALUE_NONE: | ||
return new NoneExemplarFilter(); | ||
default: | ||
self::logWarning('Unknown exemplar filter: ' . $name); | ||
|
||
return new NoneExemplarFilter(); | ||
} | ||
} | ||
} |
Oops, something went wrong.