Skip to content

Add support for per-query metrics in Hibernate statistics(#1006) #1461

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

Merged
merged 7 commits into from
Mar 19, 2020

Conversation

pawel2401
Copy link
Contributor

@pawel2401 pawel2401 commented Jun 18, 2019

Resolves #1006

@pawel2401
Copy link
Contributor Author

@shakuzen are you fine with the changes? Should I change smth else?

@pawel2401 pawel2401 requested a review from shakuzen October 14, 2019 20:14
@pawel2401
Copy link
Contributor Author

@shakuzen Anything else do you see that would be good to change?

QueryStatistics queryStatistics = statistics.getQueryStatistics(query);
if (Objects.nonNull(queryStatistics)) {
String queryName = meterRegistry.config().namingConvention().tagValue(query);
if (Objects.isNull(Search.in(meterRegistry).tags("query", queryName).functionCounter())) {
Copy link
Member

Choose a reason for hiding this comment

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

Is the point of this check because there could be multiple of the same query returned by statistics.getQueries()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emm If I remember correctly there should not, it is a little bit redundant check

Copy link
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

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

Sorry for the long delay on getting this merged, and thank you for making the changes requested during review. Great work.


/**
* A {@link MeterBinder} implementation that provides Hibernate query metrics. It exposes the
* same statistics as would be exposed when calling {@link Statistics#getQueryStatistics(String)}.
Copy link
Member

Choose a reason for hiding this comment

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

We should have a warning about potential high cardinality depending on the variety of queries one's application executes. I can add this as a polish commit post merge.


/**
* Register hibernate loadEvent listener - event trigger the metrics registration,
* only select queries are recorded in QueryStatistics {@link org.hibernate.stat.spi.StatisticsImplementor}
Copy link
Member

Choose a reason for hiding this comment

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

If we think it would be helpful to users to have this information, we should add it to one of the public methods or the class JavaDoc. On a private method, it will not be noticed by most users.

}

private HibernateQueryMetrics.MetricsEventHandler createMetricsEventHandlerMock() {
HibernateQueryMetrics hibernateQueryMetricsMock = Mockito.mock(HibernateQueryMetrics.class);
Copy link
Member

Choose a reason for hiding this comment

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

It feels like we're mocking too much when we are even mocking the class under test. I will try to rework the tests a bit to get something that feels closer to real usage. We could even make tests that use an in-memory database and normal Hibernate with that. This can be done post-merge, though.

Copy link
Member

Choose a reason for hiding this comment

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

After looking at this more closely, I see we're essentially testing the MetricsEventHandler in isolation. That's fine for this. I'll open an issue to add tests with not-mocked Hibernate classes.

@shakuzen shakuzen merged commit edf2b99 into micrometer-metrics:master Mar 19, 2020
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.

Add support for per-query metrics in Hibernate statistics
3 participants