【问题标题】:How to create a custom metrics end point for Grafana using Spring Boot 2?如何使用 Spring Boot 2 为 Grafana 创建自定义指标端点?
【发布时间】:2020-10-02 03:40:56
【问题描述】:

我正在尝试学习 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);
    }
}

application.properties

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

build.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 中创建自定义指标?

提前致谢。

【问题讨论】:

  • 您只是发表声明,没有提出问题。这段代码目前做什么?为什么这是错误的或不完整的?您需要读者提供哪些具体答案?
  • 嗨,我已经更新了问题,我需要为每天的学生创建计数创建自定义指标。这样我就可以在 grafana 上绘制图表。
  • 您只需创建一个针对学生创建的计数器。它将计算所有的创造。然后在 Graphana 中,将其按time(1d) 分组。您不需要创建自定义端点,只需将计数器注册到千分尺。 actuator/prometheus 端点将自动与其他端点公开注册的计数器。
  • 嗨,怎么做,我是这个东西的新手。

标签: spring-boot grafana grafana-api


【解决方案1】:

由于您使用的是 Prometheus,因此会自动注册一个 prometheusMeterRegistry bean。

在您的Controller 类中,您需要自动装配prometheusMeterRegistry bean。以下将做:

@Autowired
MeterRegistry registry;

然后您可以向注册表注册一个计数器并使用它递增它

Counter counter = registry.counter(counterName);
counter.increment();

更多详情请见Micrometer docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2021-04-24
    • 2020-01-12
    • 1970-01-01
    • 2020-08-22
    • 2018-07-08
    • 2022-12-03
    相关资源
    最近更新 更多