2017-12-01 15 views
0

저는 간단한 Maven Spring-Boot Application (Java)을 가지고 있으며 Prometheus를 사용하여 메트릭 정보를 수집하고 있습니다. 내 pom 파일에 필요한 모든 Prometheus 종속성이 있으며 @EnablePrometheusEndpoint 주석을 @SpringBootApplication 클래스에 포함하고 prometheus.yml 파일 (아래 그림 참조)에 "metrics_path : '/ prometheus'"를 포함 시켰습니다. (localhost : 8080/prometheus에서) 메트릭에 액세스하려고하면 406 오류가 발생합니다. 내 내 보낸 측정 항목을 보려면 내 prometheus.yml 파일을 어떻게 수정해야합니까? 필요한 모든 프로 메테우스 종속성Spring-Boot 애플리케이션에서 Prometheus 메트릭을 수집하기 위해 prometheus.yml 파일을 어떻게 구성합니까?

@SpringBootApplication 
@RestController 
@EnablePrometheusEndpoint 
public class Example { 

    //Just a logger that keeps track of relevant information: 
    private static final Logger LOGGER = Logger.getLogger(Example.class.getName()); 

    //counter for counting how many times an endpoint has been hit 
    static final Counter myCounter = Counter.build()  
               .name("CounterName") //note: by convention, counters should have "_total" suffix 
               .help("Total requests recorded by a specific endpoint") 
               .labelNames("status") 
               .register(); 
    @RequestMapping("/hello") 
    String hello() { 

     myCounter.labels("customLabel1").inc(); //increment the number of requests by one 
     LOGGER.log(Level.INFO, "Number of times /hello has been hit: " + myCounter.labels("customLabel1").get()); 

     return "Hello world! This is an example response!"; 
    } 

    @RequestMapping("/homepage") 
    String homePage() { 

     myCounter.labels("customLabel2").inc(); //increment the number of requests by one 
     LOGGER.log(Level.INFO, "Number of times /homepage has been hit: " + myCounter.labels("customLabel2").get()); 

     return "this is the home page!!"; 
    } 


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

} 

pom.xml 파일 : 아래

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.example</groupId> 
    <artifactId>springboot</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.8.RELEASE</version> 
    </parent> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 


     <!-- Prometheus dependencies --> 
     <!-- The client --> 
     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient</artifactId> 
      <version>0.1.0</version> 
     </dependency> 

     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_spring_boot</artifactId> 
      <version>0.1.0</version> 
     </dependency> 

     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_servlet</artifactId> 
      <version>0.1.0</version> 
     </dependency> 
     <!-- Hotspot JVM metrics --> 
     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_hotspot</artifactId> 
      <version>0.1.0</version> 
     </dependency> 
     <!-- Exposition HTTPServer --> 
     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_httpserver</artifactId> 
      <version>0.1.0</version> 
     </dependency> 
     <!-- Pushgateway exposition --> 
     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_pushgateway</artifactId> 
      <version>0.1.0</version> 
     </dependency> 

     <!-- Spring Boot Actuator for exposing metrics --> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
      <version>1.5.8.RELEASE</version> 
     </dependency> 


    </dependencies> 


</project> 

그리고 내 프로 메테우스됩니다

아래는 내가 악기를 찾고 오전 (단순) 봄 부트 모든 코드는 .yml 파일. 내가 긁어 어떤 프로 메테우스에 지정된 경우 지난 몇 라인, 그리고 내가 내 문제이고이 의심주의 : 당신이 봄 부팅을 사용하는 것처럼

# my global config 
global: 
    scrape_interval:  15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. 
    evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. 
    # scrape_timeout is set to the global default (10s). 

    # Attach these labels to any time series or alerts when communicating with 
    # external systems (federation, remote storage, Alertmanager). 
    external_labels: 
     monitor: 'codelab-monitor' 

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. 
rule_files: 
    # - "first.rules" 
    # - "second.rules" 

# A scrape configuration containing exactly one endpoint to scrape: 
# Here it's Prometheus itself. 
scrape_configs: 
    # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. 
    - job_name: 'prometheus' 

    # metrics_path defaults to '/metrics' 
    # scheme defaults to 'http'. 
    scrape_interval: 5s 

    static_configs: 
     - targets: ['localhost:9090'] 

#The following lines are meant to monitor my spring boot app 
    - job_name: 'hello_world_spring_boot' 
    scrape_interval: 5s 

    metrics_path: '/prometheus' 

    static_configs: 
     - targets: ['localhost:8080'] 

답변

0

, 다음 프로 메테우스 받는다는 종속성을 사용할 수 있습니다

<dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_spring_boot</artifactId> 
      <version>0.0.17</version> 
     </dependency> 

     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_hotspot</artifactId> 
      <version>0.0.17</version> 
     </dependency> 

     <dependency> 
      <groupId>io.prometheus</groupId> 
      <artifactId>simpleclient_servlet</artifactId> 
      <version>0.0.17</version> 
     </dependency> 

엔드 포인트/프로 메테우스에 충돌하는 경우, 당신은 t을 받아야합니다, 지금은 전파 측정

@Configuration 
public class MonitoringConfig { 

    @Bean 
    SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) { 

     SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics); 
     springBootMetricsCollector.register(); 

     return springBootMetricsCollector; 
    } 

    @Bean 
    ServletRegistrationBean servletRegistrationBean() { 
     DefaultExports.initialize(); 
     return new ServletRegistrationBean(new MetricsServlet(), "/prometheus"); 
    } 
} 

수 있도록 다음 클래스를 @EnablePrometheusEndpoint를 제거하고 생성 그는 메트릭스