Skip to content

Commit

Permalink
Remove the static configuration of Micrometer's observation registry
Browse files Browse the repository at this point in the history
Resolves #4222
  • Loading branch information
fmbenhassine committed Nov 8, 2022
1 parent 051e022 commit d889f84
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.configuration.annotation;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
* Bean post processor that configures observable batch artifacts (jobs and steps) with
* Micrometer's observation registry.
*
* @author Mahmoud Ben Hassine
* @since 5.0
*/
public class BatchObservabilityBeanPostProcessor implements BeanFactoryPostProcessor, BeanPostProcessor {

private static final Log LOGGER = LogFactory.getLog(BatchObservabilityBeanPostProcessor.class);

private ConfigurableListableBeanFactory beanFactory;

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
try {
if (bean instanceof AbstractJob || bean instanceof AbstractStep) {
ObservationRegistry observationRegistry = this.beanFactory.getBean(ObservationRegistry.class);
if (bean instanceof AbstractJob) {
((AbstractJob) bean).setObservationRegistry(observationRegistry);
}
if (bean instanceof AbstractStep) {
((AbstractStep) bean).setObservationRegistry(observationRegistry);
}
}
}
catch (NoSuchBeanDefinitionException e) {
LOGGER.info("No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP");
}
return bean;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ BatchRegistrar.class, ScopeConfiguration.class, AutomaticJobRegistrarBeanPostProcessor.class })
@Import({ BatchRegistrar.class, ScopeConfiguration.class, AutomaticJobRegistrarBeanPostProcessor.class,
BatchObservabilityBeanPostProcessor.class })
public @interface EnableBatchProcessing {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.Tag;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -87,6 +88,8 @@ public abstract class AbstractJob implements Job, StepLocator, BeanNameAware, In

private StepHandler stepHandler;

private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;

private BatchJobObservationConvention observationConvention = new DefaultBatchJobObservationConvention();

/**
Expand Down Expand Up @@ -289,7 +292,8 @@ public final void execute(JobExecution execution) {
BatchMetrics.METRICS_PREFIX + activeJobMeterName + ".name", execution.getJobInstance().getJobName()));
LongTaskTimer.Sample longTaskTimerSample = longTaskTimer.start();
Observation observation = BatchMetrics
.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName(), new BatchJobContext(execution))
.createObservation(BatchJobObservation.BATCH_JOB_OBSERVATION.getName(), new BatchJobContext(execution),
this.observationRegistry)
.contextualName(execution.getJobInstance().getJobName())
.observationConvention(this.observationConvention).start();
try (Observation.Scope scope = observation.openScope()) {
Expand Down Expand Up @@ -439,6 +443,10 @@ public void setObservationConvention(BatchJobObservationConvention observationCo
this.observationConvention = observationConvention;
}

public void setObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}

@Override
public String toString() {
return ClassUtils.getShortName(getClass()) + ": [name=" + name + "]";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,10 @@
import java.util.List;
import java.util.Set;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.JobParametersIncrementer;
Expand Down Expand Up @@ -99,6 +101,18 @@ public B repository(JobRepository jobRepository) {
return result;
}

/**
* Sets the observation registry for the job.
* @param observationRegistry the observation registry (optional)
* @return this to enable fluent chaining
*/
public B observationRegistry(ObservationRegistry observationRegistry) {
properties.observationRegistry = observationRegistry;
@SuppressWarnings("unchecked")
B result = (B) this;
return result;
}

/**
* Registers objects using the annotation based listener configuration.
* @param listener the object that has a method configured with listener annotation
Expand Down Expand Up @@ -170,6 +184,10 @@ protected void enhance(Job target) {
if (jobParametersValidator != null) {
job.setJobParametersValidator(jobParametersValidator);
}
ObservationRegistry observationRegistry = properties.getObservationRegistry();
if (observationRegistry != null) {
job.setObservationRegistry(observationRegistry);
}

Boolean restartable = properties.getRestartable();
if (restartable != null) {
Expand All @@ -193,6 +211,8 @@ public static class CommonJobProperties {

private JobRepository jobRepository;

private ObservationRegistry observationRegistry;

private JobParametersIncrementer jobParametersIncrementer;

private JobParametersValidator jobParametersValidator;
Expand All @@ -204,6 +224,7 @@ public CommonJobProperties(CommonJobProperties properties) {
this.name = properties.name;
this.restartable = properties.restartable;
this.jobRepository = properties.jobRepository;
this.observationRegistry = properties.observationRegistry;
this.jobExecutionListeners = new LinkedHashSet<>(properties.jobExecutionListeners);
this.jobParametersIncrementer = properties.jobParametersIncrementer;
this.jobParametersValidator = properties.jobParametersValidator;
Expand Down Expand Up @@ -233,6 +254,14 @@ public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}

public ObservationRegistry getObservationRegistry() {
return observationRegistry;
}

public void setObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ public final class BatchMetrics {

public static final String STATUS_FAILURE = "FAILURE";

/**
* Global {@link ObservationRegistry}. A {@link DefaultMeterObservationHandler} is
* attached to create a {@link Timer} for every finished {@link Observation}.
*/
public static ObservationRegistry observationRegistry;

static {
observationRegistry = ObservationRegistry.create();
observationRegistry.observationConfig()
.observationHandler(new DefaultMeterObservationHandler(Metrics.globalRegistry));
}

private BatchMetrics() {
}

Expand Down Expand Up @@ -94,7 +82,8 @@ public static Timer createTimer(String name, String description, Tag... tags) {
* @return a new observation instance
* @since 5.0
*/
public static Observation createObservation(String name, BatchJobContext context) {
public static Observation createObservation(String name, BatchJobContext context,
ObservationRegistry observationRegistry) {
return Observation.createNotStarted(name, context, observationRegistry);
}

Expand All @@ -110,7 +99,8 @@ public static Observation createObservation(String name, BatchJobContext context
* @return a new observation instance
* @since 5.0
*/
public static Observation createObservation(String name, BatchStepContext context) {
public static Observation createObservation(String name, BatchStepContext context,
ObservationRegistry observationRegistry) {
return Observation.createNotStarted(name, context, observationRegistry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.stream.Collectors;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -75,6 +76,8 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw

private JobRepository jobRepository;

private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;

private BatchStepObservationConvention observationConvention = new DefaultBatchStepObservationConvention();

/**
Expand Down Expand Up @@ -203,7 +206,7 @@ public final void execute(StepExecution stepExecution)
stepExecution.setStatus(BatchStatus.STARTED);
Observation observation = BatchMetrics
.createObservation(BatchStepObservation.BATCH_STEP_OBSERVATION.getName(),
new BatchStepContext(stepExecution))
new BatchStepContext(stepExecution), this.observationRegistry)
.contextualName(stepExecution.getStepName()).observationConvention(this.observationConvention).start();
getJobRepository().update(stepExecution);

Expand Down Expand Up @@ -424,4 +427,8 @@ public void setObservationConvention(BatchStepObservationConvention observationC
this.observationConvention = observationConvention;
}

public void setObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
*/
package org.springframework.batch.core.step.builder;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.listener.StepListenerFactoryBean;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.support.ReflectionUtils;
import org.springframework.transaction.PlatformTransactionManager;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* A base class and utility for other step builders providing access to common properties
Expand Down Expand Up @@ -68,6 +68,11 @@ public B repository(JobRepository jobRepository) {
return self();
}

public B observationRegistry(ObservationRegistry observationRegistry) {
properties.observationRegistry = observationRegistry;
return self();
}

public B startLimit(int startLimit) {
properties.startLimit = startLimit;
return self();
Expand Down Expand Up @@ -123,6 +128,11 @@ protected void enhance(Step target) {
AbstractStep step = (AbstractStep) target;
step.setJobRepository(properties.getJobRepository());

ObservationRegistry observationRegistry = properties.getObservationRegistry();
if (observationRegistry != null) {
step.setObservationRegistry(observationRegistry);
}

Boolean allowStartIfComplete = properties.allowStartIfComplete;
if (allowStartIfComplete != null) {
step.setAllowStartIfComplete(allowStartIfComplete);
Expand All @@ -149,6 +159,8 @@ public static class CommonStepProperties {

private JobRepository jobRepository;

private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;

public CommonStepProperties() {
}

Expand All @@ -157,6 +169,7 @@ public CommonStepProperties(CommonStepProperties properties) {
this.startLimit = properties.startLimit;
this.allowStartIfComplete = properties.allowStartIfComplete;
this.jobRepository = properties.jobRepository;
this.observationRegistry = properties.observationRegistry;
this.stepExecutionListeners = new ArrayList<>(properties.stepExecutionListeners);
}

Expand All @@ -168,6 +181,14 @@ public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}

public ObservationRegistry getObservationRegistry() {
return observationRegistry;
}

public void setObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.batch.core.step.factory;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.ChunkListener;
Expand Down Expand Up @@ -84,6 +85,8 @@ public class SimpleStepFactoryBean<T, S> implements FactoryBean<Step>, BeanNameA

protected JobRepository jobRepository;

protected ObservationRegistry observationRegistry = ObservationRegistry.NOOP;

private boolean singleton = true;

private ItemStream[] streams = new ItemStream[0];
Expand Down Expand Up @@ -270,6 +273,15 @@ public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}

/**
* Public setter for {@link ObservationRegistry}.
* @param observationRegistry is an optional dependency (defaults to
* {@link ObservationRegistry#NOOP}).
*/
public void setObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}

/**
* Public setter for the {@link PlatformTransactionManager}.
* @param transactionManager the transaction manager to set
Expand Down Expand Up @@ -469,6 +481,7 @@ protected void applyConfiguration(SimpleStepBuilder<T, S> builder) {
builder.transactionManager(transactionManager);
builder.transactionAttribute(getTransactionAttribute());
builder.repository(jobRepository);
builder.observationRegistry(observationRegistry);
builder.startLimit(startLimit);
builder.allowStartIfComplete(allowStartIfComplete);
builder.chunk(commitInterval);
Expand Down

0 comments on commit d889f84

Please sign in to comment.