Skip to content

Commit

Permalink
File configuration (#1249)
Browse files Browse the repository at this point in the history
* Add configuration model
* Add metrics and logs configuration model
* Remove accidentally added package dependency
* Fix `$properties` typehint to match config
Default value available -> cannot be null.
* Add configuration example that utilizes env substitution
* Fix/suppress config psalm issues
* Fix/suppress config phpstan issues
* Suppress config phan issues
* Apply cs-fixer
* Change config package version to development version 0.x
* Add README
* Add basic test
* Fix psalm and cs
* Fix composer requirement for PRs
* gitsplit, copy spi config to root composer.json
  • Loading branch information
Nevay authored Apr 1, 2024
1 parent 6dd255d commit 7ec0e90
Show file tree
Hide file tree
Showing 41 changed files with 2,298 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitsplit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ splits:
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/api.git"
- prefix: "src/SDK"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/sdk.git"
- prefix: "src/Config/SDK"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/config-sdk.git"
- prefix: "src/Contrib/Otlp"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/exporter-otlp.git"
- prefix: "src/Contrib/Grpc"
Expand Down
1 change: 1 addition & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@
'exclude_analysis_directory_list' => [
'vendor/',
'proto/',
'src/Config/SDK',
],

// Enable this to enable checks of require/include statements referring to valid paths.
Expand Down
56 changes: 48 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
"psr/http-message": "^1.0.1|^2.0",
"psr/log": "^1.1|^2.0|^3.0",
"symfony/polyfill-mbstring": "^1.23",
"symfony/polyfill-php82": "^1.26"
"symfony/polyfill-php82": "^1.26",
"tbachert/otel-sdk-configuration": "^0.1",
"tbachert/spi": "^0.2"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"php-http/discovery": true,
"symfony/runtime": true
"symfony/runtime": true,
"tbachert/spi": true
}
},
"authors": [
Expand All @@ -33,12 +36,17 @@
}
],
"replace": {
"open-telemetry/api": "self.version",
"open-telemetry/context": "self.version",
"open-telemetry/gen-otlp-protobuf": "self.version",
"open-telemetry/sdk": "self.version",
"open-telemetry/sdk-contrib": "self.version",
"open-telemetry/sem-conv": "self.version"
"open-telemetry/api": "1.0.x-dev",
"open-telemetry/context": "1.0.x-dev",
"open-telemetry/exporter-otlp": "1.0.x-dev",
"open-telemetry/exporter-zipkin": "1.0.x-dev",
"open-telemetry/extension-propagator-b3": "1.0.x-dev",
"open-telemetry/extension-propagator-jaeger": "0.0.2",
"open-telemetry/gen-otlp-protobuf": "1.0.x-dev",
"open-telemetry/sdk": "1.0.x-dev",
"open-telemetry/sdk-configuration": "0.1.x-dev",
"open-telemetry/sdk-contrib": "1.0.x-dev",
"open-telemetry/sem-conv": "1.0.x-dev"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -108,5 +116,37 @@
"ext-gmp": "To support unlimited number of synchronous metric readers",
"ext-grpc": "To use the OTLP GRPC Exporter",
"ext-protobuf": "For more performant protobuf/grpc exporting"
},
"extra": {
"spi": {
"Nevay\\OTelSDK\\Configuration\\ComponentProvider": [
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorB3",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorB3Multi",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorBaggage",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorComposite",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorJaeger",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorTraceContext",

"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SamplerAlwaysOff",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SamplerAlwaysOn",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SamplerParentBased",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SamplerTraceIdRatioBased",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SpanExporterConsole",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SpanExporterOtlp",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SpanExporterZipkin",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SpanProcessorBatch",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Trace\\SpanProcessorSimple",

"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Metrics\\AggregationResolverDefault",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Metrics\\MetricExporterConsole",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Metrics\\MetricExporterOtlp",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Metrics\\MetricReaderPeriodic",

"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordExporterConsole",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordExporterOtlp",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordProcessorBatch",
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordProcessorSimple"
]
}
}
}
29 changes: 29 additions & 0 deletions examples/load_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\Config\SDK\Configuration;

require __DIR__ . '/../vendor/autoload.php';

echo 'load config SDK example starting...' . PHP_EOL;

$config = Configuration::parseFile(__DIR__ . '/load_config.yaml');
$sdk = $config
->create()
->setAutoShutdown(true)
->build();

$tracer = $sdk->getTracerProvider()->getTracer('demo');
$meter = $sdk->getMeterProvider()->getMeter('demo');
$logger = $sdk->getLoggerProvider()->getLogger('demo');

$tracer->spanBuilder('root')->startSpan()->end();
$meter->createCounter('cnt')->add(1);

$eventLogger = new EventLogger($logger, 'my-domain');
$eventLogger->logEvent('foo', new LogRecord('hello, otel'));

echo 'Finished!' . PHP_EOL;
44 changes: 44 additions & 0 deletions examples/load_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
file_format: '0.1'

resource:
attributes:
service.name: opentelemetry-demo

propagators:
composite: [ tracecontext, baggage ]

exporters:
otlp: &otlp-exporter
protocol: http/protobuf
endpoint: http://collector:4318

tracer_provider:
sampler:
parent_based:
root:
always_on: {}
processors:
- simple:
exporter:
console: {}
- batch:
exporter:
otlp: *otlp-exporter
meter_provider:
readers:
- periodic:
exporter:
console: {}
- periodic:
exporter:
otlp:
<<: *otlp-exporter
temporality_preference: lowmemory
logger_provider:
processors:
- simple:
exporter:
console: {}
- batch:
exporter:
otlp: *otlp-exporter
34 changes: 34 additions & 0 deletions examples/load_config_env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\Config\SDK\Configuration;

