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

如何使用Spring Boot2.0通过PushGateway将度量导出到Prometheus

谢骏奇
2023-03-14

共有1个答案

夹谷英杰
2023-03-14

不幸的是,Prometheus Pushgateway自动配置没有进入Spring-Boot2。不确定是否接受包含micromter-spring-legacy设置的PR。

同时,您可以尝试设置自己的@configuration类,它包括从这里开始的所有内容。

下面是一个快速拼接的解决方案

@Configuration
@EnableConfigurationProperties(PushgatewayProperties.class)
public class PushgatewayConfiguration {

    @ConfigurationProperties(prefix = "management.metrics.export.prometheus.pushgateway")
    public static class PushgatewayProperties {
        /**
        * Enable publishing via a Prometheus Pushgateway.
        */
        private Boolean enabled = false;

        /**
        * Required host:port or ip:port of the Pushgateway.
        */
        private String baseUrl = "localhost:9091";

        /**
        * Required identifier for this application instance.
        */
        private String job;

        /**
        * Frequency with which to push metrics to Pushgateway.
        */
        private Duration pushRate = Duration.ofMinutes(1);

        /**
        * Push metrics right before shut-down. Mostly useful for batch jobs.
        */
        private boolean pushOnShutdown = true;

        /**
        * Delete metrics from Pushgateway when application is shut-down
        */
        private boolean deleteOnShutdown = true;

        /**
        * Used to group metrics in pushgateway. A common example is setting
        */
        private Map<String, String> groupingKeys = new HashMap<>();

        public Boolean getEnabled() {
            return enabled;
        }

        public void setEnabled(Boolean enabled) {
            this.enabled = enabled;
        }

        public String getBaseUrl() {
            return baseUrl;
        }

        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }

        public String getJob() {
            return job;
        }

        public void setJob(String job) {
            this.job = job;
        }

        public Duration getPushRate() {
            return pushRate;
        }

        public void setPushRate(Duration pushRate) {
            this.pushRate = pushRate;
        }

        public boolean isPushOnShutdown() {
            return pushOnShutdown;
        }

        public void setPushOnShutdown(boolean pushOnShutdown) {
            this.pushOnShutdown = pushOnShutdown;
        }

        public boolean isDeleteOnShutdown() {
            return deleteOnShutdown;
        }

        public void setDeleteOnShutdown(boolean deleteOnShutdown) {
            this.deleteOnShutdown = deleteOnShutdown;
        }

        public Map<String, String> getGroupingKeys() {
            return groupingKeys;
        }

