Skip to content

Commit 68b1b43

Browse files
brettmcNevay
andauthored
auto-instrumentation registration (#1304)
* [WIP] Add instrumentation configuration * add autoloading for auto-instrumentations using SPI * allow autoloading and non-autoloading to work * fix attribute * experimental config file * fixing invalid dependencies - deptrac was rightly complaining that API (indirectly) depended on SDK through config/sdk. For now, remove usage of Config\SDK\Configuration\Context - update deptrac config to allow some new dependencies * dont register hook manager globally or in sdk * remove unused function, psalm ignore missing extension function * possibly fixing type-hint psalm doesn't complain now, so that should be good * load config files relative to cwd * fixing hook manager enable/disable + psalm complaints * fixing 8.1 psalm error * use context to pass providers to instrumentations - make "register global" a function of Sdk, but keep the sdk builder's interface intact - invent an API instrumentation context, similar to the one in config/sdk, to pass providers to instrumentations - add an initial test of autoloading from a config file * adding tests for sdk::registerGlobal in passing, remove some dead code for handling invalid booleans - config already handles this correctly * linting * test coverage for globals * add opentelemetry extension to developer image and actions * always register instrumentations via SPI * register globals initializer for file-config sdk allow SDK created from config file to coexist with components using globals initializer * linting * remove globals init function * fix phan warning * simplify hook manager - drop storage from hook manager: can't guarantee that something else, eg swoole, won't modify storage - drop context from hook manager: we must lazy-load globals via initializers, because not all instrumentations use SPI (although that may change in future) - default hook manager to enabled * add todo to deprecate Registry in future * autoload instrumentations without config if no config provided, still try to load them. wrap registration in a try/catch/log * fixing phan ref, update doc * remove phan suppress * fix example * bump SPI to 0.2.1 * adding late-binding tracer+provider per review from Nevay, this will allow instrumentations to get things from Globals as late as possible * adding late binding logger and meter providers * more late binding test coverage * tidy * dont use CoversMethod yet not available in phpunit 10, so comment out and leave a todo * kitchen sink, remove unused var * instrumentation config as a map, per config file spec * adding general config * move general config into sdk * test config caching * move general instrumentation config to api * avoid bad version of sebastian/exporter * bump config yaml files to 0.3 * fix tests * disable hook manager during file-based init * cleanup * support multiple config files in autoloader * move hook manager enable/disable out of extension hook manager The most obvious place to do this is in a class named HookManager, which _was_ an interface for hook managers. Rename existing HookManager to HookManagerInterface, then re-purpose HookManager for globally enabling/disabling hook managers as well as determining the disabled status. * update spi config --------- Co-authored-by: Tobias Bachert <[email protected]>
1 parent 4ed0d87 commit 68b1b43

File tree

63 files changed

+1788
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1788
-108
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
experimental: true
2222
composer_args: "--ignore-platform-reqs"
2323
env:
24-
extensions: ast, grpc, protobuf
24+
extensions: ast, grpc, opentelemetry, protobuf
2525

2626
steps:
2727
- name: Set cache key

.phan/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@
373373
'vendor/composer',
374374
'vendor/grpc/grpc/src/lib',
375375
'vendor/guzzlehttp',
376+
'vendor/tbachert/spi/src',
376377
'vendor/psr',
377378
'vendor/php-http',
378379
'vendor/phpunit/phpunit/src',

composer.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"symfony/config": "^5.4 || ^6.4 || ^7.0",
2020
"symfony/polyfill-mbstring": "^1.23",
2121
"symfony/polyfill-php82": "^1.26",
22-
"tbachert/spi": "^0.2"
22+
"tbachert/spi": ">= 0.2.1"
2323
},
2424
"config": {
2525
"sort-packages": true,
@@ -101,6 +101,7 @@
101101
"phpstan/phpstan-mockery": "^1.1",
102102
"phpstan/phpstan-phpunit": "^1.3",
103103
"phpunit/phpunit": "^10 || ^11",
104+
"sebastian/exporter": "<= 6.0.1 || >= 6.1.3",
104105
"symfony/http-client": "^5.2",
105106
"symfony/var-exporter": "^5.4 || ^6.4 || ^7.0",
106107
"symfony/yaml": "^5.4 || ^6.4 || ^7.0"
@@ -148,7 +149,18 @@
148149
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordExporterConsole",
149150
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordExporterOtlp",
150151
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordProcessorBatch",
151-
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordProcessorSimple"
152+
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Logs\\LogRecordProcessorSimple",
153+
154+
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Instrumentation\\General\\HttpConfigProvider",
155+
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Instrumentation\\General\\PeerConfigProvider",
156+
157+
"OpenTelemetry\\Example\\ExampleConfigProvider"
158+
],
159+
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\HookManagerInterface": [
160+
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\ExtensionHookManager"
161+
],
162+
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\Instrumentation": [
163+
"OpenTelemetry\\Example\\ExampleInstrumentation"
152164
]
153165
}
154166
}

