Skip to content

Commit 27a188b

Browse files
authored
Auto instrumentation for curl (#1420) (open-telemetry#313)
* auto instrumentation for curl (#1420) * Added Curl to php workflow * cleanup and optimisations * Updated gitsplit * Fixed phpstan issues
1 parent 0a41216 commit 27a188b

File tree

16 files changed

+875
-0
lines changed

16 files changed

+875
-0
lines changed

.github/workflows/php.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
'Context/Swoole',
2525
'Instrumentation/CakePHP',
2626
'Instrumentation/CodeIgniter',
27+
'Instrumentation/Curl',
2728
'Instrumentation/ExtAmqp',
2829
'Instrumentation/ExtRdKafka',
2930
'Instrumentation/Guzzle',
@@ -85,6 +86,12 @@ jobs:
8586
php-version: 8.0
8687
- project: 'Instrumentation/IO'
8788
php-version: 8.1
89+
- project: 'Instrumentation/Curl'
90+
php-version: 7.4
91+
- project: 'Instrumentation/Curl'
92+
php-version: 8.0
93+
- project: 'Instrumentation/Curl'
94+
php-version: 8.1
8895
- project: 'Instrumentation/PDO'
8996
php-version: 7.4
9097
- project: 'Instrumentation/PDO'

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ splits:
1414
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sdk-bundle.git"
1515
- prefix: "src/Instrumentation/CodeIgniter"
1616
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-codeigniter.git"
17+
- prefix: "src/Instrumentation/Curl"
18+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-curl.git"
1719
- prefix: "src/Instrumentation/ExtAmqp"
1820
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-ext-amqp.git"
1921
- prefix: "src/Instrumentation/ExtRdKafka"

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"psr-4": {
1818
"OpenTelemetry\\Contrib\\Aws\\": "src/Aws/src",
1919
"OpenTelemetry\\Contrib\\Context\\Swoole\\": "src/Context/Swoole/src",
20+
"OpenTelemetry\\Contrib\\Instrumentation\\Curl\\": "src/Instrumentation/Curl/src",
2021
"OpenTelemetry\\Contrib\\Instrumentation\\ExtAmqp\\": "src/Instrumentation/ExtAmqp/src",
2122
"OpenTelemetry\\Contrib\\Instrumentation\\ExtRdKafka\\": "src/Instrumentation/ExtRdKafka/src",
2223
"OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\": "src/Instrumentation/Laravel/src",
@@ -39,6 +40,7 @@
3940
"OpenTelemetry\\Contrib\\Symfony\\": "src/Symfony/src"
4041
},
4142
"files": [
43+
"src/Instrumentation/Curl/_register.php",
4244
"src/Instrumentation/ExtAmqp/_register.php",
4345
"src/Instrumentation/ExtRdKafka/_register.php",
4446
"src/Instrumentation/HttpAsyncClient/_register.php",
@@ -59,6 +61,7 @@
5961
"open-telemetry/contrib-aws": "self.version",
6062
"open-telemetry/contrib-sdk-bundle": "self.version",
6163
"open-telemetry/context-swoole": "self.version",
64+
"open-telemetry/opentelemetry-auto-curl": "self.version",
6265
"open-telemetry/opentelemetry-auto-laravel": "self.version",
6366
"open-telemetry/opentelemetry-auto-http-async": "self.version",
6467
"open-telemetry/opentelemetry-auto-io": "self.version",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
* text=auto
2+
3+
*.md diff=markdown
4+
*.php diff=php
5+
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/.php-cs-fixer.php export-ignore
9+
/phpstan.neon.dist export-ignore
10+
/phpunit.xml.dist export-ignore
11+
/psalm.xml.dist export-ignore
12+
/tests export-ignore
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->exclude('vendor')
4+
->exclude('var/cache')
5+
->in(__DIR__);
6+
7+
$config = new PhpCsFixer\Config();
8+
return $config->setRules([
9+
'concat_space' => ['spacing' => 'one'],
10+
'declare_equal_normalize' => ['space' => 'none'],
11+
'is_null' => true,
12+
'modernize_types_casting' => true,
13+
'ordered_imports' => true,
14+
'php_unit_construct' => true,
15+
'single_line_comment_style' => true,
16+
'yoda_style' => false,
17+
'@PSR2' => true,
18+
'array_syntax' => ['syntax' => 'short'],
19+
'blank_line_after_opening_tag' => true,
20+
'blank_line_before_statement' => true,
21+
'cast_spaces' => true,
22+
'declare_strict_types' => true,
23+
'type_declaration_spaces' => true,
24+
'include' => true,
25+
'lowercase_cast' => true,
26+
'new_with_parentheses' => true,
27+
'no_extra_blank_lines' => true,
28+
'no_leading_import_slash' => true,
29+
'echo_tag_syntax' => true,
30+
'no_unused_imports' => true,
31+
'no_useless_else' => true,
32+
'no_useless_return' => true,
33+
'phpdoc_order' => true,
34+
'phpdoc_scalar' => true,
35+
'phpdoc_types' => true,
36+
'short_scalar_cast' => true,
37+
'blank_lines_before_namespace' => true,
38+
'single_quote' => true,
39+
'trailing_comma_in_multiline' => true,
40+
])
41+
->setRiskyAllowed(true)
42+
->setFinder($finder);
43+

src/Instrumentation/Curl/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-curl/releases)
2+
[![Issues](https://img.shields.io/badge/issues-pink)](https://github.com/open-telemetry/opentelemetry-php/issues)
3+
[![Source](https://img.shields.io/badge/source-contrib-green)](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Instrumentation/curl)
4+
[![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-curl)
5+
[![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-curl/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-curl/)
6+
[![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-curl/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-curl/)
7+
8+
This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.
9+
10+
# OpenTelemetry curl auto-instrumentation
11+
12+
Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to
13+
install and configure the extension and SDK.
14+
15+
## Overview
16+
Auto-instrumentation hooks are registered via composer, and client kind spans will automatically be created when calling `curl_exec` or `curl_multi_exec` functions.
17+
18+
## Limitations
19+
The curl_multi instrumentation is not resilient to shortcomings in the application and requires proper implementation. If the application does not call the curl_multi_info_read function, the instrumentation will be unable to measure the execution time for individual requests-time will be aggregated for all transfers. Similarly, error detection will be impacted, as the error code information will be missing in this case. In case of encountered issues, it is recommended to review the application code and adjust it to match example #1 provided in [curl_multi_exec documentation](https://www.php.net/manual/en/function.curl-multi-exec.php).
20+
21+
## Configuration
22+
23+
The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration):
24+
25+
```shell
26+
OTEL_PHP_DISABLED_INSTRUMENTATIONS=curl
27+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use OpenTelemetry\Contrib\Instrumentation\Curl\CurlInstrumentation;
6+
use OpenTelemetry\SDK\Sdk;
7+
8+
if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(CurlInstrumentation::NAME) === true) {
9+
return;
10+
}
11+
12+
if (extension_loaded('opentelemetry') === false) {
13+
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry curl auto-instrumentation', E_USER_WARNING);
14+
15+
return;
16+
}
17+
18+
CurlInstrumentation::register();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "open-telemetry/opentelemetry-auto-curl",
3+
"description": "OpenTelemetry auto-instrumentation for curl",
4+
"keywords": [
5+
"opentelemetry",
6+
"otel",
7+
"open-telemetry",
8+
"tracing",
9+
"curl",
10+
"instrumentation"
11+
],
12+
"type": "library",
13+
"homepage": "https://opentelemetry.io/docs/php",
14+
"readme": "./README.md",
15+
"license": "Apache-2.0",
16+
"minimum-stability": "dev",
17+
"prefer-stable": true,
18+
"require": {
19+
"php": "^8.2",
20+
"ext-curl": "*",
21+
"ext-opentelemetry": "*",
22+
"open-telemetry/api": "^1.0",
23+
"open-telemetry/sem-conv": "^1.24"
24+
},
25+
"require-dev": {
26+
"friendsofphp/php-cs-fixer": "^3",
27+
"phan/phan": "^5.0",
28+
"php-http/mock-client": "*",
29+
"phpstan/phpstan": "^1.1",
30+
"phpstan/phpstan-phpunit": "^1.0",
31+
"psalm/plugin-phpunit": "^0.18.4",
32+
"open-telemetry/sdk": "^1.0",
33+
"phpunit/phpunit": "^9.5",
34+
"vimeo/psalm": "^5.0"
35+
},
36+
"autoload": {
37+
"psr-4": {
38+
"OpenTelemetry\\Contrib\\Instrumentation\\Curl\\": "src/"
39+
},
40+
"files": [
41+
"_register.php"
42+
]
43+
},
44+
"autoload-dev": {
45+
"psr-4": {
46+
"OpenTelemetry\\Tests\\Instrumentation\\Curl\\": "tests/"
47+
}
48+
},
49+
"config": {
50+
"allow-plugins": {
51+
"php-http/discovery": false,
52+
"tbachert/spi": false
53+
}
54+
}
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/phpstan/phpstan-phpunit/extension.neon
3+
4+
parameters:
5+
tmpDir: var/cache/phpstan
6+
level: 5
7+
paths:
8+
- src
9+
- tests

0 commit comments

Comments
 (0)