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

PDO auto instrumentation #1 #102

Merged
merged 9 commits into from
Feb 2, 2023

Conversation

pdelewski
Copy link
Member

Initial commit of PDO auto instrumentation

@pdelewski pdelewski requested a review from a team January 30, 2023 20:26
@codecov
Copy link

codecov bot commented Jan 31, 2023

Codecov Report

Merging #102 (5d1a298) into main (69e8c30) will not change coverage.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##               main     #102   +/-   ##
=========================================
  Coverage     64.42%   64.42%           
  Complexity      399      399           
=========================================
  Files            42       42           
  Lines          1380     1380           
=========================================
  Hits            889      889           
  Misses          491      491           
Flag Coverage Δ
7.4 81.76% <ø> (ø)
8.0 64.24% <ø> (ø)
8.1 64.32% <ø> (ø)
8.2 64.32% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 69e8c30...5d1a298. Read the comment docs.

@brettmc
Copy link
Collaborator

brettmc commented Feb 1, 2023

don't forget to add this to .gitsplit.yml. I can create (or show you how) a read-only repo for it to be pushed to. Then, we can create an entry in packagist for this.

@pdelewski
Copy link
Member Author

don't forget to add this to .gitsplit.yml. I can create (or show you how) a read-only repo for it to be pushed to. Then, we can create an entry in packagist for this.

ok, thank you

@pdelewski pdelewski changed the title PDO auto instrumentation WIP PDO auto instrumentation #1 Feb 1, 2023
@pdelewski pdelewski force-pushed the PDO-auto-instrumentation branch from 9ff0a27 to 29896a8 Compare February 1, 2023 15:49
"require": {
"php": "^8.0",
"ext-otel_instrumentation": "*",
"open-telemetry/api": "*",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"open-telemetry/api": "*",
"open-telemetry/api": "^1",

->setAttribute(TraceAttributes::DB_CONNECTION_STRING, $params[0] ?? 'unknown')
->setAttribute(TraceAttributes::DB_USER, $params[1] ?? 'unknown')
// parse connection string
->setAttribute(TraceAttributes::DB_SYSTEM, 'mysql');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be implemented to get real value from connection string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, right. I forgot about this gap.

'fetchAll',
pre: static function (\PDOStatement $statement, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) {
/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetch', $function, $class, $filename, $lineno)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetch', $function, $class, $filename, $lineno)
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetchAll', $function, $class, $filename, $lineno)

'execute',
pre: static function (\PDOStatement $statement, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) {
/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetch', $function, $class, $filename, $lineno)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetch', $function, $class, $filename, $lineno)
$builder = self::makeBuilder($instrumentation, 'PDOStatement::execute', $function, $class, $filename, $lineno)

@pdelewski pdelewski force-pushed the PDO-auto-instrumentation branch from 319dd6d to b3f89ff Compare February 2, 2023 07:07
"php": "^8.0",
"ext-otel_instrumentation": "*",
"open-telemetry/api": "^1",
"nyholm/psr7": "^1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think nyholm/* is not required for this instrumentation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, right. Will remove it.

Copy link
Collaborator

@brettmc brettmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, merge when you're ready. I've created https://github.com/opentelemetry-php/contrib-auto-pdo so the git-split has a destination...

@pdelewski
Copy link
Member Author

Approved, merge when you're ready. I've created https://github.com/opentelemetry-php/contrib-auto-pdo so the git-split has a destination...

ok, thank you

@pdelewski
Copy link
Member Author

@brettmc For dsn parsing I added additional dependency https://github.com/Nyholm/dsn. If you have something better or you think that we should implement this by ourselves just let me know (will do that in following PR, however for me it would be reinventing the wheel).

@pdelewski pdelewski merged commit c2adbfb into open-telemetry:main Feb 2, 2023
@pdelewski
Copy link
Member Author

pdelewski commented Feb 2, 2023

@brettmc For dsn parsing I added additional dependency https://github.com/Nyholm/dsn. If you have something better or you think that we should implement this by ourselves just let me know (will do that in following PR, however for me it would be reinventing the wheel).

Hmm, I see that sdk contains dsn parser, however it seems that it's not general enough and does not fulfil PDO requirements as I'm getting InvalidArgumentException: An exporter DSN must have a exporter type and a scheme: type+scheme://host:port

->setSpanKind(SpanKind::KIND_CLIENT)
->setAttribute(TraceAttributes::DB_CONNECTION_STRING, $params[0] ?? 'unknown')
->setAttribute(TraceAttributes::DB_USER, $params[1] ?? 'unknown')
->setAttribute(TraceAttributes::DB_SYSTEM, self::getDB($params[0]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use $pdo->getAttribute(PDO::ATTR_DRIVER_NAME) to set db.system in the post hook.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDO::__construct', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT)
->setAttribute(TraceAttributes::DB_CONNECTION_STRING, $params[0] ?? 'unknown')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check that $class === PDO::class, subclasses might declare different parameters.


hook(
\PDOStatement::class,
'fetch',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we shouldn't create a span per ::fetch() etc.; large result sets will lead to a lot of spans.

Copy link
Member Author

@pdelewski pdelewski Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it and plan to change this behaviour in subsequent PR(s). BTW I was thinking about kind of grouping fetch operations mechanism, however don't know yet if that will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants