Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk autoloading #854

Merged
merged 10 commits into from
Nov 13, 2022
Prev Previous commit
Next Next commit
exampler filter from config
  • Loading branch information
brettmc committed Nov 10, 2022
commit b034034bfab306f5899a99eb46509c36dcbaca44
5 changes: 5 additions & 0 deletions src/Contrib/Otlp/MetricExporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function create(): MetricExporterInterface

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);
Expand Down
11 changes: 10 additions & 1 deletion src/SDK/Metrics/MeterProviderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandler\ImmediateStalenessHandlerFactory;
use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry;
use OpenTelemetry\SDK\Resource\ResourceInfo;
Expand All @@ -18,6 +19,7 @@ class MeterProviderBuilder
// @var array<MetricReaderInterface>
private array $metricReaders = [];
private ?ResourceInfo $resource = null;
private ?ExemplarFilterInterface $exemplarFilter = null;

public function registerMetricReader(MetricReaderInterface $reader): self
{
Expand All @@ -33,6 +35,13 @@ public function setResource(ResourceInfo $resource): self
return $this;
}

public function setExemplarFilter(ExemplarFilterInterface $exemplarFilter): self
{
$this->exemplarFilter = $exemplarFilter;

return $this;
}

public function addReader(MetricReaderInterface $reader): self
{
$this->metricReaders[] = $reader;
Expand All @@ -53,7 +62,7 @@ public function build(): MeterProviderInterface
new InstrumentationScopeFactory(Attributes::factory()),
$this->metricReaders,
new CriteriaViewRegistry(),
new WithSampledTraceExemplarFilter(),
$this->exemplarFilter ?? new WithSampledTraceExemplarFilter(),
new ImmediateStalenessHandlerFactory(),
);
}
Expand Down
30 changes: 22 additions & 8 deletions src/SDK/Metrics/MeterProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
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;
Expand All @@ -27,14 +31,6 @@ public function create(): MeterProviderInterface
if (Sdk::isDisabled()) {
return new NoopMeterProvider();
}
/**
* @todo:
* - OTEL_METRICS_EXEMPLAR_FILTER
* - OTEL_METRIC_EXPORT_INTERVAL
* - OTEL_METRIC_EXPORT_TIMEOUT
* - OTEL_EXPORTER_OTLP_METRICS_HEADERS
*/

$exporterName = Configuration::getString(Variables::OTEL_METRICS_EXPORTER, KnownValues::VALUE_NONE);
if ($exporterName === KnownValues::VALUE_NONE) {
$exporter = new NoopMetricExporter();
Expand All @@ -48,10 +44,28 @@ public function create(): MeterProviderInterface
}
$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();
}
}
}