deptrac.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ deptrac:
2525
collectors:
2626
- type: directory
2727
value: src/SDK/.*
28+
- name: ConfigSDK
29+
collectors:
30+
- type: directory
31+
value: src/Config/SDK/.*
2832
- name: Context
2933
collectors:
3034
- type: directory
@@ -85,6 +89,18 @@ deptrac:
8589
value: ^GuzzleHttp\\*
8690
- type: className
8791
value: ^Buzz\\*
92+
- name: SPI
93+
collectors:
94+
- type: className
95+
value: ^Nevay\\SPI\\*
96+
- name: SymfonyConfig
97+
collectors:
98+
- type: className
99+
value: ^Symfony\\Component\\Config\\*
100+
- type: className
101+
value: ^Symfony\\Component\\Yaml\\*
102+
- type: className
103+
value: ^Symfony\\Component\\VarExporter\\*
88104
- name: RamseyUuid
89105
collectors:
90106
- type: className
@@ -94,16 +110,29 @@ deptrac:
94110
Context:
95111
- FFI
96112
SemConv: ~
113+
ConfigSDK:
114+
- SymfonyConfig
115+
- API
116+
- SDK
117+
- SPI
118+
- PsrLog
119+
- Composer
120+
- Context
121+
- Contrib
122+
- Extension
97123
API:
98124
- Context
99125
- PsrLog
126+
- SPI
100127
SDK:
101128
- +API
129+
- ConfigSDK
102130
- SemConv
103131
- PsrHttp
104132
- HttpPlug
105133
- Composer
106134
- HttpClients
135+
- SPI
107136
- RamseyUuid
108137
Contrib:
109138
- +SDK

docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ RUN chmod +x /usr/local/bin/install-php-extensions \
1111
grpc \
1212
intl\
1313
opcache \
14+
opentelemetry \
1415
pcntl \
1516
protobuf \
1617
sockets \
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace _;
6+
7+
use Nevay\SPI\ServiceLoader;
8+
use OpenTelemetry\API\Globals;
9+
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ExtensionHookManager;
10+
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation;
11+
use OpenTelemetry\API\Logs\NoopLoggerProvider;
12+
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
13+
use OpenTelemetry\Config\SDK\Configuration;
14+
use OpenTelemetry\Config\SDK\Configuration\Context;
15+
use OpenTelemetry\Example\Example;
16+
use const PHP_EOL;
17+
18+
/**
19+
* This example uses SPI (see root composer.json extra.spi) to configure an example auto-instrumentation from a YAML file
20+
*/
21+
// EXAMPLE_INSTRUMENTATION_SPAN_NAME=test1234 php examples/instrumentation/configure_instrumentation.php
22+
23+
require __DIR__ . '/../../vendor/autoload.php';
24+
25+
Configuration::parseFile(__DIR__ . '/otel-sdk.yaml')->create(new Context())->setAutoShutdown(true)->buildAndRegisterGlobal();
26+
$configuration = \OpenTelemetry\Config\SDK\Instrumentation::parseFile(__DIR__ . '/otel-sdk.yaml')->create();
27+
$hookManager = new ExtensionHookManager();
28+
$context = new \OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context(Globals::tracerProvider(), new NoopMeterProvider(), new NoopLoggerProvider());
29+
30+
foreach (ServiceLoader::load(Instrumentation::class) as $instrumentation) {
31+
$instrumentation->register($hookManager, $configuration, $context);
32+
}
33+
34+
echo (new Example())->test(), PHP_EOL;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace _;
6+
7+
use OpenTelemetry\Example\Example;
8+
use const PHP_EOL;
9+
10+
/**
11+
* This example uses SPI (see root composer.json extra.spi) to configure an example auto-instrumentation from a YAML file.
12+
* The YAML file paths are relative to the current working directory.
13+
*/
14+
// EXAMPLE_INSTRUMENTATION_SPAN_NAME=test1234 php examples/instrumentation/configure_instrumentation_global.php
15+
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
16+
putenv('OTEL_EXPERIMENTAL_CONFIG_FILE=examples/instrumentation/otel-sdk.yaml');
17+
18+
require __DIR__ . '/../../vendor/autoload.php';
19+
20+
echo (new Example())->test(), PHP_EOL;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
file_format: '0.3'
2+
3+
tracer_provider:
4+
processors:
5+
- simple:
6+
exporter:
7+
console: {}
8+
9+
instrumentation:
10+
php:
11+
example_instrumentation:
12+
span_name: ${EXAMPLE_INSTRUMENTATION_SPAN_NAME:-example span}

examples/load_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
file_format: '0.1'
1+
file_format: '0.3'
22

33
resource:
44
attributes:

examples/load_config_env.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
file_format: '0.1'
1+
file_format: '0.3'
22

33
disabled: ${OTEL_SDK_DISABLED}
44

0 commit comments

Comments
 (0)