elastic / elastic/apm-agent-java

Improve database connection metadata capture

Open
#2,650 0 comments 0 reactions 0 assignees View on GitHub
agent-java enhancement
Dominant language
Java
Stars
594
Forks
338
Avg merge
1d 13h
Merged PRs (30d)
25

Description

This is a follow-up issue after implementing #2642 which fixes #1841 .

For SQL spans that are captured through JDBC instrumentation, the agent currently captures database metadata when the SQL statement is executed.

As getting connection metadata might involve a few extra calls to the DB server (for example with calls to `Connection.getCatalog()` or `Connection.getMetadata().getUserName()`, we avoid most of the overhead by caching the returned value on a per-connection basis.

It means that the captured database metadata (which includes the current database name) will actually depend on the state of the connection when the statement was executed.

As of writing this the implementation in #2642 only uses the value of `getCatalog` as a fallback when unable to parse the database name from JDBC URL, but could become problematic in the following scenario:
- connection with a JDBC url that does not contain the database name, for example with Oracle DB driver using `tnsnames.ora` at runtime, which is not parsed by the Java agent. In this case the agent would try to call `Connection.getCatalog()` to get the database name
- the first statement that is executed on the database is on database `DB1`, the database metadata would have `instance` = `DB1`.
- there is a call to `Connection.setCatalog("DB2")`
- a second statement is executed on the connection, this second statement would be reported to have been executed on `DB1` database whereas in practice it was on `DB2`.

In order to mitigate/fix this, the following approaches could be implemented:
- capture the database name when the connection is created from the JDBC string, before any call to `setCatalog` could be made by the application, this would at least allow to capture the original state of the connection.
- instrument the connection state and update the metadata accordingly

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.