kagamihogeの日記

kagamihogeの日記です。

Spring Batch 4.2でメトリクスをPrometheus Pushgatewayにおくる

https://docs.spring.io/spring-batch/docs/current/reference/html/monitoring-and-metrics.html を試す。

Spring Batch 4.2はMicrometerベースのメトリクスを自動的に収集する。なので、プロパティでそのメトリクスの送信設定をすれば、データがそちらに送られる。

ここではPrometheus Pushgatewayにメトリクスを送信する。

Prometheus Pushgatewayの準備

dockerで起動する。

docker run -d -p 9091:9091 prom/pushgateway

起動するとhttp://localhost:9091/#でpushgatewayの画面が見れる。

コード

build.gradle。spring-batchに加えて、actuator, prometheus, pushgatewayクライアントの依存性を入れる。

plugins {
  id 'org.springframework.boot' version '2.3.0.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}

configurations {
  developmentOnly
  runtimeClasspath {
    extendsFrom developmentOnly
  }
  compileOnly {
    extendsFrom annotationProcessor
  }
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-batch'
  implementation 'org.springframework.boot:spring-boot-starter-actuator'
  implementation 'io.micrometer:micrometer-registry-prometheus'
  implementation 'io.prometheus:simpleclient_pushgateway'

  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testImplementation 'org.springframework.batch:spring-batch-test'
  implementation 'com.h2database:h2'
}

test {
  useJUnitPlatform()
}

src/main/resources/application.propertiesでpushgateway関連の設定をする。とりあえず動いた、というレベルなので設定の詳細までは良く調べていない。

management.metrics.export.prometheus.pushgateway.enabled=true
management.metrics.export.prometheus.pushgateway.grouping-key.app_name=demo-batch-hoge
management.metrics.export.prometheus.pushgateway.job=demo-batch
management.metrics.export.prometheus.pushgateway.shutdown-operation=push

適当にspring-batchのアプリを作る。

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
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.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@EnableBatchProcessing
@SpringBootApplication
public class Main {

  @Bean
  public Job job(JobBuilderFactory jobs, @Qualifier("myjobstep1") Step s1) {
    return jobs.get("demo-batch-job").incrementer(new RunIdIncrementer()).start(s1).build();
  }

  @Bean(name = "myjobstep1")
  public Step step1(StepBuilderFactory steps) {
    return steps.get("myjobstep1").tasklet((contribution, chunkContext) -> {
      System.out.println("hoge");
      return RepeatStatus.FINISHED;
    }).build();
  }

  public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
  }
}

実行後にhttp://localhost:9091/#を見ると以下のようなメトリクスが確認できる。

f:id:kagamihoge:20200528161608j:plain

参考文献