Skip to content

Commit

Permalink
Allow configuring observation registry directly
Browse files Browse the repository at this point in the history
This changes allows configuring the observation registry directly, instead of it being fetched from the application context.
This is to allow observability when `KafkaTemplate/KafkaMessageListenerContainer` are used without an application context.
  • Loading branch information
cfredri4 authored Dec 16, 2024
1 parent 019deb7 commit 3cd6c9c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
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

0 comments on commit 3cd6c9c

Please sign in to comment.