-
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.
implement scope configuration (#1353)
- Loading branch information
Showing
67 changed files
with
1,267 additions
and
123 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
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
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
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\InstrumentationScope; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface Config | ||
{ | ||
public function setDisabled(bool $disabled): void; | ||
public function isEnabled(): bool; | ||
} |
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,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; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\InstrumentationScope; | ||
|
||
interface Configurable | ||
{ | ||
public function updateConfigurator(Configurator $configurator): void; | ||
} |
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,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
33
src/SDK/Common/InstrumentationScope/ConfiguratorClosure.php
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,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()); | ||
} | ||
} |
Oops, something went wrong.