Skip to content

Commit f0e6db3

Browse files
authored
Slim and psr-15 auto-instrumentation (open-telemetry#81)
* slim and psr15 auto-instrumentation
1 parent 824c0b7 commit f0e6db3

32 files changed

+1669
-19
lines changed

.github/workflows/php.yml

+7-11
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ jobs:
1515
matrix:
1616
operating-system: [ubuntu-latest]
1717
php-version: ['7.4', '8.0', '8.1']
18-
project: ['Aws', 'Symfony']
19-
# todo exclude some matrix combinations based on php version requirements
20-
# https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs#excluding-matrix-configurations
18+
project: ['Aws', 'Symfony', 'Instrumentation/Slim', 'Instrumentation/Psr15']
19+
exclude:
20+
- project: 'Instrumentation/Slim'
21+
php-version: 7.4
22+
- project: 'Instrumentation/Psr15'
23+
php-version: 7.4
2124

2225
steps:
2326
- uses: actions/checkout@v3
@@ -27,7 +30,7 @@ jobs:
2730
with:
2831
php-version: ${{ matrix.php-version }}
2932
coverage: xdebug
30-
extensions: ast, grpc
33+
extensions: ast, grpc, otel_instrumentation-open-telemetry/opentelemetry-php-instrumentation@main
3134

3235
- name: Validate composer.json and composer.lock
3336
run: composer validate
@@ -79,10 +82,3 @@ jobs:
7982
- name: Code Coverage
8083
working-directory: src/${{ matrix.project }}
8184
run: bash <(curl -s https://codecov.io/bash) -F ${{ matrix.php-version }}
82-
83-
packages:
84-
uses: opentelemetry-php/gh-workflows/.github/workflows/validate-packages.yml@main
85-
needs: php
86-
with:
87-
matrix_extension: '["ast, json, grpc"]'
88-
install_directory: '~/.test/.packages'

.gitsplit.yml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ splits:
1010
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-aws.git"
1111
- prefix: "src/Symfony"
1212
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sdk-bundle.git"
13+
- prefix: "src/Instrumentation/Psr15"
14+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr15.git"
15+
- prefix: "src/Instrumentation/Slim"
16+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-slim.git"
1317

1418
# List of references to split (defined as regexp)
1519
origins:

.php-cs-fixer.php

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
$finder = PhpCsFixer\Finder::create()
33
->exclude('vendor')
44
->exclude('var/cache')
5-
->exclude('proto')
65
->in(__DIR__);
76

87
$config = new PhpCsFixer\Config();

Makefile

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ help: ## Show this help
1111
@echo "example: PROJECT=Aws PHP_VERSION=8.1 make <command>"
1212
@printf "\033[33m%s:\033[0m\n" 'Available commands'
1313
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf " \033[32m%-18s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
14-
all: update style packages-composer phan psalm phpstan test ## Everything
14+
all: update style validate phan psalm phpstan test ## Everything
15+
build: ## Build image
16+
$(DOCKER_COMPOSE) build --build-arg PHP_VERSION php
1517
install: ## Install dependencies
1618
$(DC_RUN_PHP) env XDEBUG_MODE=off composer install
1719
update: ## Update dependencies
@@ -20,7 +22,7 @@ test: test-unit test-integration ## Run unit and integration tests
2022
test-unit: ## Run unit tests
2123
$(DC_RUN_PHP) env XDEBUG_MODE=coverage vendor/bin/phpunit --testsuite unit --colors=always --coverage-text --testdox --coverage-clover coverage.clover --coverage-html=tests/coverage/html
2224
test-integration: ## Run integration tests
23-
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/phpunit --testsuite integration --coverage-text --testdox --colors=always
25+
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/phpunit --testsuite integration --testdox --colors=always
2426
test-coverage: ## Run unit tests and generate coverage
2527
$(DC_RUN_PHP) env XDEBUG_MODE=coverage vendor/bin/phpunit --testsuite unit --coverage-html=tests/coverage/html
2628
phan: ## Run phan
@@ -31,12 +33,14 @@ psalm-info: ## Run psalm with info
3133
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/psalm --show-info=true --threads=1
3234
phpstan: ## Run phpstan
3335
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/phpstan analyse --memory-limit=256M
34-
packages-composer: ## Validate composer packages
36+
validate: ## Validate composer file
37+
$(DC_RUN_PHP) env XDEBUG_MODE=off composer validate
38+
packages-composer: ## Validate all composer packages
3539
$(DC_RUN) php env XDEBUG_MODE=off vendor/bin/otel packages:composer:validate
3640
bash: ## Bash shell
3741
$(DC_RUN_PHP) bash
3842
style: ## Run php-cs-fixer
3943
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --using-cache=no -vvv
4044
split: ## git-split dry run
41-
docker-compose -f docker/gitsplit/docker-compose.yaml --env-file ./.env up
45+
${DOCKER_COMPOSE} -f docker/gitsplit/docker-compose.yaml --env-file ./.env up
4246
FORCE:

composer.json

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
"composer/xdebug-handler": "^2.0",
1414
"open-telemetry/dev-tools": "dev-main"
1515
},
16+
"autoload": {
17+
"psr-4": {
18+
"OpenTelemetry\\Contrib\\Aws\\": "src/Aws/src",
19+
"OpenTelemetry\\Contrib\\Symfony\\": "src/Symfony/src",
20+
"OpenTelemetry\\Contrib\\Instrumentation\\Psr15\\": "src/Instrumentation/Psr15/src",
21+
"OpenTelemetry\\Contrib\\Instrumentation\\Slim\\": "src/Instrumentation/Slim/src"
22+
}
23+
},
1624
"config": {
1725
"sort-packages": true,
1826
"allow-plugins": {

docker-compose.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
version: '3.7'
22
services:
33
php:
4-
image: ghcr.io/open-telemetry/opentelemetry-php/opentelemetry-php-base:${PHP_VERSION:-7.4}
4+
build:
5+
context: ./docker
6+
dockerfile: Dockerfile
7+
args:
8+
- PHP_VERSION
59
volumes:
610
- ./:/usr/src/myapp
711
user: "${PHP_USER}:root"
812
environment:
913
XDEBUG_MODE: ${XDEBUG_MODE:-off}
1014
XDEBUG_CONFIG: ${XDEBUG_CONFIG:-''}
15+
PHP_IDE_CONFIG: ${PHP_IDE_CONFIG:-''}
1116
zipkin:
1217
image: openzipkin/zipkin-slim
1318
ports:

docker/Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ARG PHP_VERSION=7.4
2+
FROM php:${PHP_VERSION}-cli-alpine as php_build
3+
4+
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
5+
6+
RUN chmod +x /usr/local/bin/install-php-extensions; \
7+
apk add --update binutils; \
8+
install-php-extensions \
9+
ast \
10+
xdebug \
11+
zip \
12+
pcntl \
13+
intl \
14+
@composer \
15+
open-telemetry/opentelemetry-php-instrumentation@main \
16+
; \
17+
# strip debug symbols from extensions to reduce size
18+
find /usr/local/lib/php/extensions -name "*.so" -exec strip --strip-debug {} \;;
19+
20+
FROM php_build
21+
22+
WORKDIR /usr/src/myapp
23+
24+
RUN apk add --no-cache bash git; \
25+
find /usr/local/lib/php/extensions -type d -exec chmod +x -R {} \;; \
26+
addgroup -g "1000" -S php; \
27+
adduser --system \
28+
--gecos "" \
29+
--ingroup "php" \
30+
--uid "1000" \
31+
"php";
32+
33+
USER php

src/Aws/.php-cs-fixer.php

-1
This file was deleted.

src/Aws/.php-cs-fixer.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->exclude('vendor')
4+
->exclude('var/cache')
5+
->exclude('proto')
6+
->in(__DIR__);
7+
8+
$config = new PhpCsFixer\Config();
9+
return $config->setRules([
10+
'concat_space' => ['spacing' => 'one'],
11+
'declare_equal_normalize' => ['space' => 'none'],
12+
'is_null' => true,
13+
'modernize_types_casting' => true,
14+
'ordered_imports' => true,
15+
'php_unit_construct' => true,
16+
'single_line_comment_style' => true,
17+
'yoda_style' => false,
18+
'@PSR2' => true,
19+
'array_syntax' => ['syntax' => 'short'],
20+
'blank_line_after_opening_tag' => true,
21+
'blank_line_before_statement' => true,
22+
'cast_spaces' => true,
23+
'declare_strict_types' => true,
24+
'function_typehint_space' => true,
25+
'include' => true,
26+
'lowercase_cast' => true,
27+
'new_with_braces' => true,
28+
'no_extra_blank_lines' => true,
29+
'no_leading_import_slash' => true,
30+
'echo_tag_syntax' => true,
31+
'no_unused_imports' => true,
32+
'no_useless_else' => true,
33+
'no_useless_return' => true,
34+
'phpdoc_order' => true,
35+
'phpdoc_scalar' => true,
36+
'phpdoc_types' => true,
37+
'short_scalar_cast' => true,
38+
'single_blank_line_before_namespace' => true,
39+
'single_quote' => true,
40+
'trailing_comma_in_multiline' => true,
41+
])
42+
->setRiskyAllowed(true)
43+
->setFinder($finder);
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->exclude('vendor')
4+
->exclude('var/cache')
5+
->exclude('proto')
6+
->in(__DIR__);
7+
8+
$config = new PhpCsFixer\Config();
9+
return $config->setRules([
10+
'concat_space' => ['spacing' => 'one'],
11+
'declare_equal_normalize' => ['space' => 'none'],
12+
'is_null' => true,
13+
'modernize_types_casting' => true,
14+
'ordered_imports' => true,
15+
'php_unit_construct' => true,
16+
'single_line_comment_style' => true,
17+
'yoda_style' => false,
18+
'@PSR2' => true,
19+
'array_syntax' => ['syntax' => 'short'],
20+
'blank_line_after_opening_tag' => true,
21+
'blank_line_before_statement' => true,
22+
'cast_spaces' => true,
23+
'declare_strict_types' => true,
24+
'function_typehint_space' => true,
25+
'include' => true,
26+
'lowercase_cast' => true,
27+
'new_with_braces' => true,
28+
'no_extra_blank_lines' => true,
29+
'no_leading_import_slash' => true,
30+
'echo_tag_syntax' => true,
31+
'no_unused_imports' => true,
32+
'no_useless_else' => true,
33+
'no_useless_return' => true,
34+
'phpdoc_order' => true,
35+
'phpdoc_scalar' => true,
36+
'phpdoc_types' => true,
37+
'short_scalar_cast' => true,
38+
'single_blank_line_before_namespace' => true,
39+
'single_quote' => true,
40+
'trailing_comma_in_multiline' => true,
41+
])
42+
->setRiskyAllowed(true)
43+
->setFinder($finder);
44+

src/Instrumentation/Psr15/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# OpenTelemetry PSR-15 auto-instrumentation
2+
3+
## Requirements
4+
5+
* OpenTelemetry extension
6+
* OpenTelemetry SDK and exporters (required to actually export traces)
7+
8+
## Overview
9+
Auto-instrumentation hooks are registered via composer, and spans will automatically be created for each PSR-15 middleware that is executed.
10+
11+
To export spans, you will need to create and register a `TracerProvider` early in your application's lifecycle:
12+
13+
```php
14+
<?php
15+
require_once 'vendor/autoload.php';
16+
17+
$tracerProvider = /*create tracer provider*/;
18+
$scope = \OpenTelemetry\API\Common\Instrumentation\Configurator::create()
19+
->withTracerProvider($tracerProvider)
20+
->activate();
21+
22+
//your application runs here
23+
24+
$scope->detach();
25+
$tracerProvider->shutdown();
26+
```
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
\OpenTelemetry\Contrib\Instrumentation\Psr15\Psr15Instrumentation::register();
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "open-telemetry/opentelemetry-auto-psr15",
3+
"description": "OpenTelemetry auto-instrumentation for psr15 middleware.",
4+
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "psr15", "instrumentation"],
5+
"type": "library",
6+
"homepage": "https://opentelemetry.io/docs/php",
7+
"readme": "./README.md",
8+
"license": "Apache-2.0",
9+
"minimum-stability": "dev",
10+
"require": {
11+
"php": "^8.0",
12+
"ext-otel_instrumentation": "*",
13+
"open-telemetry/api": "dev-main",
14+
"psr/http-server-middleware": "^1"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"OpenTelemetry\\Contrib\\Instrumentation\\Psr15\\": "src/"
19+
},
20+
"files": [
21+
"_register.php"
22+
]
23+
},
24+
"require-dev": {
25+
"friendsofphp/php-cs-fixer": "^3",
26+
"nyholm/psr7": "*",
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.16",
32+
"open-telemetry/sdk": "dev-main",
33+
"phpunit/phpunit": "^9.5",
34+
"vimeo/psalm": "^4.0"
35+
}
36+
}
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
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
6+
backupGlobals="false"
7+
backupStaticAttributes="false"
8+
cacheResult="false"
9+
colors="false"
10+
convertErrorsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertWarningsToExceptions="true"
13+
forceCoversAnnotation="false"
14+
processIsolation="false"
15+
stopOnError="false"
16+
stopOnFailure="false"
17+
stopOnIncomplete="false"
18+
stopOnSkipped="false"
19+
stopOnRisky="false"
20+
timeoutForSmallTests="1"
21+
timeoutForMediumTests="10"
22+
timeoutForLargeTests="60"
23+
verbose="true">
24+
25+
<coverage processUncoveredFiles="true" disableCodeCoverageIgnore="false">
26+
<include>
27+
<directory>src</directory>
28+
</include>
29+
</coverage>
30+
31+
<php>
32+
<ini name="date.timezone" value="UTC" />
33+
<ini name="display_errors" value="On" />
34+
<ini name="display_startup_errors" value="On" />
35+
<ini name="error_reporting" value="E_ALL" />
36+
</php>
37+
38+
<testsuites>
39+
<testsuite name="unit">
40+
<directory>tests/Unit</directory>
41+
</testsuite>
42+
<testsuite name="integration">
43+
<directory>tests/Integration</directory>
44+
</testsuite>
45+
</testsuites>
46+
47+
</phpunit>
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="3"
4+
cacheDirectory="var/cache/psalm"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
8+
<projectFiles>
9+
<directory name="src"/>
10+
<directory name="tests"/>
11+
</projectFiles>
12+
<plugins>
13+
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
14+
</plugins>
15+
</psalm>

0 commit comments

Comments
 (0)