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 PrometheusProperties(
040      MetricsProperties defaultMetricsProperties,
041      Map<String, MetricsProperties> metricProperties,
042      ExemplarsProperties exemplarProperties,
043      ExporterProperties exporterProperties,
044      ExporterFilterProperties exporterFilterProperties,
045      ExporterHttpServerProperties httpServerConfig,
046      ExporterPushgatewayProperties pushgatewayProperties,
047      ExporterOpenTelemetryProperties otelConfig) {
048    this.defaultMetricsProperties = defaultMetricsProperties;
049    this.metricProperties.putAll(metricProperties);
050    this.exemplarProperties = exemplarProperties;
051    this.exporterProperties = exporterProperties;
052    this.exporterFilterProperties = exporterFilterProperties;
053    this.exporterHttpServerProperties = httpServerConfig;
054    this.exporterPushgatewayProperties = pushgatewayProperties;
055    this.exporterOpenTelemetryProperties = otelConfig;
056  }
057
058  /**
059   * The default metric properties apply for metrics where {@link #getMetricProperties(String)} is
060   * {@code null}.
061   */
062  public MetricsProperties getDefaultMetricProperties() {
063    return defaultMetricsProperties;
064  }
065
066  /**
067   * Properties specific for one metric. Should be merged with {@link
068   * #getDefaultMetricProperties()}. May return {@code null} if no metric-specific properties are
069   * configured for a metric name.
070   */
071  public MetricsProperties getMetricProperties(String metricName) {
072    return metricProperties.get(metricName.replace(".", "_"));
073  }
074
075  public ExemplarsProperties getExemplarProperties() {
076    return exemplarProperties;
077  }
078
079  public ExporterProperties getExporterProperties() {
080    return exporterProperties;
081  }
082
083  public ExporterFilterProperties getExporterFilterProperties() {
084    return exporterFilterProperties;
085  }
086
087  public ExporterHttpServerProperties getExporterHttpServerProperties() {
088    return exporterHttpServerProperties;
089  }
090
091  public ExporterPushgatewayProperties getExporterPushgatewayProperties() {
092    return exporterPushgatewayProperties;
093  }
094
095  public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
096    return exporterOpenTelemetryProperties;
097  }
098}