当前位置: 首页 > 知识库问答 >
问题:

如何从java应用程序以Prometheus格式导出指标(由DropWizard收集)?

爱繁
2023-03-14

我的目标是 - 创建 Spring 启动应用程序,使用 DropWizard 收集指标,并为 Prometheus 公开endpoint以使用应用程序指标:

我的代码:

@SpringBootApplication
@EnableMetrics(proxyTargetClass = true)
public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }

}
package com.example.demo;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.annotation.Timed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicLong;

@RestController
public class HelloController {
    private AtomicLong atomicLong = new AtomicLong();
    private Counter counter;

    @Autowired
    private MetricRegistry metricRegistry;

    @PostConstruct
    public void init() {
        counter = metricRegistry.counter("counter");
    }

    @GetMapping("/hello")
    @Timed(name = "my-index")
    public String index() {
        counter.inc();

        return "Greetings from Spring Boot!. count=" + atomicLong.incrementAndGet();
    }

}
package com.example.demo;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.FileDescriptorRatioGauge;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.codahale.metrics.servlets.AdminServlet;
import com.codahale.metrics.servlets.CpuProfileServlet;
import com.codahale.metrics.servlets.MetricsServlet;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
import io.prometheus.client.dropwizard.DropwizardExports;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class Config /*extends MetricsConfigurerAdapter*/ {
    //@Override
            //public void configureReporters(MetricRegistry metricRegistry) {
        //    // registerReporter allows the MetricsConfigurerAdapter to
        //    // shut down the reporter when the Spring context is closed
        //   // registerReporter(ConsoleReporter
        //   //         .forRegistry(metricRegistry)
        //   //         .build())
        //   //         .start(1, TimeUnit.MINUTES);


        //    new DropwizardExports(metricRegistry).register();
        //}

   @Bean
   public DropwizardExports dropwizardExports(MetricRegistry metricRegistry){
       DropwizardExports dropwizardExports = new DropwizardExports(metricRegistry);
       dropwizardExports.register();
       return dropwizardExports;
   }

    @Bean
    public MetricRegistry metricRegistry() {
        MetricRegistry metricRegistry = new MetricRegistry();
        metricRegistry.registerAll(new GarbageCollectorMetricSet());
        metricRegistry.registerAll(new MemoryUsageGaugeSet());
        metricRegistry.registerAll(new ThreadStatesGaugeSet());
        return metricRegistry;
    }

    @Bean
    public ConsoleReporter consoleReporter(MetricRegistry metricRegistry) {
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
        reporter.start(5, TimeUnit.SECONDS);
        reporter.report();
        return reporter;
    }

    @Bean
    public ServletRegistrationBean<MetricsServlet> registerMetricsServlet(MetricRegistry metricRegistry) {
        return new ServletRegistrationBean<>(new MetricsServlet(metricRegistry), "/metrics/*");
    }

    @Bean
    public ServletRegistrationBean<CpuProfileServlet> registerCpuServlet() {
        return new ServletRegistrationBean<>(new CpuProfileServlet(), "/cpu/*");
    }
}

build.gradle

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

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    // Minimum required for metrics.
    implementation ('com.ryantenney.metrics:metrics-spring:3.1.3') {
        exclude group: 'com.codahale.metrics'
        exclude group: 'org.springframework'
    }
    implementation 'io.dropwizard.metrics:metrics-core:4.2.9'
    implementation 'io.dropwizard.metrics:metrics-annotation:4.2.9'
    implementation 'io.dropwizard.metrics:metrics-servlets:4.2.9'

    implementation 'io.prometheus:simpleclient_dropwizard:0.15.0'
    implementation 'io.prometheus:simpleclient_servlet:0.15.0'
    implementation 'io.dropwizard:dropwizard-core:2.1.0'

    implementation 'com.ryantenney.metrics:metrics-spring:3.1.3'
    implementation 'io.prometheus:simpleclient_common:0.16.0'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

我访问本地主机:8080/指标并收到以下响应:

