|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Zipkin\Instrumentation\Mysqli; |
| 4 | + |
| 5 | +use mysqli_result; |
| 6 | +use Zipkin\Tracer; |
| 7 | +use Zipkin\Span; |
| 8 | +use Zipkin\Endpoint; |
| 9 | + |
| 10 | +use InvalidArgumentException; |
| 11 | +use const Zipkin\Tags\ERROR; |
| 12 | + |
| 13 | +/** |
| 14 | + * Mysqli is an instrumented extension for Mysqli. |
| 15 | + * Function signatures come are borrowed from |
| 16 | + * https://github.com/php/php-src/blob/master/ext/mysqli/mysqli.stub.php |
| 17 | + */ |
| 18 | +final class Mysqli extends \Mysqli |
| 19 | +{ |
| 20 | + private const DEFAULT_OPTIONS = [ |
| 21 | + 'tag_query' => false, |
| 22 | + 'remote_endpoint' => null, |
| 23 | + 'default_tags' => [], |
| 24 | + ]; |
| 25 | + |
| 26 | + private Tracer $tracer; |
| 27 | + |
| 28 | + private array $options; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + Tracer $tracer, |
| 32 | + array $options = [], |
| 33 | + string $host = null, |
| 34 | + string $user = null, |
| 35 | + string $password = null, |
| 36 | + string $database = '', |
| 37 | + int $port = null, |
| 38 | + string $socket = null |
| 39 | + ) { |
| 40 | + self::validateOptions($options); |
| 41 | + $this->tracer = $tracer; |
| 42 | + $this->options = $options + self::DEFAULT_OPTIONS; |
| 43 | + parent::__construct( |
| 44 | + $host ?? (ini_get('mysqli.default_host') ?: ''), |
| 45 | + $user ?? (ini_get('mysqli.default_user') ?: ''), |
| 46 | + $password ?? (ini_get('mysqli.default_pw') ?: ''), |
| 47 | + $database, |
| 48 | + $port ?? (($defaultPort = ini_get('mysqli.default_port')) ? (int) $defaultPort : 3306), |
| 49 | + $socket ?? (ini_get('mysqli.default_socket') ?: '') |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private static function validateOptions(array $opts): void |
| 54 | + { |
| 55 | + if (array_key_exists('tag_query', $opts) && ($opts['tag_query'] !== (bool) $opts['tag_query'])) { |
| 56 | + throw new InvalidArgumentException('Invalid tag_query, bool expected'); |
| 57 | + } |
| 58 | + |
| 59 | + if (array_key_exists('remote_endpoint', $opts) && !($opts['remote_endpoint'] instanceof Endpoint)) { |
| 60 | + throw new InvalidArgumentException(sprintf('Invalid remote_endpoint, %s expected', Endpoint::class)); |
| 61 | + } |
| 62 | + |
| 63 | + if (array_key_exists('default_tags', $opts) && ($opts['default_tags'] !== (array) $opts['default_tags'])) { |
| 64 | + throw new InvalidArgumentException('Invalid default_tags, array expected'); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private function addsTagsAndRemoteEndpoint(Span $span, string $query = null): void |
| 69 | + { |
| 70 | + if ($query !== null && $this->options['tag_query']) { |
| 71 | + $span->tag('sql.query', $query); |
| 72 | + } |
| 73 | + |
| 74 | + if ($this->options['remote_endpoint'] !== null) { |
| 75 | + $span->setRemoteEndpoint($this->options['remote_endpoint']); |
| 76 | + } |
| 77 | + |
| 78 | + foreach ($this->options['default_tags'] as $key => $value) { |
| 79 | + $span->tag($key, $value); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Performs a query on the database |
| 85 | + * |
| 86 | + * @return mysqli_result|bool |
| 87 | + * @alias mysqli_query |
| 88 | + */ |
| 89 | + public function query(string $query, int $resultmode = MYSQLI_STORE_RESULT) |
| 90 | + { |
| 91 | + if ($resultmode === MYSQLI_ASYNC) { |
| 92 | + // if $resultmode is async, making the timing on this execution |
| 93 | + // does not make much sense. For now we just skip tracing on this. |
| 94 | + return parent::query($query, $resultmode); |
| 95 | + } |
| 96 | + |
| 97 | + $span = $this->tracer->nextSpan(); |
| 98 | + $span->setName('sql/query'); |
| 99 | + $this->addsTagsAndRemoteEndpoint($span, $query); |
| 100 | + if ($this->options['tag_query']) { |
| 101 | + $span->tag('sql.query', $query); |
| 102 | + } |
| 103 | + |
| 104 | + $span->start(); |
| 105 | + try { |
| 106 | + $result = parent::query($query, $resultmode); |
| 107 | + if ($result === false) { |
| 108 | + $span->tag(ERROR, 'true'); |
| 109 | + } |
| 110 | + return $result; |
| 111 | + } finally { |
| 112 | + $span->finish(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return bool |
| 118 | + * @alias mysqli_real_query |
| 119 | + */ |
| 120 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName |
| 121 | + public function real_query(string $query) |
| 122 | + { |
| 123 | + $span = $this->tracer->nextSpan(); |
| 124 | + $span->setName('sql/query'); |
| 125 | + $this->addsTagsAndRemoteEndpoint($span, $query); |
| 126 | + |
| 127 | + $span->start(); |
| 128 | + try { |
| 129 | + $result = parent::real_query($query); |
| 130 | + if ($result === false) { |
| 131 | + $span->tag(ERROR, 'true'); |
| 132 | + } |
| 133 | + return $result; |
| 134 | + } finally { |
| 135 | + $span->finish(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * {@inheritdoc} |
| 141 | + * @alias mysqli_begin_transaction |
| 142 | + */ |
| 143 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName |
| 144 | + public function begin_transaction($flags = 0, $name = null) |
| 145 | + { |
| 146 | + $span = $this->tracer->nextSpan(); |
| 147 | + $span->setName('sql/begin_transaction'); |
| 148 | + $this->addsTagsAndRemoteEndpoint($span); |
| 149 | + $span->start(); |
| 150 | + if ($name !== null) { |
| 151 | + $span->tag('mysqli.transaction_name', (string) $name); |
| 152 | + } |
| 153 | + try { |
| 154 | + if ($name === null) { |
| 155 | + $result = parent::begin_transaction($flags); |
| 156 | + } else { |
| 157 | + $result = parent::begin_transaction($flags, $name); |
| 158 | + } |
| 159 | + |
| 160 | + if ($result === false) { |
| 161 | + $span->tag(ERROR, 'true'); |
| 162 | + } |
| 163 | + return $result; |
| 164 | + } finally { |
| 165 | + $span->finish(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @return bool |
| 171 | + * @alias mysqli_commit |
| 172 | + */ |
| 173 | + public function commit(int $flags = -1, ?string $name = null) |
| 174 | + { |
| 175 | + $span = $this->tracer->nextSpan(); |
| 176 | + $span->setName('sql/begin_transaction'); |
| 177 | + $this->addsTagsAndRemoteEndpoint($span); |
| 178 | + $span->start(); |
| 179 | + if ($name !== null) { |
| 180 | + $span->tag('mysqli.transaction_name', $name); |
| 181 | + } |
| 182 | + try { |
| 183 | + if ($name === null) { |
| 184 | + $result = parent::commit($flags); |
| 185 | + } else { |
| 186 | + $result = parent::commit($flags, $name); |
| 187 | + } |
| 188 | + |
| 189 | + if ($result === false) { |
| 190 | + $span->tag(ERROR, 'true'); |
| 191 | + } |
| 192 | + return $result; |
| 193 | + } finally { |
| 194 | + $span->finish(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * {@inheritdoc} |
| 200 | + * @alias mysqli_rollback |
| 201 | + */ |
| 202 | + public function rollback($flags = 0, $name = null) |
| 203 | + { |
| 204 | + $span = $this->tracer->nextSpan(); |
| 205 | + $span->setName('sql/rollback'); |
| 206 | + $this->addsTagsAndRemoteEndpoint($span); |
| 207 | + $span->start(); |
| 208 | + if ($name !== null) { |
| 209 | + $span->tag('mysqli.transaction_name', (string) $name); |
| 210 | + } |
| 211 | + try { |
| 212 | + if ($name === null) { |
| 213 | + $result = parent::commit($flags); |
| 214 | + } else { |
| 215 | + $result = parent::commit($flags, $name); |
| 216 | + } |
| 217 | + if ($result === false) { |
| 218 | + $span->tag(ERROR, 'true'); |
| 219 | + } |
| 220 | + return $result; |
| 221 | + } finally { |
| 222 | + $span->finish(); |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments