spring-cloud / spring-cloud-dataflow-samples

Sample starter applications and code for use with the Spring Cloud Data Flow project
http://cloud.spring.io/spring-cloud-dataflow/
220 stars 203 forks source link

Prometheus Metrics #142

Closed ilmarcoronchi closed 3 years ago

ilmarcoronchi commented 3 years ago

Moving Job Definition from spring-cloud-dataflow-samples/monitoring-samples/task-apps/task-demo-metrics-prometheus/src/main/java/com/example/task/taskdemometrics/TaskDemoMetricsApplication.java to a class annotated with @Configuration (see below code) doesn't send metrics.

import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FooJobDefinition {

  private static final Logger LOGGER = LoggerFactory.getLogger(FooJobDefinition.class);

  private Random random = new Random();

  @Autowired
  public JobBuilderFactory jobBuilderFactory;

  @Autowired
  public StepBuilderFactory stepBuilderFactory;

  @Bean
  public Job job1() {
    return this.jobBuilderFactory.get("job1")
        .start(step1())
        .next(step2())
        .incrementer(new RunIdIncrementer())
        .build();
  }

  @Bean
  public Step step1() {
    return this.stepBuilderFactory.get("step1")
        .<Integer, Integer>chunk(10)
        .reader(new ListItemReader<>(IntStream.rangeClosed(0, this.random.nextInt(100))
            .boxed().collect(Collectors.toList())))
        .writer(list -> list.forEach(e -> {
          LOGGER.info(">>>>>>>>{}<<<<<<<<",e);
        })).build();
  }

  @Bean
  public Step step2() {
    return this.stepBuilderFactory.get("step2")
        .tasklet((contribution, context) -> {
          Thread.sleep(this.random.nextInt(10000));
          return RepeatStatus.FINISHED;
        }).build();
  }
}
ilmarcoronchi commented 3 years ago

Configuration Mistake

sabbyanandan commented 3 years ago

Glad you got it working, @ilmarcoronchi!