{
  "version": "4.0.0",
  "gauges": {
    "G1-Old-Generation.count": {
      "value": 0
    },
    "G1-Old-Generation.time": {
      "value": 0
    },
    "G1-Young-Generation.count": {
      "value": 7
    },
    "G1-Young-Generation.time": {
      "value": 31
    },
    "blocked.count": {
      "value": 0
    },
    "count": {
      "value": 26
    },
    "daemon.count": {
      "value": 22
    },
    "deadlock.count": {
      "value": 0
    },
    "deadlocks": {
      "value": []
    },
    "heap.committed": {
      "value": 301989888
    },
    "heap.init": {
      "value": 532676608
    },
    "heap.max": {
      "value": 8518631424
    },
    "heap.usage": {
      "value": 0.008041180864688155
    },
    "heap.used": {
      "value": 68499856
    },
    "new.count": {
      "value": 0
    },
    "non-heap.committed": {
      "value": 51707904
    },
    "non-heap.init": {
      "value": 2555904
    },
    "non-heap.max": {
      "value": -1
    },
    "non-heap.usage": {
      "value": -5.0738536E7
    },
    "non-heap.used": {
      "value": 50738536
    },
    "peak.count": {
      "value": 32
    },
    "pools.CodeCache.committed": {
      "value": 10551296
    },
    "pools.CodeCache.init": {
      "value": 2555904
    },
    "pools.CodeCache.max": {
      "value": 50331648
    },
    "pools.CodeCache.usage": {
      "value": 0.2039642333984375
    },
    "pools.CodeCache.used": {
      "value": 10265856
    },
    "pools.Compressed-Class-Space.committed": {
      "value": 5177344
    },
    "pools.Compressed-Class-Space.init": {
      "value": 0
    },
    "pools.Compressed-Class-Space.max": {
      "value": 1073741824
    },
    "pools.Compressed-Class-Space.usage": {
      "value": 0.004625104367733002
    },
    "pools.Compressed-Class-Space.used": {
      "value": 4966168
    },
    "pools.G1-Eden-Space.committed": {
      "value": 188743680
    },
    "pools.G1-Eden-Space.init": {
      "value": 29360128
    },
    "pools.G1-Eden-Space.max": {
      "value": -1
    },
    "pools.G1-Eden-Space.usage": {
      "value": 0.26666666666666666
    },
    "pools.G1-Eden-Space.used": {
      "value": 50331648
    },
    "pools.G1-Eden-Space.used-after-gc": {
      "value": 0
    },
    "pools.G1-Old-Gen.committed": {
      "value": 109051904
    },
    "pools.G1-Old-Gen.init": {
      "value": 503316480
    },
    "pools.G1-Old-Gen.max": {
      "value": 8518631424
    },
    "pools.G1-Old-Gen.usage": {
      "value": 0.0017806278080379123
    },
    "pools.G1-Old-Gen.used": {
      "value": 15168512
    },
    "pools.G1-Old-Gen.used-after-gc": {
      "value": 15168512
    },
    "pools.G1-Survivor-Space.committed": {
      "value": 4194304
    },
    "pools.G1-Survivor-Space.init": {
      "value": 0
    },
    "pools.G1-Survivor-Space.max": {
      "value": -1
    },
    "pools.G1-Survivor-Space.usage": {
      "value": 0.7151832580566406
    },
    "pools.G1-Survivor-Space.used": {
      "value": 2999696
    },
    "pools.G1-Survivor-Space.used-after-gc": {
      "value": 2999696
    },
    "pools.Metaspace.committed": {
      "value": 35979264
    },
    "pools.Metaspace.init": {
      "value": 0
    },
    "pools.Metaspace.max": {
      "value": -1
    },
    "pools.Metaspace.usage": {
      "value": 0.9868604316086066
    },
    "pools.Metaspace.used": {
      "value": 35506512
    },
    "runnable.count": {
      "value": 10
    },
    "terminated.count": {
      "value": 0
    },
    "timed_waiting.count": {
      "value": 5
    },
    "total.committed": {
      "value": 353697792
    },
    "total.init": {
      "value": 535232512
    },
    "total.max": {
      "value": 8518631423
    },
    "total.used": {
      "value": 119238392
    },
    "total_started.count": {
      "value": 47
    },
    "waiting.count": {
      "value": 11
    }
  },
  "counters": {
    "counter": {
      "count": 9
    }
  },
  "histograms": {},
  "meters": {},
  "timers": {}
}

显然,此输出不适用于普罗米修斯(至少所有点都应替换为“_”)

如何使输出格式为普罗米修斯做好准备?

根据文档,我已经了解类io.prometheus.client.dropwizardDropwizardExports负责生成为Prometheus准备的格式的度量,但我不明白如何。

共有3个答案

杜浩壤
2023-03-14

最后我找到了根本原因:

com.codahale.metrics.servlets.MetricsServlet

应替换为

io.prometheus.client.exporter.MetricsServlet
公冶伟
2023-03-14

我没有看到显着差异,但它是工作示例:

public class JavaDropwizard {
  // Create registry for Dropwizard metrics.
  static final MetricRegistry metrics = new MetricRegistry();
  // Create a Dropwizard counter.
  static final Counter counter = metrics.counter("my.example.counter.total");

  public static void main( String[] args ) throws Exception {
      // Increment the counter.
      counter.inc();

      // Hook the Dropwizard registry into the Prometheus registry
      // via the DropwizardExports collector.
      CollectorRegistry.defaultRegistry.register(new DropwizardExports(metrics));


      // Expose Prometheus metrics.
      Server server = new Server(1234);
      ServletContextHandler context = new ServletContextHandler();
      context.setContextPath("/");
      server.setHandler(context);
      context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
      // Add metrics about CPU, JVM memory etc.
      DefaultExports.initialize();
      // Start the webserver.
      server.start();
      server.join();
  }
}

