001package io.prometheus.metrics.config;
002
003import java.util.HashMap;
004import java.util.Map;
005
006/**
007 * The Prometheus Java client library can be configured at runtime (e.g. using a properties file).
008 * <p>
009 * This class represents the runtime configuration.
010 */
011public class PrometheusProperties {
012
013    private static final PrometheusProperties instance = PrometheusPropertiesLoader.load();
014
015    private final MetricsProperties defaultMetricsProperties;
016    private final Map<String, MetricsProperties> metricProperties = new HashMap<>();
017    private final ExemplarsProperties exemplarProperties;
018    private final ExporterProperties exporterProperties;
019    private final ExporterFilterProperties exporterFilterProperties;
020    private final ExporterHttpServerProperties httpServerConfig;
021
022    /**
023     * Get the properties instance. When called for the first time, {@code get()} loads the properties from the following locations:
024     * <ul>
025     *     <li>{@code prometheus.properties} file found in the classpath.</li>
026     *     <li>Properties file specified in the {@code PROMETHEUS_CONFIG} environment variable or the {@code prometheus.config} system property.</li>
027     *     <li>Individual properties from system properties.</li>
028     * </ul>
029     */
030    public static PrometheusProperties get() throws PrometheusPropertiesException {
031        return instance;
032    }
033
034    public PrometheusProperties(
035            MetricsProperties defaultMetricsProperties,
036            Map<String, MetricsProperties> metricProperties,
037            ExemplarsProperties exemplarProperties,
038            ExporterProperties exporterProperties,
039            ExporterFilterProperties exporterFilterProperties,
040            ExporterHttpServerProperties httpServerConfig) {
041        this.defaultMetricsProperties = defaultMetricsProperties;
042        this.metricProperties.putAll(metricProperties);
043        this.exemplarProperties = exemplarProperties;
044        this.exporterProperties = exporterProperties;
045        this.exporterFilterProperties = exporterFilterProperties;
046        this.httpServerConfig = httpServerConfig;
047    }
048
049    /**
050     * The default metric properties apply for metrics where {@link #getMetricProperties(String)} is {@code null}.
051     */
052    public MetricsProperties getDefaultMetricProperties() {
053        return defaultMetricsProperties;
054    }
055
056    /**
057     * Properties specific for one metric. Should be merged with {@link #getDefaultMetricProperties()}.
058     * May return {@code null} if no metric-specific properties are configured for a metric name.
059     */
060    public MetricsProperties getMetricProperties(String metricName) {
061        return metricProperties.get(metricName.replace(".", "_"));
062    }
063
064    public ExemplarsProperties getExemplarProperties() {
065        return exemplarProperties;
066    }
067
068    public ExporterProperties getExporterProperties() {
069        return exporterProperties;
070    }
071
072    public ExporterFilterProperties getExporterFilterProperties() {
073        return exporterFilterProperties;
074    }
075
076    public ExporterHttpServerProperties getExporterHttpServerProperties() {
077        return httpServerConfig;
078    }
079}