require __DIR__ . '/../vendor/autoload.php';

echo 'load config SDK example starting...' . PHP_EOL;

$_SERVER['OTEL_SERVICE_NAME'] = 'opentelemetry-demo';
$_SERVER['OTEL_EXPORTER_OTLP_PROTOCOL'] = 'http/protobuf';
$_SERVER['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'http://collector:4318';
$_SERVER['OTEL_TRACES_SAMPLER_ARG'] = '0.5';

$config = Configuration::parseFile(__DIR__ . '/load_config_env.yaml');
$sdk = $config
->create()
->setAutoShutdown(true)
->build();

$tracer = $sdk->getTracerProvider()->getTracer('demo');
$meter = $sdk->getMeterProvider()->getMeter('demo');
$logger = $sdk->getLoggerProvider()->getLogger('demo');

$tracer->spanBuilder('root')->startSpan()->end();
$meter->createCounter('cnt')->add(1);

$eventLogger = new EventLogger($logger, 'my-domain');
$eventLogger->logEvent('foo', new LogRecord('hello, otel'));

echo 'Finished!' . PHP_EOL;
49 changes: 49 additions & 0 deletions examples/load_config_env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
file_format: '0.1'

disabled: ${OTEL_SDK_DISABLED}

resource:
attributes:
service.name: ${OTEL_SERVICE_NAME}

propagators:
composite: [ tracecontext, baggage ]

attribute_limits:
attribute_value_length_limit: ${OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT}
attribute_count_limit: ${OTEL_ATTRIBUTE_COUNT_LIMIT}

exporters:
otlp: &otlp-exporter
protocol: ${OTEL_EXPORTER_OTLP_PROTOCOL}
endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT}
certificate: ${OTEL_EXPORTER_OTLP_CERTIFICATE}
client_key: ${OTEL_EXPORTER_OTLP_CLIENT_KEY}
client_certificate: ${OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE}
compression: ${OTEL_EXPORTER_OTLP_COMPRESSION}
timeout: ${OTEL_EXPORTER_OTLP_TIMEOUT}

tracer_provider:
sampler:
parent_based:
root:
trace_id_ratio_based:
ratio: ${OTEL_TRACES_SAMPLER_ARG}
limits:
attribute_value_length_limit: ${OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT}
attribute_count_limit: ${OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT}
event_count_limit: ${OTEL_SPAN_EVENT_COUNT_LIMIT}
link_count_limit: ${OTEL_SPAN_LINK_COUNT_LIMIT}
event_attribute_count_limit: ${OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT}
link_attribute_count_limit: ${OTEL_LINK_ATTRIBUTE_COUNT_LIMIT}
processors:
- simple:
exporter:
console: {}
- batch:
schedule_delay: ${OTEL_BSP_SCHEDULE_DELAY}
export_timeout: ${OTEL_BSP_EXPORT_TIMEOUT}
max_queue_size: ${OTEL_BSP_MAX_QUEUE_SIZE}
max_export_batch_size: ${OTEL_BSP_MAX_EXPORT_BATCH_SIZE}
exporter:
otlp: *otlp-exporter
6 changes: 5 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ parameters:
-
message: "#Call to an undefined method .*:expects.*#"
paths:
- tests
- tests
-
message: "#Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface::.*#"
paths:
- src/Config/SDK
30 changes: 25 additions & 5 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,33 @@
<issueHandlers>
<UndefinedClass>
<errorLevel type="suppress">
<referencedClass name="GMP" />
<referencedClass name="GMP"/>
</errorLevel>
</UndefinedClass>
<ArgumentTypeCoercion>
</UndefinedClass>
<ArgumentTypeCoercion>
<errorLevel type="suppress">
<directory name="./examples" />
<directory name="./examples"/>
</errorLevel>
</ArgumentTypeCoercion>
</ArgumentTypeCoercion>
<UndefinedInterfaceMethod>
<errorLevel type="suppress">
<directory name="src/Config/SDK/ComponentProvider"/>
</errorLevel>
</UndefinedInterfaceMethod>
<PossiblyNullReference>
<errorLevel type="suppress">
<directory name="src/Config/SDK/ComponentProvider"/>
</errorLevel>
</PossiblyNullReference>
<MoreSpecificImplementedParamType>
<errorLevel type="suppress">
<directory name="src/Config/SDK/ComponentProvider"/>
</errorLevel>
</MoreSpecificImplementedParamType>
<InvalidDocblock>
<errorLevel type="suppress">
<directory name="src/Config/SDK/ComponentProvider"/>
</errorLevel>
</InvalidDocblock>
</issueHandlers>
</psalm>
36 changes: 36 additions & 0 deletions src/Config/SDK/ComponentProvider/Logs/LogRecordExporterConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Config\SDK\ComponentProvider\Logs;

use Nevay\OTelSDK\Configuration\ComponentProvider;
use Nevay\OTelSDK\Configuration\ComponentProviderRegistry;
use Nevay\OTelSDK\Configuration\Context;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporter;
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
use OpenTelemetry\SDK\Registry;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

/**
* @implements ComponentProvider<LogRecordExporterInterface>
*/
final class LogRecordExporterConsole implements ComponentProvider
{

/**
* @param array{} $properties
*/
public function createPlugin(array $properties, Context $context): LogRecordExporterInterface
{
return new ConsoleExporter(Registry::transportFactory('stream')->create(
endpoint: 'php://stdout',
contentType: 'application/json',
));
}

public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition
{
return new ArrayNodeDefinition('console');
}
}
Loading

0 comments on commit 7ec0e90

Please sign in to comment.