Skip to content
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

Auto instrumentation for mysqli #318

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
'Instrumentation/IO',
'Instrumentation/Laravel',
'Instrumentation/MongoDB',
'Instrumentation/MySqli',
'Instrumentation/OpenAIPHP',
'Instrumentation/PDO',
# Sort PSRs numerically.
Expand Down Expand Up @@ -92,6 +93,12 @@ jobs:
php-version: 8.0
- project: 'Instrumentation/Curl'
php-version: 8.1
- project: 'Instrumentation/MySqli'
php-version: 7.4
- project: 'Instrumentation/MySqli'
php-version: 8.0
- project: 'Instrumentation/MySqli'
php-version: 8.1
- project: 'Instrumentation/PDO'
php-version: 7.4
- project: 'Instrumentation/PDO'
Expand Down Expand Up @@ -132,7 +139,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug
extensions: ast, amqp, grpc, opentelemetry, rdkafka
extensions: ast, amqp, grpc, opentelemetry, rdkafka, mysqli

- name: Validate composer.json and composer.lock
run: composer validate
Expand Down Expand Up @@ -192,6 +199,11 @@ jobs:
run: |
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:29092,PLAINTEXT_HOST://localhost:9092 docker compose up kafka -d --wait

- name: Start Mysql
if: ${{ matrix.project == 'Instrumentation/MySqli' }}
run: |
docker compose up mysql -d --wait

- name: Run PHPUnit
working-directory: src/${{ matrix.project }}
run: vendor/bin/phpunit
Expand Down
2 changes: 2 additions & 0 deletions .gitsplit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ splits:
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-laravel.git"
- prefix: "src/Instrumentation/MongoDB"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-mongodb.git"
- prefix: "src/Instrumentation/MySqli"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-mysqli.git"
- prefix: "src/Instrumentation/OpenAIPHP"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-openai.git"
- prefix: "src/Instrumentation/PDO"
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"OpenTelemetry\\Contrib\\Instrumentation\\HttpAsyncClient\\": "src/Instrumentation/HttpAsyncClient/src",
"OpenTelemetry\\Contrib\\Instrumentation\\IO\\": "src/Instrumentation/IO/src",
"OpenTelemetry\\Contrib\\Instrumentation\\MongoDB\\": "src/Instrumentation/MongoDB/src",
"OpenTelemetry\\Contrib\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/src",
"OpenTelemetry\\Contrib\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/src",
"OpenTelemetry\\Contrib\\Instrumentation\\Psr3\\": "src/Instrumentation/Psr3/src",
"OpenTelemetry\\Contrib\\Instrumentation\\Psr15\\": "src/Instrumentation/Psr15/src",
Expand All @@ -47,6 +48,7 @@
"src/Instrumentation/IO/_register.php",
"src/Instrumentation/Laravel/_register.php",
"src/Instrumentation/MongoDB/_register.php",
"src/Instrumentation/MySqli/_register.php",
"src/Instrumentation/PDO/_register.php",
"src/Instrumentation/Psr3/_register.php",
"src/Instrumentation/Psr15/_register.php",
Expand All @@ -66,6 +68,7 @@
"open-telemetry/opentelemetry-auto-http-async": "self.version",
"open-telemetry/opentelemetry-auto-io": "self.version",
"open-telemetry/opentelemetry-auto-mongodb": "self.version",
"open-telemetry/opentelemetry-auto-mysqli": "self.version",
"open-telemetry/opentelemetry-auto-pdo": "self.version",
"open-telemetry/opentelemetry-auto-psr3": "self.version",
"open-telemetry/opentelemetry-auto-psr15": "self.version",
Expand Down
20 changes: 19 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ services:
PHP_IDE_CONFIG: ${PHP_IDE_CONFIG:-''}
RABBIT_HOST: ${RABBIT_HOST:-rabbitmq}
KAFKA_HOST: ${KAFKA_HOST:-kafka}
MYSQL_HOST: ${MYSQL_HOST:-mysql}


zipkin:
image: openzipkin/zipkin-slim
Expand Down Expand Up @@ -61,4 +63,20 @@ services:
volumes:
- ./docker/kafka/update_run.sh:/tmp/update_run.sh


mysql:
image: mysql:8.0
hostname: mysql
ports:
- "3306:3306/tcp"
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: otel_db
MYSQL_USER: otel_user
MYSQL_PASSWORD: otel_passwd
healthcheck:
test: mysql -uotel_user -potel_passwd -e "USE otel_db;"
interval: 30s
timeout: 30s
retries: 3
volumes:
- ./docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN install-php-extensions \
opentelemetry \
mongodb \
amqp \
rdkafka
rdkafka \
mysqli

USER php
34 changes: 34 additions & 0 deletions docker/mysql/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CREATE DATABASE IF NOT EXISTS otel_db2;
CREATE USER 'otel_user2'@'%' IDENTIFIED BY 'otel_passwd';


GRANT ALL PRIVILEGES ON *.* TO 'otel_user'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'otel_user2'@'%';
FLUSH PRIVILEGES;


USE otel_db;

CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO users (name, email) VALUES
('John Doe', '[email protected]'),
('Jane Smith', '[email protected]'),
('Bob Johnson', '[email protected]');

CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT NOT NULL DEFAULT 0
);

INSERT INTO products (name, price, stock) VALUES
('Laptop', 999.99, 10),
('Smartphone', 499.99, 25),
('Headphones', 49.99, 50);
12 changes: 12 additions & 0 deletions src/Instrumentation/MySqli/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
* text=auto

*.md diff=markdown
*.php diff=php

/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.php export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml.dist export-ignore
/tests export-ignore
1 change: 1 addition & 0 deletions src/Instrumentation/MySqli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
43 changes: 43 additions & 0 deletions src/Instrumentation/MySqli/.php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('var/cache')
->in(__DIR__);

$config = new PhpCsFixer\Config();
return $config->setRules([
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'is_null' => true,
'modernize_types_casting' => true,
'ordered_imports' => true,
'php_unit_construct' => true,
'single_line_comment_style' => true,
'yoda_style' => false,
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => true,
'cast_spaces' => true,
'declare_strict_types' => true,
'type_declaration_spaces' => true,
'include' => true,
'lowercase_cast' => true,
'new_with_parentheses' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'echo_tag_syntax' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_types' => true,
'short_scalar_cast' => true,
'blank_lines_before_namespace' => true,
'single_quote' => true,
'trailing_comma_in_multiline' => true,
])
->setRiskyAllowed(true)
->setFinder($finder);

56 changes: 56 additions & 0 deletions src/Instrumentation/MySqli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-mysqli/releases)
[![Issues](https://img.shields.io/badge/issues-pink)](https://github.com/open-telemetry/opentelemetry-php/issues)
[![Source](https://img.shields.io/badge/source-contrib-green)](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Instrumentation/MySqli)
[![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-mysqli)
[![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-mysqli/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-mysqli/)
[![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-mysqli/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-mysqli/)

This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.

# OpenTelemetry mysqli auto-instrumentation

Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to
install and configure the extension and SDK.

## Overview
Auto-instrumentation hooks are registered via composer, and client kind spans will automatically be created when calling following functions or methods:

* `mysqli_connect`
* `mysqli::__construct`
* `mysqli::connect`
* `mysqli_real_connect`
* `mysqli::real_connect`

* `mysqli_query`
* `mysqli::query`
* `mysqli_real_query`
* `mysqli::real_query`
* `mysqli_execute_query`
* `mysqli::execute_query`
* `mysqli_multi_query`
* `mysqli::multi_query`
* `mysqli_next_result`
* `mysqli::next_result`

* `mysqli_begin_transaction`
* `mysqli::begin_transaction`
* `mysqli_rollback`
* `mysqli::rollback`
* `mysqli_commit`
* `mysqli::commit`
*
* `mysqli_stmt_execute`
* `mysqli_stmt::execute`
* `mysqli_stmt_next_result`
* `mysqli_stmt::next_result`

## Configuration

### Disabling mysqli instrumentation

The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration):

```shell
OTEL_PHP_DISABLED_INSTRUMENTATIONS=mysqli
```

18 changes: 18 additions & 0 deletions src/Instrumentation/MySqli/_register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use OpenTelemetry\Contrib\Instrumentation\MySqli\MySqliInstrumentation;
use OpenTelemetry\SDK\Sdk;

if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(MySqliInstrumentation::NAME) === true) {
return;
}

if (extension_loaded('opentelemetry') === false) {
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry mysqli auto-instrumentation', E_USER_WARNING);

return;
}

MySqliInstrumentation::register();
59 changes: 59 additions & 0 deletions src/Instrumentation/MySqli/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "open-telemetry/opentelemetry-auto-mysqli",
"description": "OpenTelemetry auto-instrumentation for mysqli",
"keywords": [
"opentelemetry",
"otel",
"open-telemetry",
"tracing",
"mysql",
"mysqli",
"instrumentation"
],
"type": "library",
"homepage": "https://opentelemetry.io/docs/php",
"readme": "./README.md",
"license": "Apache-2.0",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.2",
"ext-mysqli": "*",
"ext-opentelemetry": "*",
"open-telemetry/api": "^1.0",
"open-telemetry/sem-conv": "^1.27.1",
"symfony/polyfill-mbstring": "^1.31"
},
"suggest": {
"ext-mbstring": "For better performance than symfony/polyfill-mbstring"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"phan/phan": "^5.0",
"php-http/mock-client": "*",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"psalm/plugin-phpunit": "^0.18.4",
"open-telemetry/sdk": "^1.0",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^5.0"
},
"autoload": {
"psr-4": {
"OpenTelemetry\\Contrib\\Instrumentation\\MySqli\\": "src/"
},
"files": [
"_register.php"
]
},
"autoload-dev": {
"psr-4": {
"OpenTelemetry\\Tests\\Instrumentation\\MySqli\\": "tests/"
}
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
9 changes: 9 additions & 0 deletions src/Instrumentation/MySqli/phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
tmpDir: var/cache/phpstan
level: 5
paths:
- src
- tests
Loading
Loading