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

implement scope configuration #1353

Merged
merged 39 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c12f89d
add enabled function
brettmc Jul 18, 2024
7f8324b
tracer configurator
brettmc Jul 18, 2024
b61feed
logger config
brettmc Jul 18, 2024
3111994
meter config
brettmc Jul 18, 2024
6248609
allow tracer reconfiguration after creation
brettmc Jul 19, 2024
1a4fca7
test enabling a tracer mid-trace
brettmc Jul 19, 2024
0d70f91
tracer configurator builder
brettmc Jul 19, 2024
8c6fb99
consolidate configurators and config
brettmc Jul 19, 2024
dbd1108
Merge branch 'main' into scope-config
brettmc Jul 20, 2024
776f87a
metrics reconfig
brettmc Jul 20, 2024
3c8cd17
gauge enabled
brettmc Jul 20, 2024
46c5730
rector
brettmc Jul 20, 2024
3bdae48
default values
brettmc Jul 20, 2024
8447c8d
instrument disabled if meter disabled
brettmc Jul 20, 2024
83f81c6
rename enabled to isEnabled
brettmc Jul 20, 2024
ccf7da5
do not export metrics when meter disabled
brettmc Jul 21, 2024
9b33ef7
add benchmark test
brettmc Jul 21, 2024
705bfc4
update logger config on provider config update
brettmc Jul 21, 2024
0feb995
tests
brettmc Jul 21, 2024
3955dba
rename method, add tests
brettmc Jul 22, 2024
0eb8707
rename method, tests
brettmc Jul 22, 2024
0447da6
add configurable interface to providers
brettmc Jul 22, 2024
471d391
interface
brettmc Jul 22, 2024
f94a733
ScopeConfigurator interface
brettmc Jul 22, 2024
4d21c6d
param
brettmc Jul 22, 2024
25c7249
remove duplicate interface implementation
brettmc Jul 22, 2024
f30df57
nullsafe, rename method
brettmc Jul 22, 2024
9807215
fix weakmap GC
brettmc Jul 23, 2024
48cf8fa
do not export metrics of disabled meters
brettmc Jul 23, 2024
4dcdb01
update benchmark per review feedback
brettmc Jul 23, 2024
65ff1d3
use wildcards instead of regex pattern in name predicate
brettmc Jul 23, 2024
6d018cc
rector + style
brettmc Jul 23, 2024
a233ad7
Merge branch 'main' into scope-config
brettmc Jul 23, 2024
d2c9793
add isEnabled to late binding tracer/logger/meter
brettmc Jul 23, 2024
b5f0c72
disable metrics streams instead of callbacks
brettmc Jul 24, 2024
6fb6b1d
add skipped test for disabling metric streams
brettmc Jul 24, 2024
e7e0104
note a todo on reconfiguring meters
brettmc Jul 25, 2024
1d78401
replace scope configurator implementation
brettmc Jul 25, 2024
b2f89b6
Merge branch 'main' into scope-config
brettmc Aug 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
'PhanAccessClassInternal',
'PhanAccessMethodInternal',
'PhanAccessPropertyInternal',
'PhanTypeMismatchPropertyReal',
'PhanTemplateTypeNotUsedInFunctionReturn',
],

Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ parameters:
message: "#Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface::.*#"
paths:
- src/Config/SDK
-
message: "#Cannot call method .* on null#"
paths:
- tests/Integration/SDK/Trace
-
message: "#Property .*Logger.*config .* does not accept .*Config?#"
paths:
- src/SDK/Logs
-
message: "#.*return with type T is not subtype.*#"
paths:
- src/SDK/Common/InstrumentationScope
2 changes: 1 addition & 1 deletion src/API/Logs/LateBindingLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function emit(LogRecord $logRecord): void
($this->logger ??= ($this->factory)())->emit($logRecord);
}

