Class DatabaseActivity

java.lang.Object
com.vaadin.observability.micrometer.DatabaseActivity

public final class DatabaseActivity extends Object
A running tally, per thread, of the JDBC work done on it: queries executed and result-set rows read.

It exists so that instrumentation which only sees the Vaadin side of a data load — the data provider events, which say a component asked for 150 items and got 150 back — can also report what that load cost underneath. Those two numbers are the same whether the page came out of one indexed query or out of sixty thousand, and the difference between those two is the whole finding.

The tally is monotonic and never reset. A caller measures a window by taking current() at its start and since(Work) at its end, so windows can nest without one clearing another's baseline.

Threading. The counters are thread-confined, which is what makes them cheap — a plain increment, no contention. It also means a window only counts the queries issued on the thread that opened it: a data provider that hands its fetch to another thread would see none of them. That case is reported as unknown rather than as zero, since the two are indistinguishable here and a confident "no database work" would be the more damaging of the two answers.

The counters cost two longs on each thread that touches JDBC and are never removed, which is deliberate: they belong to pooled request threads that outlive any single query, and a remove() on a window boundary would throw away the baseline of an enclosing window.

  • Method Details

    • instrumented

      public static void instrumented()
      Declares that JDBC queries are being counted. Called by whichever instrumentation wraps the application's data sources.
    • isInstrumented

      public static boolean isInstrumented()
      Returns:
      whether JDBC queries are being counted at all
    • queryExecuted

      public static void queryExecuted()
      Counts one executed query on the current thread.
    • rowsRead

      public static void rowsRead(long rows)
      Counts rows read from one result set on the current thread.
      Parameters:
      rows - the number of rows read, ignored when negative
    • current

      public static DatabaseActivity.Work current()
      Opens a measurement window.
      Returns:
      the current thread's running tally, to be handed back to since(Work), or DatabaseActivity.Work.UNKNOWN when nothing is being counted
    • since

      public static DatabaseActivity.Work since(DatabaseActivity.Work start)
      Closes a measurement window opened by current().
      Parameters:
      start - the tally taken when the window opened, may be null
      Returns:
      the work done on this thread since then, or DatabaseActivity.Work.UNKNOWN when there is nothing to report — no instrumentation, no baseline, or no query counted, the last of which also covers a window whose queries ran on another thread