Skip to content

Commit f892be9

Browse files
authored
Auto instrumentation for mysqli (open-telemetry#318)
1 parent 6542b13 commit f892be9

20 files changed

+3004
-3
lines changed

.github/workflows/php.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
'Instrumentation/IO',
3333
'Instrumentation/Laravel',
3434
'Instrumentation/MongoDB',
35+
'Instrumentation/MySqli',
3536
'Instrumentation/OpenAIPHP',
3637
'Instrumentation/PDO',
3738
# Sort PSRs numerically.
@@ -92,6 +93,12 @@ jobs:
9293
php-version: 8.0
9394
- project: 'Instrumentation/Curl'
9495
php-version: 8.1
96+
- project: 'Instrumentation/MySqli'
97+
php-version: 7.4
98+
- project: 'Instrumentation/MySqli'
99+
php-version: 8.0
100+
- project: 'Instrumentation/MySqli'
101+
php-version: 8.1
95102
- project: 'Instrumentation/PDO'
96103
php-version: 7.4
97104
- project: 'Instrumentation/PDO'
@@ -136,7 +143,7 @@ jobs:
136143
with:
137144
php-version: ${{ matrix.php-version }}
138145
coverage: xdebug
139-
extensions: ast, amqp, grpc, opentelemetry, rdkafka
146+
extensions: ast, amqp, grpc, opentelemetry, rdkafka, mysqli
140147

141148
- name: Validate composer.json and composer.lock
142149
run: composer validate
@@ -196,6 +203,11 @@ jobs:
196203
run: |
197204
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:29092,PLAINTEXT_HOST://localhost:9092 docker compose up kafka -d --wait
198205
206+
- name: Start Mysql
207+
if: ${{ matrix.project == 'Instrumentation/MySqli' }}
208+
run: |
209+
docker compose up mysql -d --wait
210+
199211
- name: Run PHPUnit
200212
working-directory: src/${{ matrix.project }}
201213
run: vendor/bin/phpunit

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ splits:
3030
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-laravel.git"
3131
- prefix: "src/Instrumentation/MongoDB"
3232
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-mongodb.git"
33+
- prefix: "src/Instrumentation/MySqli"
34+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-mysqli.git"
3335
- prefix: "src/Instrumentation/OpenAIPHP"
3436
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-openai.git"
3537
- prefix: "src/Instrumentation/PDO"

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"OpenTelemetry\\Contrib\\Instrumentation\\HttpAsyncClient\\": "src/Instrumentation/HttpAsyncClient/src",
2525
"OpenTelemetry\\Contrib\\Instrumentation\\IO\\": "src/Instrumentation/IO/src",
2626
"OpenTelemetry\\Contrib\\Instrumentation\\MongoDB\\": "src/Instrumentation/MongoDB/src",
27+
"OpenTelemetry\\Contrib\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/src",
2728
"OpenTelemetry\\Contrib\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/src",
2829
"OpenTelemetry\\Contrib\\Instrumentation\\Psr3\\": "src/Instrumentation/Psr3/src",
2930
"OpenTelemetry\\Contrib\\Instrumentation\\Psr15\\": "src/Instrumentation/Psr15/src",
@@ -47,6 +48,7 @@
4748
"src/Instrumentation/IO/_register.php",
4849
"src/Instrumentation/Laravel/_register.php",
4950
"src/Instrumentation/MongoDB/_register.php",
51+
"src/Instrumentation/MySqli/_register.php",
5052
"src/Instrumentation/PDO/_register.php",
5153
"src/Instrumentation/Psr3/_register.php",
5254
"src/Instrumentation/Psr15/_register.php",
@@ -66,6 +68,7 @@
6668
"open-telemetry/opentelemetry-auto-http-async": "self.version",
6769
"open-telemetry/opentelemetry-auto-io": "self.version",
6870
"open-telemetry/opentelemetry-auto-mongodb": "self.version",
71+
"open-telemetry/opentelemetry-auto-mysqli": "self.version",
6972
"open-telemetry/opentelemetry-auto-pdo": "self.version",
7073
"open-telemetry/opentelemetry-auto-psr3": "self.version",
7174
"open-telemetry/opentelemetry-auto-psr15": "self.version",

docker-compose.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ services:
1414
PHP_IDE_CONFIG: ${PHP_IDE_CONFIG:-''}
1515
RABBIT_HOST: ${RABBIT_HOST:-rabbitmq}
1616
KAFKA_HOST: ${KAFKA_HOST:-kafka}
17+
MYSQL_HOST: ${MYSQL_HOST:-mysql}
18+
1719

1820
zipkin:
1921
image: openzipkin/zipkin-slim
@@ -61,4 +63,20 @@ services:
6163
volumes:
6264
- ./docker/kafka/update_run.sh:/tmp/update_run.sh
6365

64-
66+
mysql:
67+
image: mysql:8.0
68+
hostname: mysql
69+
ports:
70+
- "3306:3306/tcp"
71+
environment:
72+
MYSQL_ROOT_PASSWORD: root_password
73+
MYSQL_DATABASE: otel_db
74+
MYSQL_USER: otel_user
75+
MYSQL_PASSWORD: otel_passwd
76+
healthcheck:
77+
test: mysql -uotel_user -potel_passwd -e "USE otel_db;"
78+
interval: 30s
79+
timeout: 30s
80+
retries: 3
81+
volumes:
82+
- ./docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql

docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ RUN install-php-extensions \
77
opentelemetry \
88
mongodb \
99
amqp \
10-
rdkafka
10+
rdkafka \
11+
mysqli
1112

1213
USER php

docker/mysql/init.sql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
CREATE DATABASE IF NOT EXISTS otel_db2;
2+
CREATE USER 'otel_user2'@'%' IDENTIFIED BY 'otel_passwd';
3+
4+
5+
GRANT ALL PRIVILEGES ON *.* TO 'otel_user'@'%';
6+
GRANT ALL PRIVILEGES ON *.* TO 'otel_user2'@'%';
7+
FLUSH PRIVILEGES;
8+
9+
10+
USE otel_db;
11+
12+
CREATE TABLE users (
13+
id INT AUTO_INCREMENT PRIMARY KEY,
14+
name VARCHAR(255) NOT NULL,
15+
email VARCHAR(255) UNIQUE NOT NULL,
16+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
17+
);
18+
19+
INSERT INTO users (name, email) VALUES
20+
('John Doe', '[email protected]'),
21+
('Jane Smith', '[email protected]'),
22+
('Bob Johnson', '[email protected]');
23+
24+
CREATE TABLE products (
25+
id INT AUTO_INCREMENT PRIMARY KEY,
26+
name VARCHAR(255) NOT NULL,
27+
price DECIMAL(10, 2) NOT NULL,
28+
stock INT NOT NULL DEFAULT 0
29+
);
30+
31+
INSERT INTO products (name, price, stock) VALUES
32+
('Laptop', 999.99, 10),
33+
('Smartphone', 499.99, 25),
34+
('Headphones', 49.99, 50);
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+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-mysqli/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/MySqli)
4+
[![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-mysqli)
5+
[![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-mysqli/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-mysqli/)
6+
[![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-mysqli/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-mysqli/)
7+
8+
This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.
9+
10+
# OpenTelemetry mysqli 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 following functions or methods:
17+
18+
* `mysqli_connect`
19+
* `mysqli::__construct`
20+
* `mysqli::connect`
21+
* `mysqli_real_connect`
22+
* `mysqli::real_connect`
23+
24+
* `mysqli_query`
25+
* `mysqli::query`
26+
* `mysqli_real_query`
27+
* `mysqli::real_query`
28+
* `mysqli_execute_query`
29+
* `mysqli::execute_query`
30+
* `mysqli_multi_query`
31+
* `mysqli::multi_query`
32+
* `mysqli_next_result`
33+
* `mysqli::next_result`
34+
35+
* `mysqli_begin_transaction`
36+
* `mysqli::begin_transaction`
37+
* `mysqli_rollback`
38+
* `mysqli::rollback`
39+
* `mysqli_commit`
40+
* `mysqli::commit`
41+
*
42+
* `mysqli_stmt_execute`
43+
* `mysqli_stmt::execute`
44+
* `mysqli_stmt_next_result`
45+
* `mysqli_stmt::next_result`
46+
47+
## Configuration
48+
49+
### Disabling mysqli instrumentation
50+
51+
The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration):
52+
53+
```shell
54+
OTEL_PHP_DISABLED_INSTRUMENTATIONS=mysqli
55+
```
56+

0 commit comments

Comments
 (0)