绒球.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.robustperception.java_examples</groupId>
  <artifactId>java_dropwizard</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <name>java_dropwizard</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>io.prometheus</groupId>
      <artifactId>simpleclient</artifactId>
      <version>0.15.0</version>
    </dependency>
    <dependency>
      <groupId>io.prometheus</groupId>
      <artifactId>simpleclient_hotspot</artifactId>
      <version>0.15.0</version>
    </dependency>
    <dependency>
      <groupId>io.prometheus</groupId>
      <artifactId>simpleclient_servlet</artifactId>
      <version>0.15.0</version>
    </dependency>
    <dependency>
      <groupId>io.prometheus</groupId>
      <artifactId>simpleclient_dropwizard</artifactId>
      <version>0.15.0</version>
    </dependency>
    <dependency>
        <groupId>io.dropwizard.metrics</groupId>
        <artifactId>metrics-core</artifactId>
        <version>4.2.9</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>8.1.7.v20120910</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
      </plugin>
      <!-- Build a full jar with dependencies --> 
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
        <manifest>
          <mainClass>io.robustperception.java_examples.JavaDropwizard</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
微生曾琪
2023-03-14

我已经实现了将指标导出到普罗米修斯的不同方法

1.1) 推送网关的自定义实现

我编写了将输出生成到 StringBuilder 中的代码,并且只遵循文档 https://prometheus.io/docs/instrumenting/exposition_formats/

最后,使用您选择的java HttpClient将该字符串发布到Pushgateway。

1.2) 普罗米修斯要抓取的自定义页面

我写了一个动态页面(servlet,jsp,...),它生成具有纯/文本内容类型的输出,并且只遵循文档 https://prometheus.io/docs/instrumenting/exposition_formats/

配置 Prometheus 以抓取该动态页面。

我使用了 https://github.com/prometheus/client_java,这是Java的官方客户端库。查看自述文件中有关导出的章节,它非常好,涵盖了推送指标和抓取。

 类似资料:
  • 我在k8s上部署了普罗米修斯节点导出吊舱。它工作得很好。 但是当我试图通过调用自定义Go应用程序中的节点导出器度量API来获取系统度量时 结果格式如下 那些长文本很难解析,我希望得到JSON格式的结果来轻松解析它们。 https://github.com/prometheus/node_exporter/issues/1062 我检查了Prometheus节点导出器GitHub的问题,有人推荐了p

  • 所以我的问题是。我的Spring Boot应用程序使用Hibernate/JPA和ehCache进行二级缓存。我想用Dropwizard/Coda-Hale指标来检测ehCache,但我不确定如何去做。如果我手动创建缓存实例,那就简单了。您只需使用如下所示的装饰器。但是因为是Spring/Hibernate,我无法控制缓存。你知道我该怎么设置这个吗?

  • 我正在尝试自定义一个网格,然后使用gltfExporter from Threejs导出它,但是它仍然会在附加了所有变形/形状关键点的情况下导出,我想在最终导出网格时删除它们。 克隆场景/网格不起作用。 网格导出时附加所有变形/形状键,或者不导出

  • 我的Spring Boot应用程序只是有一个计数器指标。我只是不知道如何将这些信息发送给普罗米修斯。我正在使用Maven(构建工具)和Spring Boot(Java)。

  • 问题内容: 用代码退出Java应用程序的最佳方法是什么? 问题答案: 您可以用于此目的。 根据oracle的Java 8文档: 终止当前正在运行的Java虚拟机。参数用作状态码;按照惯例, 非零状态代码表示异常终止 。 此方法在类Runtime中调用exit方法。此方法永远不会正常返回。 该调用实际上等效于该调用:

  • 我有一个简单的Maven Spring-Boot应用程序(Java),并使用Prometheus从其中收集度量信息。我在pom文件中有所有必要的Prometheus依赖项,并且我在@SpringBootApplication类中包含了@EnablePrometheusEndpoint注释,但是当我运行应用程序并尝试访问localhost:8080/Prometheus(我认为这是Prometheu

  • 在我的dropwizard rest应用程序中,我的指标配置是这样的,(hello-world.yml) 这是一个SLF4J记者,我把它放在了我的YAML配置文件中。当我想获取这个值并在我的应用程序文件中使用它时。我看到的唯一选择是将此作为< code >地图阅读 此问题与graphite服务器配置的Dropwizard Yaml相同。但是这个问题还是没有答案,所以我创造了一个新问题来试试运气。

  • 阅读HikariCP维基上有关如何启用拖放向导指标的说明,它说只需在或中配置一个实例。 问题是,在Spring Boot,所有的配置都是由自动配置处理的,所以我根本不用手动配置HikariCP池。 有关于如何做的说明吗?我是否必须通过定义自己的bean并在文件中设置所有设置来完全覆盖自动配置?