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