Skip to content

Shorten jdbc.connection observation scope to prevent leakage with OIV #82

@ttddyy

Description

@ttddyy

Description:

The observation scope for "jdbc.connection" is opened when a DB connection is acquired and closes when the DB connection is closed.
When Open In View (OIV) is enabled, since it delays the DB connection close until after the view is rendered, it causes the "jdbc.connection" observation/scope to leak outside of the intended observation boundary.

Example

@GetMapping("/")
List<Car> cars() {
  // (1)
  return Observation.createNotStarted("my.observation", registry).observe(() -> {
    // (2)
    List<Car> cars = repository.findAll();  // DB access
    // (3)
    return cars;
  });
  // (4)
}

OIV disabled

At point (1), the active scope is "http.server.requests".
At point (2), the active scope is "my.observation".
At point (3), the active scope is "my.observation".
At point (4), the active scope should be "http.server.requests".

Event Type Name
START Observation http.server.requests
OPEN Scope http.server.requests
START Observation my.observation
OPEN Scope my.observation
START Observation jdbc.connection
OPEN Scope jdbc.connection
… acquire connection …
START Observation jdbc.query
OPEN Scope jdbc.query
… perform query …
CLOSE Scope jdbc.query
STOP Observation jdbc.query
… interact result set, etc …
CLOSE Scope jdbc.connection
STOP Observation jdbc.connection
CLOSE Scope my.observation
STOP Observation my.observation
CLOSE Scope http.server.requests
STOP Observation http.server.requests

OIV enabled

At point (1), the active scope is "http.server.requests".
At point (2), the active scope is "my.observation".
At point (3), the active scope is "jdbc.connection". <==
At point (4), the active scope is "jdbc.connection". <==

Event Type Name
START Observation http.server.requests
OPEN Scope http.server.requests
START Observation my.observation
OPEN Scope my.observation
START Observation jdbc.connection
OPEN Scope jdbc.connection
… acquire connection …
START Observation jdbc.query
OPEN Scope jdbc.query
… perform query …
CLOSE Scope jdbc.query
STOP Observation jdbc.query
… interact result set, etc …
CLOSE Scope my.observation <== wrong order
STOP Observation my.observation <== wrong order
CLOSE Scope jdbc.connection <== wrong order
STOP Observation jdbc.connection <== wrong order
CLOSE Scope http.server.requests
STOP Observation http.server.requests

Proposing Change

There is no good reason to keep the jdbc.connection scope open for the entire lifetime of the connection.
Closing the scope right after acquisition ensures observations do not leak into application logic and keeps scope boundaries consistent.

Event Type Name
START Observation http.server.requests
OPEN Scope http.server.requests
START Observation my.observation
OPEN Scope my.observation
START Observation jdbc.connection
OPEN Scope jdbc.connection
… acquire connection …
CLOSE Scope jdbc.connection <== scope closed immediately
START Observation jdbc.query
OPEN Scope jdbc.query
… perform query …
CLOSE Scope jdbc.query
STOP Observation jdbc.query
… interact result set, etc …
STOP Observation jdbc.connection
CLOSE Scope my.observation
STOP Observation my.observation
CLOSE Scope http.server.requests
STOP Observation http.server.requests

To keep the observation hierarchy, pass the jdbc.connection observation as the parent to the child observations(query, result-set, generated-keys).

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions