-
Notifications
You must be signed in to change notification settings - Fork 214
auto-instrumentation registration #1304
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
Merged
bobstrecansky
merged 53 commits into
open-telemetry:main
from
brettmc:auto-instrumentation-registration
Jul 22, 2024
Merged
Changes from 36 commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
be76d19
[WIP] Add instrumentation configuration
Nevay c178431
add autoloading for auto-instrumentations using SPI
brettmc 31f93c0
allow autoloading and non-autoloading to work
brettmc ef343e1
fix attribute
brettmc ba765d9
experimental config file
brettmc b771c57
fixing invalid dependencies
brettmc ba994d5
dont register hook manager globally or in sdk
brettmc 35feeff
remove unused function, psalm ignore missing extension function
brettmc 43fc772
possibly fixing type-hint
brettmc 76ce98f
load config files relative to cwd
brettmc 73f83e0
fixing hook manager enable/disable + psalm complaints
brettmc 7e57997
fixing 8.1 psalm error
brettmc f29b713
use context to pass providers to instrumentations
brettmc 20cd7ef
adding tests for sdk::registerGlobal
brettmc c8373a3
linting
brettmc 9ba45be
test coverage for globals
brettmc a4d9046
add opentelemetry extension to developer image and actions
brettmc e704719
always register instrumentations via SPI
brettmc d94d4cc
register globals initializer for file-config sdk
brettmc 04aa26a
linting
brettmc 7262680
remove globals init function
brettmc 579713a
fix phan warning
brettmc 8fd3724
simplify hook manager
brettmc e6f85fe
add todo to deprecate Registry in future
brettmc d032942
autoload instrumentations without config
brettmc 82d62db
fixing phan ref, update doc
brettmc 0e5ffac
remove phan suppress
brettmc de4262b
fix example
brettmc be50116
bump SPI to 0.2.1
brettmc 5798a40
Merge branch 'main' into auto-instrumentation-registration
brettmc 7acacb6
adding late-binding tracer+provider
brettmc 77653d4
adding late binding logger and meter providers
brettmc c0cd4ed
more late binding test coverage
brettmc fe5fe3e
tidy
brettmc 9459cb0
Merge branch 'main' into auto-instrumentation-registration
brettmc 1c0e1b5
dont use CoversMethod yet
brettmc dcbdc37
Merge branch 'main' into auto-instrumentation-registration
brettmc 4b6deb8
kitchen sink, remove unused var
brettmc 610832b
instrumentation config as a map, per config file spec
brettmc a85982f
adding general config
brettmc ccde00d
move general config into sdk
brettmc 552a7e8
test config caching
brettmc 1164e6b
move general instrumentation config to api
brettmc 3695630
avoid bad version of sebastian/exporter
brettmc 7c0131b
bump config yaml files to 0.3
brettmc 90a69ce
fix tests
brettmc d1b1f6f
Merge branch 'main' into auto-instrumentation-registration
brettmc 3c2ac3f
disable hook manager during file-based init
brettmc f841065
cleanup
brettmc 898982b
support multiple config files in autoloader
brettmc 668d6e8
Merge branch 'main' into auto-instrumentation-registration
brettmc 51234bd
move hook manager enable/disable out of extension hook manager
brettmc 933dce7
update spi config
brettmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace _; | ||
|
|
||
| use Nevay\SPI\ServiceLoader; | ||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ExtensionHookManager; | ||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; | ||
| use OpenTelemetry\API\Logs\NoopLoggerProvider; | ||
| use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider; | ||
| use OpenTelemetry\API\Trace\NoopTracerProvider; | ||
| use OpenTelemetry\Config\SDK\Configuration; | ||
| use OpenTelemetry\Config\SDK\Configuration\Context; | ||
| use OpenTelemetry\Example\Example; | ||
| use const PHP_EOL; | ||
|
|
||
| /** | ||
| * This example uses SPI (see root composer.json extra.spi) to configure an example auto-instrumentation from a YAML file | ||
| */ | ||
| // EXAMPLE_INSTRUMENTATION_SPAN_NAME=test1234 php examples/instrumentation/configure_instrumentation.php | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| Configuration::parseFile(__DIR__ . '/otel-sdk.yaml')->create(new Context())->setAutoShutdown(true)->buildAndRegisterGlobal(); | ||
| $configuration = \OpenTelemetry\Config\SDK\Instrumentation::parseFile(__DIR__ . '/otel-instrumentation.yaml')->create(); | ||
| $hookManager = new ExtensionHookManager(); | ||
| $context = new \OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context(new NoopTracerProvider(), new NoopMeterProvider(), new NoopLoggerProvider()); | ||
|
|
||
| foreach (ServiceLoader::load(Instrumentation::class) as $instrumentation) { | ||
| $instrumentation->register($hookManager, $configuration, $context); | ||
| } | ||
|
|
||
| echo (new Example())->test(), PHP_EOL; |
21 changes: 21 additions & 0 deletions
21
examples/instrumentation/configure_instrumentation_global.php
This file contains hidden or 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,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace _; | ||
|
|
||
| use OpenTelemetry\Example\Example; | ||
| use const PHP_EOL; | ||
|
|
||
| /** | ||
| * This example uses SPI (see root composer.json extra.spi) to configure an example auto-instrumentation from a YAML file. | ||
| * The YAML file paths are relative to the current working directory. | ||
| */ | ||
| // EXAMPLE_INSTRUMENTATION_SPAN_NAME=test1234 php examples/instrumentation/configure_instrumentation_global.php | ||
| putenv('OTEL_PHP_AUTOLOAD_ENABLED=true'); | ||
| putenv('OTEL_EXPERIMENTAL_CONFIG_FILE=examples/instrumentation/otel-sdk.yaml'); | ||
| putenv('OTEL_PHP_INSTRUMENTATION_CONFIG_FILE=examples/instrumentation/otel-instrumentation.yaml'); | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| echo (new Example())->test(), PHP_EOL; | ||
This file contains hidden or 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,3 @@ | ||
| config: | ||
| - example_instrumentation: | ||
| span_name: ${EXAMPLE_INSTRUMENTATION_SPAN_NAME:-example span} |
This file contains hidden or 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,7 @@ | ||
| file_format: '0.1' | ||
|
|
||
| tracer_provider: | ||
| processors: | ||
| - simple: | ||
| exporter: | ||
| console: {} |
This file contains hidden or 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\Example; | ||
|
|
||
| final class Example | ||
| { | ||
|
|
||
| public function test(): int | ||
| { | ||
| return 42; | ||
| } | ||
| } |
This file contains hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Example; | ||
|
|
||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration; | ||
|
|
||
| final class ExampleConfig implements InstrumentationConfiguration | ||
| { | ||
|
|
||
| public function __construct( | ||
| public readonly string $spanName, | ||
| public readonly bool $enabled = true, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or 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,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Example; | ||
|
|
||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration; | ||
| use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; | ||
| use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry; | ||
| use OpenTelemetry\Config\SDK\Configuration\Context; | ||
| use OpenTelemetry\Config\SDK\Configuration\Validation; | ||
| use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
|
|
||
| /** | ||
| * @implements ComponentProvider<InstrumentationConfiguration> | ||
| */ | ||
| final class ExampleConfigProvider implements ComponentProvider | ||
| { | ||
|
|
||
| /** | ||
| * @psalm-suppress MoreSpecificImplementedParamType | ||
| * @param array{ | ||
| * span_name: string, | ||
| * enabled: bool, | ||
| * } $properties | ||
| */ | ||
| public function createPlugin(array $properties, Context $context): InstrumentationConfiguration | ||
| { | ||
| return new ExampleConfig( | ||
| spanName: $properties['span_name'], | ||
| enabled: $properties['enabled'], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @psalm-suppress UndefinedInterfaceMethod,PossiblyNullReference | ||
| */ | ||
| public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition | ||
| { | ||
| $root = new ArrayNodeDefinition('example_instrumentation'); | ||
| $root | ||
| ->children() | ||
| ->scalarNode('span_name')->isRequired()->validate()->always(Validation::ensureString())->end()->end() | ||
| ->end() | ||
| ->canBeDisabled() | ||
| ; | ||
|
|
||
| return $root; | ||
| } | ||
| } |
This file contains hidden or 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,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Example; | ||
|
|
||
| use Exception; | ||
| use OpenTelemetry\API\Globals; | ||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry; | ||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; | ||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; | ||
| use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; | ||
| use OpenTelemetry\API\Trace\Span; | ||
| use OpenTelemetry\Context\Context; | ||
|
|
||
| final class ExampleInstrumentation implements Instrumentation | ||
| { | ||
|
|
||
| /** | ||
| * @todo can we pass in just the config for _this_ instrumentation, rather than all? | ||
| */ | ||
| public function register(HookManager $hookManager, ConfigurationRegistry $configuration, InstrumentationContext $context): void | ||
| { | ||
| $config = $configuration->get(ExampleConfig::class) ?? throw new Exception('example instrumentation must be configured'); | ||
| if (!$config->enabled) { | ||
| return; | ||
| } | ||
|
|
||
| $tracer = Globals::tracerProvider()->getTracer('example-instrumentation'); | ||
brettmc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $hookManager->hook( | ||
| Example::class, | ||
| 'test', | ||
| static function () use ($tracer, $config): void { | ||
| $context = Context::getCurrent(); | ||
|
|
||
| $span = $tracer | ||
| ->spanBuilder($config->spanName) | ||
| ->setParent($context) | ||
| ->startSpan(); | ||
|
|
||
| Context::storage()->attach($span->storeInContext($context)); | ||
| }, | ||
| static function (): void { | ||
| if (!$scope = Context::storage()->scope()) { | ||
| return; | ||
| } | ||
|
|
||
| $scope->detach(); | ||
|
|
||
| $span = Span::fromContext($scope->context()); | ||
| $span->end(); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.