001package io.prometheus.metrics.config; 002 003import java.util.HashMap; 004import java.util.Map; 005import javax.annotation.Nullable; 006 007/** 008 * The Prometheus Java client library can be configured at runtime (e.g. using a properties file). 009 * 010 * <p>This class represents the runtime configuration. 011 */ 012public class PrometheusProperties { 013 014 private static final PrometheusProperties instance = PrometheusPropertiesLoader.load(); 015 016 private final MetricsProperties defaultMetricsProperties; 017 private final Map<String, MetricsProperties> metricProperties = new HashMap<>(); 018 private final ExemplarsProperties exemplarProperties; 019 private final ExporterProperties exporterProperties; 020 private final ExporterFilterProperties exporterFilterProperties; 021 private final ExporterHttpServerProperties exporterHttpServerProperties; 022 private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties; 023 private final ExporterPushgatewayProperties exporterPushgatewayProperties; 024 025 /** 026 * Get the properties instance. When called for the first time, {@code get()} loads the properties 027 * from the following locations: 028 * 029 * <ul> 030 * <li>{@code prometheus.properties} file found in the classpath. 031 * <li>Properties file specified in the {@code PROMETHEUS_CONFIG} environment variable or the 032 * {@code prometheus.config} system property. 033 * <li>Individual properties from system properties. 034 * </ul> 035 */ 036 public static PrometheusProperties get() throws PrometheusPropertiesException { 037 return instance; 038 } 039 040 public static Builder builder() { 041 return new Builder(); 042 } 043 044 public PrometheusProperties( 045 MetricsProperties defaultMetricsProperties, 046 Map<String, MetricsProperties> metricProperties, 047 ExemplarsProperties exemplarProperties, 048 ExporterProperties exporterProperties, 049 ExporterFilterProperties exporterFilterProperties, 050 ExporterHttpServerProperties httpServerConfig, 051 ExporterPushgatewayProperties pushgatewayProperties, 052 ExporterOpenTelemetryProperties otelConfig) { 053 this.defaultMetricsProperties = defaultMetricsProperties; 054 this.metricProperties.putAll(metricProperties); 055 this.exemplarProperties = exemplarProperties; 056 this.exporterProperties = exporterProperties; 057 this.exporterFilterProperties = exporterFilterProperties; 058 this.exporterHttpServerProperties = httpServerConfig; 059 this.exporterPushgatewayProperties = pushgatewayProperties; 060 this.exporterOpenTelemetryProperties = otelConfig; 061 } 062 063 /** 064 * The default metric properties apply for metrics where {@link #getMetricProperties(String)} is 065 * {@code null}. 066 */ 067 public MetricsProperties getDefaultMetricProperties() { 068 return defaultMetricsProperties; 069 } 070 071 /** 072 * Properties specific for one metric. Should be merged with {@link 073 * #getDefaultMetricProperties()}. May return {@code null} if no metric-specific properties are 074 * configured for a metric name. 075 */ 076 @Nullable 077 public MetricsProperties getMetricProperties(String metricName) { 078 return metricProperties.get(metricName.replace(".", "_")); 079 } 080 081 public ExemplarsProperties getExemplarProperties() { 082 return exemplarProperties; 083 } 084 085 public ExporterProperties getExporterProperties() { 086 return exporterProperties; 087 } 088 089 public ExporterFilterProperties getExporterFilterProperties() { 090 return exporterFilterProperties; 091 } 092 093 public ExporterHttpServerProperties getExporterHttpServerProperties() { 094 return exporterHttpServerProperties; 095 } 096 097 public ExporterPushgatewayProperties getExporterPushgatewayProperties() { 098 return exporterPushgatewayProperties; 099 } 100 101 public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() { 102 return exporterOpenTelemetryProperties; 103 } 104 105 public static class Builder { 106 private MetricsProperties defaultMetricsProperties = MetricsProperties.builder().build(); 107 private Map<String, MetricsProperties> metricProperties = new HashMap<>(); 108 private ExemplarsProperties exemplarProperties = ExemplarsProperties.builder().build(); 109 private ExporterProperties exporterProperties = ExporterProperties.builder().build(); 110 private ExporterFilterProperties exporterFilterProperties = 111 ExporterFilterProperties.builder().build(); 112 private ExporterHttpServerProperties exporterHttpServerProperties = 113 ExporterHttpServerProperties.builder().build(); 114 private ExporterPushgatewayProperties pushgatewayProperties = 115 ExporterPushgatewayProperties.builder().build(); 116 private ExporterOpenTelemetryProperties otelConfig = 117 ExporterOpenTelemetryProperties.builder().build(); 118 119 private Builder() {} 120 121 public Builder defaultMetricsProperties(MetricsProperties defaultMetricsProperties) { 122 this.defaultMetricsProperties = defaultMetricsProperties; 123 return this; 124 } 125 126 public Builder metricProperties(Map<String, MetricsProperties> metricProperties) { 127 this.metricProperties = metricProperties; 128 return this; 129 } 130 131 /** Convenience for adding a single named MetricsProperties */ 132 public Builder putMetricProperty(String name, MetricsProperties props) { 133 this.metricProperties.put(name, props); 134 return this; 135 } 136 137 public Builder exemplarProperties(ExemplarsProperties exemplarProperties) { 138 this.exemplarProperties = exemplarProperties; 139 return this; 140 } 141 142 public Builder exporterProperties(ExporterProperties exporterProperties) { 143 this.exporterProperties = exporterProperties; 144 return this; 145 } 146 147 public Builder exporterFilterProperties(ExporterFilterProperties exporterFilterProperties) { 148 this.exporterFilterProperties = exporterFilterProperties; 149 return this; 150 } 151 152 public Builder exporterHttpServerProperties( 153 ExporterHttpServerProperties exporterHttpServerProperties) { 154 this.exporterHttpServerProperties = exporterHttpServerProperties; 155 return this; 156 } 157 158 public Builder pushgatewayProperties(ExporterPushgatewayProperties pushgatewayProperties) { 159 this.pushgatewayProperties = pushgatewayProperties; 160 return this; 161 } 162 163 public Builder exporterOpenTelemetryProperties( 164 ExporterOpenTelemetryProperties exporterOpenTelemetryProperties) { 165 this.otelConfig = exporterOpenTelemetryProperties; 166 return this; 167 } 168 169 public PrometheusProperties build() { 170 return new PrometheusProperties( 171 defaultMetricsProperties, 172 metricProperties, 173 exemplarProperties, 174 exporterProperties, 175 exporterFilterProperties, 176 exporterHttpServerProperties, 177 pushgatewayProperties, 178 otelConfig); 179 } 180 } 181}