All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.sdk.autoconfigure.MetricExporterConfiguration Maven / Gradle / Ivy

There is a newer version: 1.42.1
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.sdk.autoconfigure;

import io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConfigurableMetricReaderProvider;
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import java.io.Closeable;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.annotation.Nullable;

final class MetricExporterConfiguration {

  private static final Duration DEFAULT_EXPORT_INTERVAL = Duration.ofMinutes(1);
  private static final Map EXPORTER_ARTIFACT_ID_BY_NAME;
  private static final Map READER_ARTIFACT_ID_BY_NAME;

  static {
    EXPORTER_ARTIFACT_ID_BY_NAME = new HashMap<>();
    EXPORTER_ARTIFACT_ID_BY_NAME.put("logging", "opentelemetry-exporter-logging");
    EXPORTER_ARTIFACT_ID_BY_NAME.put("logging-otlp", "opentelemetry-exporter-logging-otlp");
    EXPORTER_ARTIFACT_ID_BY_NAME.put("otlp", "opentelemetry-exporter-otlp");

    READER_ARTIFACT_ID_BY_NAME = new HashMap<>();
    READER_ARTIFACT_ID_BY_NAME.put("prometheus", "opentelemetry-exporter-prometheus");
  }

  static MetricReader configureReader(
      String name,
      ConfigProperties config,
      SpiHelper spiHelper,
      BiFunction
          metricExporterCustomizer,
      List closeables) {
    NamedSpiManager spiExportersManager =
        metricExporterSpiManager(config, spiHelper);
    MetricExporter metricExporter = configureExporter(name, spiExportersManager);

    // If no metric exporter with name, try to load metric reader
    if (metricExporter == null) {
      NamedSpiManager spiMetricReadersManager =
          metricReadersSpiManager(config, spiHelper);
      MetricReader metricReader = configureMetricReader(name, spiMetricReadersManager);
      if (metricReader != null) {
        closeables.add(metricReader);
        return metricReader;
      }
      // No exporter or reader with the name
      throw new ConfigurationException("Unrecognized value for otel.metrics.exporter: " + name);
    }

    // Customize metric exporter and associate it with PeriodicMetricReader
    closeables.add(metricExporter);
    MetricExporter customizedMetricExporter =
        metricExporterCustomizer.apply(metricExporter, config);
    if (customizedMetricExporter != metricExporter) {
      closeables.add(customizedMetricExporter);
    }
    MetricReader reader =
        PeriodicMetricReader.builder(customizedMetricExporter)
            .setInterval(config.getDuration("otel.metric.export.interval", DEFAULT_EXPORT_INTERVAL))
            .build();
    closeables.add(reader);
    return reader;
  }

  // Visible for testing
  static NamedSpiManager metricReadersSpiManager(
      ConfigProperties config, SpiHelper spiHelper) {
    return spiHelper.loadConfigurable(
        ConfigurableMetricReaderProvider.class,
        ConfigurableMetricReaderProvider::getName,
        ConfigurableMetricReaderProvider::createMetricReader,
        config);
  }

  // Visible for testing.
  @Nullable
  static MetricReader configureMetricReader(
      String name, NamedSpiManager spiMetricReadersManager) {
    MetricReader metricReader = spiMetricReadersManager.getByName(name);
    if (metricReader == null) {
      String artifactId = READER_ARTIFACT_ID_BY_NAME.get(name);
      if (artifactId != null) {
        throw missingArtifactException(name, artifactId);
      }
      return null;
    }
    return metricReader;
  }

  // Visible for testing
  static NamedSpiManager metricExporterSpiManager(
      ConfigProperties config, SpiHelper spiHelper) {
    return spiHelper.loadConfigurable(
        ConfigurableMetricExporterProvider.class,
        ConfigurableMetricExporterProvider::getName,
        ConfigurableMetricExporterProvider::createExporter,
        config);
  }

  // Visible for testing.
  @Nullable
  static MetricExporter configureExporter(
      String name, NamedSpiManager spiExportersManager) {
    MetricExporter metricExporter = spiExportersManager.getByName(name);
    if (metricExporter == null) {
      String artifactId = EXPORTER_ARTIFACT_ID_BY_NAME.get(name);
      if (artifactId != null) {
        throw missingArtifactException(name, artifactId);
      }
      return null;
    }
    return metricExporter;
  }

  private static ConfigurationException missingArtifactException(
      String exporterName, String artifactId) {
    return new ConfigurationException(
        "otel.metrics.exporter set to \""
            + exporterName
            + "\" but "
            + artifactId
            + " not found on classpath. Make sure to add it as a dependency.");
  }

  private MetricExporterConfiguration() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy