Skip to content

[JOOQ] MetricsDSLContext - fetchExists doesn't report provided tags #6583

@eye-dee

Description

@eye-dee

Describe the bug
When using dsl.tag("tag", "value").fetchExists(SOME_TABLE, SOME_CONDITION);, the expected metric is not reported. The issue arises because the tag value is not being passed correctly to the query listener. Internally, the following code is executed:

fetchExists(selectOne().from(table).where(condition));

This causes query tags to be removed from the thread-local context prematurely. Specifically, in MetricsDSLContext, the time() method clears the tags before they're available to the JooqExecuteListener:

public Configuration time(Configuration c) {
    Iterable<Tag> queryTags = contextTags.get();
    contextTags.remove();
    return derive(c, () -> new JooqExecuteListener(registry, tags, () -> queryTags));
}

As a result, no tags are attached to the query metrics when fetchExists is used.


Environment

  • Micrometer version: 1.15.2
  • Micrometer registry: prometheus
  • OS: macOS
  • Java version: OpenJDK 21

To Reproduce
Use the following code snippet:

dsl.tag("tag", "value")
   .fetchExists(SOME_TABLE, SOME_CONDITION);

Observe that no query metric with the tag "tag" = "value" is reported.


Expected behavior
The tag provided via dsl.tag(...) should be passed to the query listener, and metrics should be reported with the specified tag.


Additional context
It appears that fetchExists internally rewrites the query and performs a new one via selectOne().from(...), causing the tags to be cleared before metric recording. This issue may also affect other similar high-level shortcut methods if they internally trigger a new execution context.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions