Spring boot 如何创建Spring boot 2 Grafana服装度量终点

Spring boot 如何创建Spring boot 2 Grafana服装度量终点,spring-boot,grafana,grafana-api,Spring Boot,Grafana,Grafana Api,我正在尝试学习Grafana,并使用SpringBoot2、Prometheus和Grafana创建应用程序。 我需要为每天的学生创建计数创建自定义指标 import com.demo.mockito.entity.StudentEntity; import com.demo.mockito.service.StudentService; import io.micrometer.core.annotation.Timed; import io.micrometer.core.instrumen

我正在尝试学习Grafana,并使用SpringBoot2、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 metrix of student.getRollNo()
        return ResponseEntity.ok(student);
    }
}
格雷德尔先生

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);
    }
}
package com.demo.mockito;
导入io.micrometer.core.aop.TimedAspect;
进口io.千分尺.岩心.仪器.计量器具;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.annotation.Bean;
导入javax.sql.DataSource;
@SpringBoot应用程序
公共类应用程序{
公共静态void main(字符串[]args){
run(SpringMockitoApplication.class,args);
}
//自定义应用程序名称
@豆子
MeterRegistryCustomizer配置器(
@值(“${spring.application.name}”)字符串applicationName){
return(registry)->registry.config().commonTags(“应用程序”,applicationName);
}
@豆子
TimedAspect TimedAspect(计量注册表){
返回新的TimedAspect(注册表);
}
}
我需要为每天的学生创建计数创建自定义的执行器端点度量,以便我可以使用Grafana绘制其图形。卷号是一个自动生成的字段,所以它会给我一个学生总数的计数

如何在spring boot中创建自定义指标?
提前感谢。

由于您正在与普罗米修斯合作,因此会自动注册一个
普罗米修斯流量计注册

控制器
类中,需要自动连接
prometheusMeterRegistry
bean。以下步骤可以:

@Autowired
MeterRegistry registry;
然后,您可以向注册表注册一个计数器,并使用

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

有关更多详细信息,请参阅。

您只做了陈述,没有提出任何问题。这段代码目前做什么?为什么这是错误的或不完整的?你需要读者给出什么具体的答案?嗨,我已经更新了这个问题,我需要为我每天的学生创建计数创建自定义指标。这样我就可以在grafana上画出这个图,你只需要创建一个计数器就可以了。它将计算所有的创造。然后在Graphana中,按
时间(1d)
分组。您不需要创建自定义端点,只需将计数器注册到测微计即可
actuator/prometheus
endpoint将自动与其他人一起公开已注册的计数器。嗨,怎么做,我是这方面的新手。
Counter counter = registry.counter(counterName);
counter.increment();