Skip to content

Commit

Permalink
Upgrade client to 1.2.1, prometheusProperties, merge config props
Browse files Browse the repository at this point in the history
  • Loading branch information
shakuzen committed Apr 5, 2024
1 parent 70089c1 commit 63a3d2a
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand All @@ -42,6 +45,23 @@ public class PrometheusProperties {
*/
private boolean descriptions = true;

/**
* Configuration options for using Prometheus Pushgateway, allowing metrics to be
* pushed when they cannot be scraped.
*/
private final Pushgateway pushgateway = new Pushgateway();

/**
* Histogram type for backing DistributionSummary and Timer.
*/
@Deprecated(since = "3.3.0")
private HistogramFlavor histogramFlavor = HistogramFlavor.Prometheus;

/**
* Additional properties to pass to the Prometheus client.
*/
private final Map<String, String> prometheusProperties = new HashMap<>();

/**
* Step size (i.e. reporting frequency) to use.
*/
Expand All @@ -55,6 +75,14 @@ public void setDescriptions(boolean descriptions) {
this.descriptions = descriptions;
}

public HistogramFlavor getHistogramFlavor() {
return this.histogramFlavor;
}

public void setHistogramFlavor(HistogramFlavor histogramFlavor) {
this.histogramFlavor = histogramFlavor;
}

public Duration getStep() {
return this.step;
}
Expand All @@ -71,4 +99,132 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Pushgateway getPushgateway() {
return this.pushgateway;
}

public Map<String, String> getPrometheusProperties() {
return this.prometheusProperties;
}

/**
* Configuration options for push-based interaction with Prometheus.
*/
public static class Pushgateway {

/**
* Enable publishing over a Prometheus Pushgateway.
*/
private Boolean enabled = false;

/**
* Base URL for the Pushgateway.
*/
private String baseUrl = "http://localhost:9091";

/**
* Login user of the Prometheus Pushgateway.
*/
private String username;

/**
* Login password of the Prometheus Pushgateway.
*/
private String password;

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

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

/**
* Grouping key for the pushed metrics.
*/
private Map<String, String> groupingKey = new HashMap<>();

/**
* Operation that should be performed on shutdown.
*/
private ShutdownOperation shutdownOperation = ShutdownOperation.NONE;

public Boolean getEnabled() {
return this.enabled;
}

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

public String getBaseUrl() {
return this.baseUrl;
}

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

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public Duration getPushRate() {
return this.pushRate;
}

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

public String getJob() {
return this.job;
}

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

public Map<String, String> getGroupingKey() {
return this.groupingKey;
}

public void setGroupingKey(Map<String, String> groupingKey) {
this.groupingKey = groupingKey;
}

public ShutdownOperation getShutdownOperation() {
return this.shutdownOperation;
}

public void setShutdownOperation(ShutdownOperation shutdownOperation) {
this.shutdownOperation = shutdownOperation;
}

}

public enum HistogramFlavor {

Prometheus, VictoriaMetrics;

HistogramFlavor() {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;

import java.time.Duration;
import java.util.Map;
import java.util.Properties;

import io.micrometer.prometheusmetrics.PrometheusConfig;

Expand Down Expand Up @@ -55,4 +57,20 @@ public Duration step() {
return get(PrometheusProperties::getStep, PrometheusConfig.super::step);
}

@Override
public Properties prometheusProperties() {
return get(this::fromPropertiesMap, PrometheusConfig.super::prometheusProperties);
}

private Properties fromPropertiesMap(PrometheusProperties prometheusProperties) {
Map<String, String> map = prometheusProperties.getPrometheusProperties();
if (map.isEmpty()) {
return null;
}
Properties properties = PrometheusConfig.super.prometheusProperties();
properties = (properties != null) ? properties : new Properties();
properties.putAll(map);
return properties;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(PrometheusMeterRegistry.class)
@ConditionalOnEnabledMetricsExport("prometheus")
@EnableConfigurationProperties(PrometheusSimpleclientProperties.class)
@EnableConfigurationProperties(PrometheusProperties.class)
public class PrometheusSimpleclientMetricsExportAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public PrometheusConfig simpleclientPrometheusConfig(PrometheusSimpleclientProperties prometheusProperties) {
public PrometheusConfig simpleclientPrometheusConfig(PrometheusProperties prometheusProperties) {
return new PrometheusSimpleclientPropertiesConfigAdapter(prometheusProperties);
}

Expand Down Expand Up @@ -134,9 +134,8 @@ public static class PrometheusPushGatewayConfiguration {
@Bean
@ConditionalOnMissingBean
public PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
PrometheusSimpleclientProperties prometheusProperties, Environment environment)
throws MalformedURLException {
PrometheusSimpleclientProperties.Pushgateway properties = prometheusProperties.getPushgateway();
PrometheusProperties prometheusProperties, Environment environment) throws MalformedURLException {
PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway();
Duration pushRate = properties.getPushRate();
String job = getJob(properties, environment);
Map<String, String> groupingKey = properties.getGroupingKey();
Expand All @@ -154,7 +153,7 @@ private PushGateway initializePushGateway(String url) throws MalformedURLExcepti
return new PushGateway(new URL(url));
}

private String getJob(PrometheusSimpleclientProperties.Pushgateway properties, Environment environment) {
private String getJob(PrometheusProperties.Pushgateway properties, Environment environment) {
String job = properties.getJob();
job = (job != null) ? job : environment.getProperty("spring.application.name");
return (job != null) ? job : FALLBACK_JOB;
Expand Down
Loading

0 comments on commit 63a3d2a

Please sign in to comment.