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 exporterHttpServerProperties; 021 private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties; 022 023 /** 024 * Get the properties instance. When called for the first time, {@code get()} loads the properties from the following locations: 025 * <ul> 026 * <li>{@code prometheus.properties} file found in the classpath.</li> 027 * <li>Properties file specified in the {@code PROMETHEUS_CONFIG} environment variable or the {@code prometheus.config} system property.</li> 028 * <li>Individual properties from system properties.</li> 029 * </ul> 030 */ 031 public static PrometheusProperties get() throws PrometheusPropertiesException { 032 return instance; 033 } 034 035 public PrometheusProperties( 036 MetricsProperties defaultMetricsProperties, 037 Map<String, MetricsProperties> metricProperties, 038 ExemplarsProperties exemplarProperties, 039 ExporterProperties exporterProperties, 040 ExporterFilterProperties exporterFilterProperties, 041 ExporterHttpServerProperties httpServerConfig, 042 ExporterOpenTelemetryProperties otelConfig) { 043 this.defaultMetricsProperties = defaultMetricsProperties; 044 this.metricProperties.putAll(metricProperties); 045 this.exemplarProperties = exemplarProperties; 046 this.exporterProperties = exporterProperties; 047 this.exporterFilterProperties = exporterFilterProperties; 048 this.exporterHttpServerProperties = httpServerConfig; 049 this.exporterOpenTelemetryProperties = otelConfig; 050 } 051 052 /** 053 * The default metric properties apply for metrics where {@link #getMetricProperties(String)} is {@code null}. 054 */ 055 public MetricsProperties getDefaultMetricProperties() { 056 return defaultMetricsProperties; 057 } 058 059 /** 060 * Properties specific for one metric. Should be merged with {@link #getDefaultMetricProperties()}. 061 * May return {@code null} if no metric-specific properties are configured for a metric name. 062 */ 063 public MetricsProperties getMetricProperties(String metricName) { 064 return metricProperties.get(metricName.replace(".", "_")); 065 } 066 067 public ExemplarsProperties getExemplarProperties() { 068 return exemplarProperties; 069 } 070 071 public ExporterProperties getExporterProperties() { 072 return exporterProperties; 073 } 074 075 public ExporterFilterProperties getExporterFilterProperties() { 076 return exporterFilterProperties; 077 } 078 079 public ExporterHttpServerProperties getExporterHttpServerProperties() { 080 return exporterHttpServerProperties; 081 } 082 083 public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() { 084 return exporterOpenTelemetryProperties; 085 } 086}