springboot输出metrics到prometheus

搭建push gateway

version: '2'
services:
  prometheus:
    build: .
    ports:
      - 9090:9090
    volumes:
      - /tmp/prometheus-data:/prometheus-data
    links:
      - pushgateway
  pushgateway:
    image: prom/pushgateway
    ports:
      - 9999:9091

配置从pushgateway采取数据

# 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'.
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'push-metrics'
    static_configs:
      - targets: ['192.168.99.100:9999']

maven

        <!-- The client -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient</artifactId>
            <version>0.0.21</version>
        </dependency>
        <!-- Hotspot JVM metrics -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.0.21</version>
        </dependency>
        <!-- Exposition servlet -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_servlet</artifactId>
            <version>0.0.21</version>
        </dependency>
        <!-- Pushgateway exposition -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.0.21</version>
        </dependency>
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_dropwizard</artifactId>
            <version>0.0.21</version>
        </dependency>

java config

@Component
public class PrometheusConfig {

    @Autowired
    MetricRegistry dropwizardRegistry;

    @Value("${spring.application.name}")
    String applicationName;

    @Value("${prometheus.pushgateway.host}")
    String pushHost;

    @Value("${prometheus.pushgateway.intervalInMillis:10000}")
    long intervalInMillis;

    private final CollectorRegistry prometheusRegistry = new CollectorRegistry();

    @PostConstruct
    public void initialize() {
        DropwizardExports prometheus = new DropwizardExports(dropwizardRegistry);
        prometheus.register(prometheusRegistry);

        PushGateway prometheusPush = new PushGateway(pushHost);

        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
            try {
                prometheus.collect();
                prometheusPush.push(prometheusRegistry, applicationName);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }, 5000, intervalInMillis, TimeUnit.MILLISECONDS);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于生产级别的Java应用程序的框架。而Prometheus是一个开源的监控和警报系统,用于记录和查询应用程序的时间序列数据。在Spring Boot中集成Prometheus可以方便地监控和度量应用程序的性能指标。 要在Spring Boot中集成Prometheus并自定义Prometheus的指标,可以按照以下步骤进行操作: 1. 添加依赖:在Spring Boot项目的pom.xml文件中添加Prometheus相关的依赖。例如: ```xml <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> ``` 2. 配置Prometheus:在Spring Boot项目的配置文件(如application.properties或application.yml)中添加Prometheus相关的配置。例如: ```yaml management: endpoints: web: exposure: include: prometheus ``` 这样配置后,Spring Boot会自动将Prometheus的监控端点暴露出来。 3. 自定义指标:在代码中使用Micrometer库来定义和记录自定义的指标。Micrometer是一个度量库,可以与Prometheus集成。例如,可以使用`Counter`来记录计数器指标,使用`Gauge`来记录度量指标等。以下是一个示例: ```java import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CustomMetrics { private final Counter customCounter; @Autowired public CustomMetrics(MeterRegistry meterRegistry) { customCounter = meterRegistry.counter("custom_counter"); } public void incrementCustomCounter() { customCounter.increment(); } } ``` 在上述示例中,我们定义了一个名为`custom_counter`的计数器指标,并通过`MeterRegistry`将其注册到Micrometer中。 4. 访问指标:启动Spring Boot应用程序后,可以通过访问`/actuator/prometheus`端点来获取Prometheus格式的指标数据。例如,可以使用浏览器或curl命令访问`http://localhost:8080/actuator/prometheus`来获取指标数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值