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

Allow configuring observation registry directly #3643

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
* @author Soby Chacko
* @author Gurps Bassi
* @author Valentina Armenise
* @author Christian Fredriksson
*/
public class KafkaTemplate<K, V> implements KafkaOperations<K, V>, ApplicationContextAware, BeanNameAware,
ApplicationListener<ContextStoppedEvent>, DisposableBean, SmartInitializingSingleton {
Expand Down Expand Up @@ -456,6 +457,16 @@ public void setObservationConvention(KafkaTemplateObservationConvention observat
this.observationConvention = observationConvention;
}

/**
* Configure the {@link ObservationRegistry} to use for recording observations.
* @param observationRegistry the observation registry to use.
* @since 3.3.1
*/
public void setObservationRegistry(ObservationRegistry observationRegistry) {
Assert.notNull(observationRegistry, "'observationRegistry' must not be null");
this.observationRegistry = observationRegistry;
}

/**
* Return the {@link KafkaAdmin}, used to find the cluster id for observation, if
* present.
Expand All @@ -479,8 +490,10 @@ public void setKafkaAdmin(KafkaAdmin kafkaAdmin) {
@Override
public void afterSingletonsInstantiated() {
if (this.observationEnabled && this.applicationContext != null) {
this.observationRegistry = this.applicationContext.getBeanProvider(ObservationRegistry.class)
.getIfUnique(() -> this.observationRegistry);
if (this.observationRegistry.isNoop()) {
this.observationRegistry = this.applicationContext.getBeanProvider(ObservationRegistry.class)
.getIfUnique(() -> this.observationRegistry);
}
if (this.kafkaAdmin == null) {
this.kafkaAdmin = this.applicationContext.getBeanProvider(KafkaAdmin.class).getIfUnique();
if (this.kafkaAdmin != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.function.Function;
import java.util.regex.Pattern;

import io.micrometer.observation.ObservationRegistry;
import org.aopalliance.aop.Advice;
import org.apache.kafka.clients.consumer.ConsumerRecord;

Expand Down Expand Up @@ -281,6 +282,8 @@ public enum EOSMode {

private boolean observationEnabled;

private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;

private Duration consumerStartTimeout = DEFAULT_CONSUMER_START_TIMEOUT;

private Boolean subBatchPerPartition;
Expand Down Expand Up @@ -716,6 +719,20 @@ public void setObservationEnabled(boolean observationEnabled) {
this.observationEnabled = observationEnabled;
}

public ObservationRegistry getObservationRegistry() {
return this.observationRegistry;
}

/**
* Configure the {@link ObservationRegistry} to use for recording observations.
* @param observationRegistry the observation registry to use.
* @since 3.3.1
*/
public void setObservationRegistry(ObservationRegistry observationRegistry) {
Assert.notNull(observationRegistry, "'observationRegistry' must not be null");
this.observationRegistry = observationRegistry;
}

/**
* Set additional tags for the Micrometer listener timers.
* @param tags the tags.
Expand Down Expand Up @@ -1118,6 +1135,9 @@ public String toString() {
+ (this.observationConvention != null
? "\n observationConvention=" + this.observationConvention
: "")
+ (this.observationRegistry != null
? "\n observationRegistry=" + this.observationRegistry
: "")
+ "\n restartAfterAuthExceptions=" + this.restartAfterAuthExceptions
+ "\n]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.log.LogAccessor;
Expand Down Expand Up @@ -171,6 +170,7 @@
* @author Borahm Lee
* @author Lokesh Alamuri
* @author Sanghyeok An
* @author Christian Fredriksson
*/
public class KafkaMessageListenerContainer<K, V> // NOSONAR line count
extends AbstractMessageListenerContainer<K, V> implements ConsumerPauseResumeEventPublisher {
Expand Down Expand Up @@ -372,14 +372,15 @@ protected void doStart() {
}
GenericMessageListener<?> listener = (GenericMessageListener<?>) messageListener;
ListenerType listenerType = determineListenerType(listener);
ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
ApplicationContext applicationContext = getApplicationContext();
if (applicationContext != null && containerProperties.isObservationEnabled()) {
ObjectProvider<ObservationRegistry> registry =
applicationContext.getBeanProvider(ObservationRegistry.class);
ObservationRegistry reg = registry.getIfUnique();
if (reg != null) {
observationRegistry = reg;
ObservationRegistry observationRegistry = containerProperties.getObservationRegistry();
if (observationRegistry.isNoop()) {
ApplicationContext applicationContext = getApplicationContext();
if (applicationContext != null && containerProperties.isObservationEnabled()) {
ObservationRegistry reg = applicationContext.getBeanProvider(ObservationRegistry.class)
.getIfUnique();
if (reg != null) {
observationRegistry = reg;
}
}
}
this.listenerConsumer = new ListenerConsumer(listener, listenerType, observationRegistry);
Expand Down
Loading