我将Spring Boot版本从1.5.x升级到2.0.1,但遇到了新的度量标准问题。
要使用千分尺在Spring boot 2.0+中,我必须移除micrometer-spring-legacy的<dependency/> 。 不幸的是, management.metrics.export.prometheus.pushgateway所有配置消失了。 那么,如何使用spring boot 2.0将度量标准导出到pushgateway? 非常感谢!
I upgraded Spring Boot version from 1.5.x to 2.0.1, but struggled with an issue of new Metrics.
To use micrometer In Spring boot 2.0+, I must remove the <dependency/> of micrometer-spring-legacy. Unfortunately, all of the config of management.metrics.export.prometheus.pushgateway disappeared. So how can I export metrics to pushgateway using spring boot 2.0? Many thanks!
最满意答案
不幸的是,普罗米修斯Pushgateway自动配置还没有将其应用到Spring-Boot 2.不确定是否接受包含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; } } }Unfortunately the Prometheus Pushgateway auto-configuration hasn't made it into Spring-Boot 2. Unsure if a PR which incorporates the micromter-spring-legacy setup would be accepted.
In the meantime you could try to setup your own @Configuration class which includes everything starting here.
Here's a quickly stitched together solution:
@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; } } }更多推荐
发布评论