public function enabled(): bool
public function isEnabled(): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Logs/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public function emit(LogRecord $logRecord): void;
* are about to generate a LogRecord, to avoid performing computationally expensive work.
* @experimental
*/
public function enabled(): bool;
public function isEnabled(): bool;
}
2 changes: 1 addition & 1 deletion src/API/Logs/NoopLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function log($level, $message, array $context = []): void
{
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Metrics/GaugeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @experimental
*/
interface GaugeInterface
interface GaugeInterface extends SynchronousInstrument
{

/**
Expand Down
7 changes: 6 additions & 1 deletion src/API/Metrics/Instrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ interface Instrument
{
/**
* Determine if the instrument is enabled. Instrumentation authors SHOULD call this API each time they record a measurement.
*
* MUST return false if:
* - The MeterConfig of the Meter used to create the instrument has parameter disabled=true
* - All resolved views for the instrument are configured with the Drop Aggregation
* @experimental
* @see https://opentelemetry.io/docs/specs/otel/metrics/sdk/#instrument-enabled
*/
public function enabled(): bool;
public function isEnabled(): bool;
}
5 changes: 5 additions & 0 deletions src/API/Metrics/LateBindingMeter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function createObservableUpDownCounter(string $name, ?string $unit = null
{
return ($this->meter ??= ($this->factory)())->createObservableUpDownCounter($name, $unit, $description, $advisory, $callbacks);
}

public function isEnabled(): bool
{
return true;
}
}
2 changes: 2 additions & 0 deletions src/API/Metrics/MeterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,6 @@ public function createObservableUpDownCounter(
array|callable $advisory = [],
callable ...$callbacks,
): ObservableUpDownCounterInterface;

public function isEnabled(): bool;
}
2 changes: 1 addition & 1 deletion src/API/Metrics/Noop/NoopCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function add($amount, iterable $attributes = [], $context = null): void
// no-op
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/API/Metrics/Noop/NoopGauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function record(float|int $amount, iterable $attributes = [], $context =
{
// no-op
}

public function isEnabled(): bool
{
return false;
}
}
2 changes: 1 addition & 1 deletion src/API/Metrics/Noop/NoopHistogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function record($amount, iterable $attributes = [], $context = null): voi
// no-op
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/API/Metrics/Noop/NoopMeter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function createObservableUpDownCounter(string $name, ?string $unit = null
{
return new NoopObservableUpDownCounter();
}

public function isEnabled(): bool
{
return false;
}
}
2 changes: 1 addition & 1 deletion src/API/Metrics/Noop/NoopObservableCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function observe(callable $callback, bool $weaken = false): ObservableCal
return new NoopObservableCallback();
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Metrics/Noop/NoopObservableGauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function observe(callable $callback, bool $weaken = false): ObservableCal
return new NoopObservableCallback();
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Metrics/Noop/NoopObservableUpDownCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function observe(callable $callback, bool $weaken = false): ObservableCal
return new NoopObservableCallback();
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Metrics/Noop/NoopUpDownCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function add($amount, iterable $attributes = [], $context = null): void
// no-op
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Trace/LateBindingTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function spanBuilder(string $spanName): SpanBuilderInterface
return ($this->tracer ??= ($this->factory)())->spanBuilder($spanName);
}

public function enabled(): bool
public function isEnabled(): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Trace/NoopTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function spanBuilder(string $spanName): SpanBuilderInterface
return new NoopSpanBuilder(Context::storage());
}

public function enabled(): bool
public function isEnabled(): bool
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Trace/TracerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public function spanBuilder(string $spanName): SpanBuilderInterface;
* creating a new span.
* @experimental
*/
public function enabled(): bool;
public function isEnabled(): bool;
}
14 changes: 14 additions & 0 deletions src/SDK/Common/InstrumentationScope/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\InstrumentationScope;

/**
* @internal
*/
interface Config
{
public function setDisabled(bool $disabled): void;
public function isEnabled(): bool;
}
20 changes: 20 additions & 0 deletions src/SDK/Common/InstrumentationScope/ConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\InstrumentationScope;

