我正在尝试学习 Grafana 并使用 Spring Boot 2、Prometheus 和 Grafana 创建应用程序作为指标。我需要为每天的学生创建计数创建自定义指标。
import com.demo.mockito.entity.StudentEntity;
import com.demo.mockito.service.StudentService;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Autowired
private StudentRepository studentRepository;
@Timed
@GetMapping("studentEntity")
public ResponseEntity<StudentEntity> studentEntity() {
StudentEntity studentEntity = new StudentEntity();
studentEntity.setName("shri");
studentEntity.setSurName("sharma");
StudentEntity student = studentRepository.save(studentEntity); // <- need to create metrics of student.getRollNo()
return ResponseEntity.ok(student);
}
}
应用程序属性
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
# Enable prometheus exporter
management.metrics.export.prometheus.enabled=true
# Enable prometheus end point
management.endpoints.web.exposure.include=prometheus
# enable percentile-based histogram for http requests
management.metrics.distribution.percentiles-histogram.http.server.requests=true
# http SLA histogram buckets
management.metrics.distribution.sla.http.server.requests=100ms,150ms,250ms,500ms,1s
# enable JVM metrics
management.metrics.enable.jvm=true
spring.application.name=test app
management.metrics.tags.application=${spring.application.name}
spring.app.jpa.properties.hibernate.hibernateDDL=create
spring.app.jpa.properties.hibernate.hibernateShowSql=true
spring.jpa.generate-ddl=true
spring.jpa.database.schema=student_db
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
构建.gradle
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
主要应用:
package com.demo.mockito;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
@SpringBootApplication
public class SpringMockitoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMockitoApplication.class, args);
}
// custom application name
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
@Bean
TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
}
我需要为每天的学生创建计数创建自定义执行器端点指标,以便我可以使用 Grafana 绘制其图表。这rollNo
是一个自动生成的字段,因此它会给我一个学生总数的计数。
如何在 Spring Boot 中创建自定义指标?
提前致谢。