        public void setGroupingKeys(Map<String, String> groupingKeys) {
            this.groupingKeys = groupingKeys;
        }
    }

    static class PrometheusPushGatewayEnabledCondition extends AllNestedConditions {
        public PrometheusPushGatewayEnabledCondition() {
            super(ConfigurationPhase.PARSE_CONFIGURATION);
        }

        @ConditionalOnProperty(value = "management.metrics.export.prometheus.enabled", matchIfMissing = true)
        static class PrometheusMeterRegistryEnabled {
            //
        }

        @ConditionalOnProperty("management.metrics.export.prometheus.pushgateway.enabled")
        static class PushGatewayEnabled {
            //
        }
    }

    /**
    * Configuration for
    * <a href="https://github.com/prometheus/pushgateway">Prometheus
    * Pushgateway</a>.
    *
    * @author David J. M. Karlsen
    */
    @Configuration
    @ConditionalOnClass(PushGateway.class)
    @Conditional(PrometheusPushGatewayEnabledCondition.class)
    @Incubating(since = "1.0.0")
    public class PrometheusPushGatewayConfiguration {
        private final Logger logger = LoggerFactory.getLogger(PrometheusPushGatewayConfiguration.class);
        private final CollectorRegistry collectorRegistry;
        private final PushgatewayProperties pushgatewayProperties;
        private final PushGateway pushGateway;
        private final Environment environment;
        private final ScheduledExecutorService executorService;

        PrometheusPushGatewayConfiguration(CollectorRegistry collectorRegistry,
                PushgatewayProperties pushgatewayProperties, Environment environment) {
            this.collectorRegistry = collectorRegistry;
            this.pushgatewayProperties = pushgatewayProperties;
            this.pushGateway = new PushGateway(pushgatewayProperties.getBaseUrl());
            this.environment = environment;
            this.executorService = Executors.newSingleThreadScheduledExecutor((r) -> {
                final Thread thread = new Thread(r);
                thread.setDaemon(true);
                thread.setName("micrometer-pushgateway");
                return thread;
            });
            executorService.scheduleAtFixedRate(this::push, 0, pushgatewayProperties.getPushRate().toMillis(),
                    TimeUnit.MILLISECONDS);
        }

        void push() {
            try {
                pushGateway.pushAdd(collectorRegistry, job(), pushgatewayProperties.getGroupingKeys());
            } catch (UnknownHostException e) {
                logger.error("Unable to locate host '" + pushgatewayProperties.getBaseUrl()
                        + "'. No longer attempting metrics publication to this host");
                executorService.shutdown();
            } catch (Throwable t) {
                logger.error("Unable to push metrics to Prometheus Pushgateway", t);
            }
        }

        @PreDestroy
        void shutdown() {
            executorService.shutdown();
            if (pushgatewayProperties.isPushOnShutdown()) {
                push();
            }
            if (pushgatewayProperties.isDeleteOnShutdown()) {
                try {
                    pushGateway.delete(job(), pushgatewayProperties.getGroupingKeys());
                } catch (Throwable t) {
                    logger.error("Unable to delete metrics from Prometheus Pushgateway", t);
                }
            }
        }

        private String job() {
            String job = pushgatewayProperties.getJob();
            if (job == null) {
                job = environment.getProperty("spring.application.name");
            }
            if (job == null) {
                // There's a history of Prometheus spring integration defaulting the job name to
                // "spring" from when
                // Prometheus integration didn't exist in Spring itself.
                job = "spring";
            }
            return job;
        }
    }

}
 类似资料:
  • 我正在寻找一种将Spring Boot 2中的Spring Boot指标导出到文件的方法。 在Spring Boot 1.5.10中,我们使用了一个自定义MetricsExporter类,该类实现MetricWriter并重写set和increment方法,以使用记录器写入度量。我们使用了一个日志文件,因为我们有一种不同的机制来稍后处理日志文件以进行度量分析。 我们还使用了MetricsConfi

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

  • 问题内容: 我想通过php将MYSQL数据导出到Excel / CSV。这样我以后可以使用我的数据库,或者有人可以使用和理解它。 问题答案: 我想这就是你要找的 您可以通过检查以下地址来创建自己的文件:http : //www.programmingfacts.com/export-mysql-data-into- excelcsv-via-php/ 我不能在这里添加工作代码,这是错误的= / 但

  • 想实现一个html的标签导出到pdf,html标签里有图片,图片也要导出到pdf,且可以设置图片,文字的样式,需要导出A4纸格式的,后面要打印这个A4值成册的,有老哥们做过类似的功能吗?

  • 我遵循这个答案(将电子表格的行转换为单独的XML文件),但是我收到一个错误,告诉我没有定义对象。我很抱歉,因为我是VBA新手。 我的表格遵循代码中显示的顺序,例如。 第1列{例如A}=文件名字符串 第677列{e.g.ZA}=XML文件第一行的字符串 第683列{e.g.ZG}=视频文件的标题{此列和以下所有列已经有了相应的标记(例如。 产生错误的代码是: 突出显示的错误是“doc.getElem

  • 问题内容: 我想将JPanel中的图像导出到矢量图形文件中,以便可以以高于屏幕的分辨率对其进行编辑和打印。本质上,我希望使用目标图形调用它的功能,该目标图形将绘图命令保存到矢量图形文件中。 有什么好的简单方法呢?推荐哪些库?哪种矢量格式最好,为什么? 问题答案: 看看Java EPS Graphics2D包。 许多Java程序都使用Graphics2D在屏幕上绘制内容,虽然很容易将输出另存为png