trait ConfigTrait
{
private bool $disabled = false;

public function setDisabled(bool $disabled): void
{
$this->disabled = $disabled;
}

public function isEnabled(): bool
{
return $this->disabled === false;
}
}
10 changes: 10 additions & 0 deletions src/SDK/Common/InstrumentationScope/Configurable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\InstrumentationScope;

interface Configurable
{
public function updateConfigurator(Configurator $configurator): void;
}
104 changes: 104 additions & 0 deletions src/SDK/Common/InstrumentationScope/Configurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\InstrumentationScope;

use Closure;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
use OpenTelemetry\SDK\Logs\LoggerConfig;
use OpenTelemetry\SDK\Metrics\MeterConfig;
use OpenTelemetry\SDK\Trace\TracerConfig;
use WeakMap;

/**
* @template T
*/
final class Configurator
{
/** @var Closure(InstrumentationScopeInterface): T */
private readonly Closure $factory;
/** @var WeakMap<InstrumentationScopeInterface, T> */
private WeakMap $configs;
/** @var list<ConfiguratorClosure> */
private array $configurators = [];

/**
* @param Closure(InstrumentationScopeInterface): T $factory
* @psalm-suppress PropertyTypeCoercion
*/
public function __construct(Closure $factory)
{
$this->configs = new WeakMap();
$this->factory = $factory;
}

/**
* @param Closure(T, InstrumentationScopeInterface): void $closure
*/
public function with(Closure $closure, ?string $name, ?string $version = null, ?string $schemaUrl = null): self
{
$this->configurators[] = $configurator = new ConfiguratorClosure($closure, self::namePattern($name), $version, $schemaUrl);

foreach ($this->configs as $instrumentationScope => $config) {
if ($configurator->matches($instrumentationScope)) {
($configurator->closure)($config, $instrumentationScope);
}
}

return $this;
}

/**
* @return T
*/
public function resolve(InstrumentationScopeInterface $instrumentationScope): Config
{
if ($config = $this->configs[$instrumentationScope] ?? null) {
return $config;
}

$config = ($this->factory)($instrumentationScope);
foreach ($this->configurators as $configurator) {
if ($configurator->matches($instrumentationScope)) {
($configurator->closure)($config, $instrumentationScope);
}
}

return $this->configs[$instrumentationScope] ??= $config;
}

/**
* Create a default Configurator for a LoggerConfig
* @return Configurator<LoggerConfig>
*/
public static function logger(): self
{
return (new Configurator(static fn () => new LoggerConfig()));
}

/**
* Create a default Configurator for a MeterConfig
* @return Configurator<MeterConfig>
*/
public static function meter(): self
{
return (new Configurator(static fn () => new MeterConfig()));
}

/**
* Create a default Configurator for a TracerConfig
* @return Configurator<TracerConfig>
*/
public static function tracer(): self
{
return (new Configurator(static fn () => new TracerConfig()));
}

private static function namePattern(?string $name): ?string
{
return $name !== null
? sprintf('/^%s$/', strtr(preg_quote($name, '/'), ['\\?' => '.', '\\*' => '.*']))
: null;
}
}
33 changes: 33 additions & 0 deletions src/SDK/Common/InstrumentationScope/ConfiguratorClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\InstrumentationScope;

use Closure;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;

/**
* @internal
*/
final class ConfiguratorClosure
{

public function __construct(
public readonly Closure $closure,
private readonly ?string $name,
private readonly ?string $version,
private readonly ?string $schemaUrl,
) {
}

/**
* @psalm-suppress ArgumentTypeCoercion
*/
public function matches(InstrumentationScopeInterface $instrumentationScope): bool
{
return ($this->name === null || preg_match($this->name, $instrumentationScope->getName()))
&& ($this->version === null || $this->version === $instrumentationScope->getVersion())
&& ($this->schemaUrl === null || $this->schemaUrl === $instrumentationScope->getSchemaUrl());
}
}
Loading
Loading