Skip to content
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

Optional custom scheduler for KafkaMetrics #4977

Conversation

vasiliy-sarzhynskyi
Copy link
Contributor

@vasiliy-sarzhynskyi vasiliy-sarzhynskyi commented Apr 20, 2024

Resolves gh-4976

@vasiliy-sarzhynskyi
Copy link
Contributor Author

It's a proposed contribution for enhancement #4976 to extend KafkaMetrics with custom scheduler

@shakuzen shakuzen changed the title Fixes #4976 - added optional custom scheduler to Consumer KafkaClientMetrics Optional custom scheduler for Consumer KafkaClientMetrics Oct 8, 2024
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.

Thank you for the pull request. This looks reasonable to me. Before merging, I'm trying to get an idea of the impact to other projects so this can be most easily used by end users. What would change in Spring Kafka and Spring Boot? I guess MicrometerConsumerListener in Spring Kafka would need to optionally take a ScheduledExecutorService and KafkaMetricsAutoConfiguration in Spring Boot would need a way to get a ScheduledExecutorService from user configuration (how would it do this?) to pass to the MicrometerConsumerListener it makes.

@artembilan do you have any concerns about this from a Spring Kafka perspective?

* @param tags additional tags
* @param scheduler scheduler to check and bind metrics
*/
public KafkaClientMetrics(Consumer<?, ?> kafkaConsumer, Iterable<Tag> tags, ScheduledExecutorService scheduler) {

Choose a reason for hiding this comment

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

This is fine with me since it does not bring any breaking changes.
In Spring we have a ThreadPoolTaskScheduler (which is auto-configured by Spring Boot) and that one comes with a property:

	/**
	 * Return the underlying ScheduledExecutorService for native access.
	 * @return the underlying ScheduledExecutorService (never {@code null})
	 * @throws IllegalStateException if the ThreadPoolTaskScheduler hasn't been initialized yet
	 */
	public ScheduledExecutorService getScheduledExecutor() throws IllegalStateException {

I'll be totally fine exposing that property on the MicrometerConsumerListener and MicrometerProducerListener.
However you are not done with the fixed here yet.
Pay attention to those ctors of this class which relies on the Producer and AdminClient, respectively.
Thanks

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the review @artembilan.
@vasiliy-sarzhynskyi any reason to not add similar constructors for producers and admin clients?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shakuzen I need to check how producers and admin clients are used by Spring-Kafka, and how many threads we might have with multiple produced topics (whether the number of threads increased or not). We could add there as well, but do we have use cases when producers and admin clients might need that? In my understanding, it's especially relevant for consumers as we might specify concurrency config value, and a single app instance will have hundreds of threads.

Choose a reason for hiding this comment

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

I’m not sure how is this relevant to the use of those clients in the app, the scheduler we provide here is used for publishing metrics from those clients. See bindTo() impl.

Copy link
Member

Choose a reason for hiding this comment

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

This scheduler isn't for publishing; MeterRegistry implementations can be configured with a scheduler to publish metrics. This scheduler is for syncing the metrics from the Kafka client to Micrometer's registry periodically. The background is in #4976, but with many clients and the existing code, each client will have a thread for syncing between the Kafka client's metrics and Micrometer. With the changes here, a thread pool could be shared among the clients for this syncing.

Choose a reason for hiding this comment

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

Thank you, @shakuzen , for clarification!
So, sounds like I was right anyway: this scheduler has nothing to do with Spring Kafka and has same effect for all the clients.
Therefore similar injection approach has to be applied for Producer and Admin.
Otherwise it is strange that we can customize a scheduler for Consumer client metrics synchronization, but that is not the case for Producer and Admin.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. In that case, I will update the current PR for Producer and Admin as well

Copy link
Contributor Author

@vasiliy-sarzhynskyi vasiliy-sarzhynskyi Oct 11, 2024

Choose a reason for hiding this comment

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

@shakuzen I pushed my changes for Producer and AdminClient constructors as well. After I rebased on branch main, recently added test class VirtualThreadMetricsTests is failing, looks like it's flaky.
I could try to increase the upper bound threshold inside VirtualThreadMetricsTests pinnedEventsShouldBeRecorded(..), but would be better to contact owner of these changes to clarify the intention of asserted values (changes by Virtual threads metrics)

Copy link
Member

Choose a reason for hiding this comment

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

Pinging @jonatan-ivanov about the flakiness of VirtualThreadMetricsTests. I'll re-run the CI manually in the meantime.

@vasiliy-sarzhynskyi vasiliy-sarzhynskyi force-pushed the gh-4976-kafka-metrics-with-custom-scheduler branch from cdff25d to ad1d299 Compare October 11, 2024 14:22
…oducer and AdminClient KafkaClientMetrics
@vasiliy-sarzhynskyi vasiliy-sarzhynskyi force-pushed the gh-4976-kafka-metrics-with-custom-scheduler branch from ad1d299 to f9dae77 Compare October 11, 2024 15:57
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.

Thanks for the pull request and quick response to review. This looks good to me. I'll polish a couple things when merging.

@shakuzen
Copy link
Member

I've added the same changes for KafkaStreamsMetrics and updated the JavaDocs to specifically mention the purpose and responsibilities for the custom scheduler.

@shakuzen shakuzen changed the title Optional custom scheduler for Consumer KafkaClientMetrics Optional custom scheduler for KafkaMetrics Oct 15, 2024
@shakuzen shakuzen merged commit b9d4107 into micrometer-metrics:main Oct 15, 2024
7 checks passed
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.

Allow user-provided custom scheduler for periodically binding KafkaMetrics
3 participants