Skip to content

Commit

Permalink
Allow runtime changes to tracer/meter/logger config disabled flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay committed Jul 20, 2024
1 parent 1250797 commit 08bc772
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 48 deletions.
8 changes: 7 additions & 1 deletion logs/Internal/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Nevay\OTelSDK\Common\ContextResolver;
use Nevay\OTelSDK\Common\InstrumentationScope;
use Nevay\OTelSDK\Logs\LoggerConfig;
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Trace\Span;
Expand All @@ -15,13 +16,18 @@ final class Logger implements LoggerInterface {
public function __construct(
private readonly LoggerState $loggerState,
private readonly InstrumentationScope $instrumentationScope,
private readonly LoggerConfig $loggerConfig,
) {}

public function enabled(): bool {
return true;
return !$this->loggerConfig->disabled;
}

public function emit(LogRecord $logRecord): void {
if ($this->loggerConfig->disabled) {
return;
}

$context = ContextResolver::resolve(Accessor::getContext($logRecord), $this->loggerState->contextStorage);

$record = new ReadWriteLogRecord(
Expand Down
9 changes: 2 additions & 7 deletions logs/Internal/LoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use OpenTelemetry\API\Logs\LoggerInterface;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Logs\NoopEventLogger;
use OpenTelemetry\API\Logs\NoopLogger;
use OpenTelemetry\Context\ContextStorageInterface;
use Psr\Log\LoggerInterface as PsrLoggerInterface;

Expand Down Expand Up @@ -65,13 +64,9 @@ public function getLogger(

$instrumentationScope = new InstrumentationScope($name, $version, $schemaUrl,
$this->instrumentationScopeAttributesFactory->builder()->addAll($attributes)->build());
$loggerConfig = ($this->loggerConfigurator)($instrumentationScope);

$config = ($this->loggerConfigurator)($instrumentationScope);
if ($config->disabled) {
return new NoopLogger();
}

return new Logger($this->loggerState, $instrumentationScope);
return new Logger($this->loggerState, $instrumentationScope, $loggerConfig);
}

public function getEventLogger(
Expand Down
2 changes: 1 addition & 1 deletion logs/LoggerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
final class LoggerConfig {

public function __construct(
public readonly bool $disabled = false,
public bool $disabled = false,
) {}
}
4 changes: 3 additions & 1 deletion metrics/Internal/Instrument/AsynchronousInstrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Nevay\OTelSDK\Metrics\Instrument;
use Nevay\OTelSDK\Metrics\Internal\Registry\MetricWriter;
use Nevay\OTelSDK\Metrics\Internal\StalenessHandler\ReferenceCounter;
use Nevay\OTelSDK\Metrics\MeterConfig;
use OpenTelemetry\API\Metrics\ObservableCallbackInterface;
use OpenTelemetry\API\Metrics\ObserverInterface;
use WeakMap;
Expand All @@ -22,6 +23,7 @@ public function __construct(
private readonly Instrument $instrument,
private readonly ReferenceCounter $referenceCounter,
private readonly WeakMap $destructors,
private readonly MeterConfig $meterConfig,
) {
assert($this instanceof InstrumentHandle);

Expand All @@ -37,7 +39,7 @@ public function getHandle(): Instrument {
}

public function enabled(): bool {
return $this->writer->enabled($this->instrument);
return !$this->meterConfig->disabled && $this->writer->enabled($this->instrument);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion metrics/Internal/Instrument/SynchronousInstrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Nevay\OTelSDK\Metrics\Instrument;
use Nevay\OTelSDK\Metrics\Internal\Registry\MetricWriter;
use Nevay\OTelSDK\Metrics\Internal\StalenessHandler\ReferenceCounter;
use Nevay\OTelSDK\Metrics\MeterConfig;
use OpenTelemetry\Context\ContextInterface;
use function assert;

Expand All @@ -16,6 +17,7 @@ public function __construct(
private readonly MetricWriter $writer,
private readonly Instrument $instrument,
private readonly ReferenceCounter $referenceCounter,
private readonly MeterConfig $meterConfig,
) {
assert($this instanceof InstrumentHandle);

Expand All @@ -31,7 +33,7 @@ public function getHandle(): Instrument {
}

public function enabled(): bool {
return $this->writer->enabled($this->instrument);
return !$this->meterConfig->disabled && $this->writer->enabled($this->instrument);
}

/**
Expand All @@ -41,6 +43,10 @@ public function enabled(): bool {
* @noinspection PhpMissingParamTypeInspection
*/
public function write($amount, iterable $attributes = [], $context = null): void {
if ($this->meterConfig->disabled) {
return;
}

$this->writer->record($this->instrument, $amount, $attributes, $context);
}
}
10 changes: 6 additions & 4 deletions metrics/Internal/Meter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nevay\OTelSDK\Metrics\Internal\Instrument\ObservableUpDownCounter;
use Nevay\OTelSDK\Metrics\Internal\Instrument\UpDownCounter;
use Nevay\OTelSDK\Metrics\Internal\StalenessHandler\MultiReferenceCounter;
use Nevay\OTelSDK\Metrics\MeterConfig;
use OpenTelemetry\API\Metrics\AsynchronousInstrument;
use OpenTelemetry\API\Metrics\CounterInterface;
use OpenTelemetry\API\Metrics\GaugeInterface;
Expand All @@ -35,6 +36,7 @@ final class Meter implements MeterInterface {
public function __construct(
private readonly MeterState $meterState,
private readonly InstrumentationScope $instrumentationScope,
private readonly MeterConfig $meterConfig,
) {}

private static function dummyInstrument(): Instrument {
Expand Down Expand Up @@ -121,9 +123,9 @@ public function createObservableUpDownCounter(string $name, ?string $unit = null
*/
private function createSynchronousInstrument(string $class, InstrumentType $type, string $name, ?string $unit, ?string $description, array $advisory): InstrumentHandle {
[$instrument, $referenceCounter] = $this->meterState->createSynchronousInstrument(new Instrument(
$type, $name, $unit, $description, $advisory), $this->instrumentationScope);
$type, $name, $unit, $description, $advisory), $this->instrumentationScope, $this->meterConfig);

return new $class($this->meterState->registry, $instrument, $referenceCounter);
return new $class($this->meterState->registry, $instrument, $referenceCounter, $this->meterConfig);
}

/**
Expand All @@ -138,13 +140,13 @@ private function createAsynchronousInstrument(string $class, InstrumentType $typ
$advisory = [];
}
[$instrument, $referenceCounter] = $this->meterState->createAsynchronousInstrument(new Instrument(
$type, $name, $unit, $description, $advisory), $this->instrumentationScope);
$type, $name, $unit, $description, $advisory), $this->instrumentationScope, $this->meterConfig);

foreach ($callbacks as $callback) {
$this->meterState->registry->registerCallback(closure($callback), $instrument);
$referenceCounter->acquire(true);
}

return new $class($this->meterState->registry, $instrument, $referenceCounter, $this->meterState->destructors);
return new $class($this->meterState->registry, $instrument, $referenceCounter, $this->meterState->destructors, $this->meterConfig);
}
}
33 changes: 21 additions & 12 deletions metrics/Internal/MeterMetricProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nevay\OTelSDK\Metrics\MetricFilter;
use Nevay\OTelSDK\Metrics\MetricFilterResult;
use Nevay\OTelSDK\Metrics\MetricProducer;
use Traversable;
use function array_keys;
use function count;
use const COUNT_RECURSIVE;
Expand Down Expand Up @@ -42,16 +43,15 @@ public function registerMetricSource(int $streamId, MetricStreamSource $streamSo
}

public function produce(?MetricFilter $metricFilter = null, ?Cancellation $cancellation = null): iterable {
$sources = $metricFilter
? $this->applyMetricFilter($this->sources, $metricFilter)
: $this->sources;
$sources = $this->applyMetricFilter($this->sources, $metricFilter);
$streamIds = count($sources) === count($this->sources)
? $this->streamIds ??= array_keys($this->sources)
: array_keys($sources);
$collector = $this->collector;

return new SizedTraversable(
(function() use ($sources, $streamIds, $cancellation) {
$this->collector->collectAndPush($streamIds, $cancellation);
(static function() use ($collector, $sources, $streamIds, $cancellation): Traversable {
$collector->collectAndPush($streamIds, $cancellation);
unset($streamIds, $cancellation);

foreach ($sources as $streamSources) {
Expand All @@ -71,15 +71,10 @@ public function produce(?MetricFilter $metricFilter = null, ?Cancellation $cance
* @param array<int, list<MetricStreamSource>> $sources
* @return array<int, array<int, MetricStreamSource>>
*/
private function applyMetricFilter(array $sources, MetricFilter $filter): array {
private function applyMetricFilter(array $sources, ?MetricFilter $filter): array {
foreach ($sources as $streamId => $streamSources) {
foreach ($streamSources as $sourceId => $source) {
$result = $filter->testMetric(
$source->descriptor->instrumentationScope,
$source->descriptor->name,
$source->descriptor->instrumentType,
$source->descriptor->unit,
);
$result = self::testMetric($source, $filter);

if ($result === MetricFilterResult::Accept) {
// no-op
Expand All @@ -89,6 +84,7 @@ private function applyMetricFilter(array $sources, MetricFilter $filter): array
$source->descriptor,
new FilteredMetricStream($source->descriptor, $source->stream, $filter),
$source->reader,
$source->meterConfig,
);
}
if ($result === MetricFilterResult::Drop) {
Expand All @@ -103,4 +99,17 @@ private function applyMetricFilter(array $sources, MetricFilter $filter): array

return $sources;
}

private static function testMetric(MetricStreamSource $source, ?MetricFilter $filter): MetricFilterResult {
if ($source->meterConfig->disabled) {
return MetricFilterResult::Drop;
}

return $filter?->testMetric(
$source->descriptor->instrumentationScope,
$source->descriptor->name,
$source->descriptor->instrumentType,
$source->descriptor->unit,
) ?? MetricFilterResult::Accept;
}
}
9 changes: 2 additions & 7 deletions metrics/Internal/MeterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Nevay\OTelSDK\Metrics\MetricReader;
use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\Context\ContextStorageInterface;
use Psr\Log\LoggerInterface;
use WeakMap;
Expand Down Expand Up @@ -90,13 +89,9 @@ public function getMeter(

$instrumentationScope = new InstrumentationScope($name, $version, $schemaUrl,
$this->instrumentationScopeAttributesFactory->builder()->addAll($attributes)->build());
$meterConfig = ($this->meterConfigurator)($instrumentationScope);

$config = ($this->meterConfigurator)($instrumentationScope);
if ($config->disabled) {
return new NoopMeter();
}

return new Meter($this->meterState, $instrumentationScope);
return new Meter($this->meterState, $instrumentationScope, $meterConfig);
}

public function shutdown(?Cancellation $cancellation = null): bool {
Expand Down
9 changes: 5 additions & 4 deletions metrics/Internal/MeterState.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Nevay\OTelSDK\Metrics\Internal\Stream\SynchronousMetricStream;
use Nevay\OTelSDK\Metrics\Internal\View\ResolvedView;
use Nevay\OTelSDK\Metrics\Internal\View\ViewRegistry;
use Nevay\OTelSDK\Metrics\MeterConfig;
use Nevay\OTelSDK\Metrics\MetricReader;
use Nevay\OTelSDK\Metrics\View;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function getAsynchronousInstrument(Instrument $instrument, Instrumentatio
/**
* @return array{Instrument, ReferenceCounter}
*/
public function createSynchronousInstrument(Instrument $instrument, InstrumentationScope $instrumentationScope): array {
public function createSynchronousInstrument(Instrument $instrument, InstrumentationScope $instrumentationScope, MeterConfig $meterConfig): array {
$instrumentationScopeId = self::instrumentationScopeId($instrumentationScope);
$instrumentId = self::instrumentId($instrument);

Expand Down Expand Up @@ -125,7 +126,7 @@ public function createSynchronousInstrument(Instrument $instrument, Instrumentat
$dedup[$dedupId] = $streamId;
}
$stream = $streams[$streamId];
$source = new MetricStreamSource($view->descriptor, $stream, $stream->register($view->temporality));
$source = new MetricStreamSource($view->descriptor, $stream, $stream->register($view->temporality), $meterConfig);
$view->metricProducer->registerMetricSource($streamId, $source);
}

Expand All @@ -148,7 +149,7 @@ public function createSynchronousInstrument(Instrument $instrument, Instrumentat
/**
* @return array{Instrument, ReferenceCounter}
*/
public function createAsynchronousInstrument(Instrument $instrument, InstrumentationScope $instrumentationScope): array {
public function createAsynchronousInstrument(Instrument $instrument, InstrumentationScope $instrumentationScope, MeterConfig $meterConfig): array {
$instrumentationScopeId = self::instrumentationScopeId($instrumentationScope);
$instrumentId = self::instrumentId($instrument);

Expand Down Expand Up @@ -182,7 +183,7 @@ public function createAsynchronousInstrument(Instrument $instrument, Instrumenta
$dedup[$dedupId] = $streamId;
}
$stream = $streams[$streamId];
$source = new MetricStreamSource($view->descriptor, $stream, $stream->register($view->temporality));
$source = new MetricStreamSource($view->descriptor, $stream, $stream->register($view->temporality), $meterConfig);
$view->metricProducer->registerMetricSource($streamId, $source);
}

Expand Down
2 changes: 2 additions & 0 deletions metrics/Internal/MetricStreamSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Nevay\OTelSDK\Metrics\Data\Descriptor;
use Nevay\OTelSDK\Metrics\Internal\Stream\MetricStream;
use Nevay\OTelSDK\Metrics\MeterConfig;

/**
* @internal
Expand All @@ -13,5 +14,6 @@ public function __construct(
public readonly Descriptor $descriptor,
public readonly MetricStream $stream,
public readonly int $reader,
public readonly MeterConfig $meterConfig,
) {}
}
2 changes: 1 addition & 1 deletion metrics/MeterConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
final class MeterConfig {

public function __construct(
public readonly bool $disabled = false,
public bool $disabled = false,
) {}
}
9 changes: 8 additions & 1 deletion trace/Internal/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Nevay\OTelSDK\Trace\Internal;

use Nevay\OTelSDK\Common\InstrumentationScope;
use Nevay\OTelSDK\Trace\TracerConfig;
use OpenTelemetry\API\Trace\NoopSpanBuilder;
use OpenTelemetry\API\Trace\SpanBuilderInterface;
use OpenTelemetry\API\Trace\TracerInterface;

Expand All @@ -13,13 +15,18 @@ final class Tracer implements TracerInterface {
public function __construct(
private readonly TracerState $tracerState,
private readonly InstrumentationScope $instrumentationScope,
private readonly TracerConfig $tracerConfig,
) {}

public function enabled(): bool {
return true;
return !$this->tracerConfig->disabled;
}

public function spanBuilder(string $spanName): SpanBuilderInterface {
if ($this->tracerConfig->disabled) {
return new NoopSpanBuilder($this->tracerState->contextStorage);
}

return new SpanBuilder($this->tracerState, $this->instrumentationScope, $spanName);
}
}
9 changes: 2 additions & 7 deletions trace/Internal/TracerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Nevay\OTelSDK\Trace\Sampler;
use Nevay\OTelSDK\Trace\SpanProcessor;
use Nevay\OTelSDK\Trace\TracerConfig;
use OpenTelemetry\API\Trace\NoopTracer;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Context\ContextStorageInterface;
Expand Down Expand Up @@ -79,13 +78,9 @@ public function getTracer(

$instrumentationScope = new InstrumentationScope($name, $version, $schemaUrl,
$this->instrumentationScopeAttributesFactory->builder()->addAll($attributes)->build());
$tracerConfig = ($this->tracerConfigurator)($instrumentationScope);

$config = ($this->tracerConfigurator)($instrumentationScope);
if ($config->disabled) {
return new NoopTracer();
}

return new Tracer($this->tracerState, $instrumentationScope);
return new Tracer($this->tracerState, $instrumentationScope, $tracerConfig);
}

public function shutdown(?Cancellation $cancellation = null): bool {
Expand Down
2 changes: 1 addition & 1 deletion trace/TracerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
final class TracerConfig {

public function __construct(
public readonly bool $disabled = false,
public bool $disabled = false,
) {}
}

0 comments on commit 08bc772

Please sign in to comment.