|
|
@ -16,6 +16,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
|
|
|
|
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import io.micrometer.core.instrument.Clock;
|
|
|
|
import io.micrometer.core.instrument.Clock;
|
|
|
|
import io.micrometer.prometheus.PrometheusConfig;
|
|
|
|
import io.micrometer.prometheus.PrometheusConfig;
|
|
|
|
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
|
|
|
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
|
|
@ -24,6 +29,7 @@ import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
|
|
|
|
import io.prometheus.client.exporter.PushGateway;
|
|
|
|
import io.prometheus.client.exporter.PushGateway;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
|
|
@ -44,21 +50,17 @@ import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.core.log.LogMessage;
|
|
|
|
import org.springframework.core.log.LogMessage;
|
|
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to Prometheus.
|
|
|
|
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to Prometheus.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|
|
|
|
* @since 2.0.0
|
|
|
|
* @author Jon Schneider
|
|
|
|
* @author Jon Schneider
|
|
|
|
* @author David J. M. Karlsen
|
|
|
|
* @author David J. M. Karlsen
|
|
|
|
* @since 2.0.0
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@AutoConfigureBefore({CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class})
|
|
|
|
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
|
|
|
|
@AutoConfigureAfter(MetricsAutoConfiguration.class)
|
|
|
|
@AutoConfigureAfter(MetricsAutoConfiguration.class)
|
|
|
|
@ConditionalOnBean(Clock.class)
|
|
|
|
@ConditionalOnBean(Clock.class)
|
|
|
|
@ConditionalOnClass(PrometheusMeterRegistry.class)
|
|
|
|
@ConditionalOnClass(PrometheusMeterRegistry.class)
|
|
|
@ -75,7 +77,7 @@ public class PrometheusMetricsExportAutoConfiguration {
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig prometheusConfig,
|
|
|
|
public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig prometheusConfig,
|
|
|
|
CollectorRegistry collectorRegistry, Clock clock) {
|
|
|
|
CollectorRegistry collectorRegistry, Clock clock) {
|
|
|
|
return new PrometheusMeterRegistry(prometheusConfig, collectorRegistry, clock);
|
|
|
|
return new PrometheusMeterRegistry(prometheusConfig, collectorRegistry, clock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -118,30 +120,30 @@ public class PrometheusMetricsExportAutoConfiguration {
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
public PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
|
|
|
|
public PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
|
|
|
|
PrometheusProperties prometheusProperties, Environment environment) {
|
|
|
|
PrometheusProperties prometheusProperties, Environment environment) {
|
|
|
|
PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway();
|
|
|
|
PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway();
|
|
|
|
Duration pushRate = properties.getPushRate();
|
|
|
|
Duration pushRate = properties.getPushRate();
|
|
|
|
String job = getJob(properties, environment);
|
|
|
|
String job = getJob(properties, environment);
|
|
|
|
Map<String, String> groupingKey = properties.getGroupingKey();
|
|
|
|
Map<String, String> groupingKey = properties.getGroupingKey();
|
|
|
|
ShutdownOperation shutdownOperation = properties.getShutdownOperation();
|
|
|
|
ShutdownOperation shutdownOperation = properties.getShutdownOperation();
|
|
|
|
return new PrometheusPushGatewayManager(getPushGateway(properties.getBaseUrl()), collectorRegistry,
|
|
|
|
PushGateway pushGateway = initializePushGateway(properties.getBaseUrl());
|
|
|
|
pushRate, job, groupingKey, shutdownOperation);
|
|
|
|
if (StringUtils.hasText(properties.getUsername())) {
|
|
|
|
|
|
|
|
pushGateway.setConnectionFactory(
|
|
|
|
|
|
|
|
new BasicAuthHttpConnectionFactory(properties.getUsername(), properties.getPassword()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PrometheusPushGatewayManager(pushGateway, collectorRegistry, pushRate, job, groupingKey,
|
|
|
|
|
|
|
|
shutdownOperation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private PushGateway getPushGateway(String url) {
|
|
|
|
private PushGateway initializePushGateway(String url) {
|
|
|
|
PushGateway pushGateway = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
pushGateway = new PushGateway(new URL(url));
|
|
|
|
return new PushGateway(new URL(url));
|
|
|
|
} catch (MalformedURLException ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (MalformedURLException ex) {
|
|
|
|
logger.warn(LogMessage
|
|
|
|
logger.warn(LogMessage
|
|
|
|
.format("Invalid PushGateway base url '%s': update your configuration to a valid URL", url));
|
|
|
|
.format("Invalid PushGateway base url '%s': update your configuration to a valid URL", url));
|
|
|
|
pushGateway = new PushGateway(url);
|
|
|
|
return new PushGateway(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway();
|
|
|
|
|
|
|
|
if (properties.getAuthEnabled()) {
|
|
|
|
|
|
|
|
pushgateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(properties.getAuthusername(), properties.getAuthpassword()));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pushGateway;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getJob(PrometheusProperties.Pushgateway properties, Environment environment) {
|
|
|
|
private String getJob(PrometheusProperties.Pushgateway properties, Environment environment) {
|
|
|
|