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

com.undefinedlabs.scope.settings.props.YamlScopeConfigFileProperties Maven / Gradle / Ivy

package com.undefinedlabs.scope.settings.props;

import com.undefinedlabs.scope.settings.ScopeConfigFileProperties;
import com.undefinedlabs.scope.settings.ScopeSettings;
import com.undefinedlabs.scope.utils.FileUtils;
import com.undefinedlabs.scope.utils.props.SystemProps;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;

public enum YamlScopeConfigFileProperties implements ScopeConfigFileProperties {
  INSTANCE;

  static final Map YAML_SCOPE_CONFIG_PROPERTIES;

  static {
    final String externalConfigPath = System.getenv(ScopeSettings.SCOPE_CONFIG_FILE);
    final File externalFile =
        StringUtils.isNotEmpty(externalConfigPath) ? Paths.get(externalConfigPath).toFile() : null;
    final boolean yamlFileIsOK =
        externalFile != null && externalFile.exists() && externalFile.isFile();
    if (StringUtils.isNotEmpty(externalConfigPath) && !yamlFileIsOK) {
      System.err.println(
          "Yaml Config filepath '" + externalConfigPath + "' does not exist or it is not a file.");
    }

    YAML_SCOPE_CONFIG_PROPERTIES =
        populateYamlConfigProperties(
            yamlFileIsOK ? externalFile : findFile(SystemProps.USER_DIR, "scope", "yaml", "yml"));
  }

  protected static Map populateYamlConfigProperties(final File yamlConfigFile) {
    if (yamlConfigFile == null) {
      return Collections.unmodifiableMap(new HashMap());
    }

    try {
      final Map yamlProperties = new HashMap<>();
      final Yaml yaml = new Yaml();

      final Map yamlFileProps = yaml.load(new FileInputStream(yamlConfigFile));
      final Map yamlLoggerProps = getMap(yamlFileProps, "logger");
      final Map yamlCodePathProps = getMap(yamlFileProps, "code_path");
      final Map yamlInstrumentationProps = getMap(yamlFileProps, "instrumentation");
      final Map yamlInstrumentationDb = getMap(yamlInstrumentationProps, "db");
      final Map yamlInstrumentationHttp = getMap(yamlInstrumentationProps, "http");

      final Map yamlTracerProps = getMap(yamlFileProps, "tracer");
      final Map yamlTracerDispatcher = getMap(yamlTracerProps, "dispatcher");
      final Map yamlTracerDispatcherSpans = getMap(yamlTracerDispatcher, "spans");
      final Map yamlTracerDispatcherEvents = getMap(yamlTracerDispatcher, "events");

      final Map yamlRunnerProps = getMap(yamlFileProps, "runner");

      yamlProperties.put(ScopeSettings.SCOPE_SERVICE, getString(yamlFileProps, "service"));
      yamlProperties.put(ScopeSettings.SCOPE_COMMIT_SHA, getString(yamlFileProps, "commit_sha"));
      yamlProperties.put(ScopeSettings.SCOPE_REPOSITORY, getString(yamlFileProps, "repository"));
      yamlProperties.put(ScopeSettings.SCOPE_SOURCE_ROOT, getString(yamlFileProps, "source_root"));
      yamlProperties.put(ScopeSettings.SCOPE_BRANCH, getString(yamlFileProps, "branch"));
      yamlProperties.put(ScopeSettings.SCOPE_LOGGER_LEVEL, getString(yamlLoggerProps, "level"));
      yamlProperties.put(ScopeSettings.SCOPE_LOGGER_ROOT, getString(yamlLoggerProps, "root"));
      yamlProperties.put(
          ScopeSettings.SCOPE_CODE_PATH_ENABLED, getBoolean(yamlCodePathProps, "enabled"));
      yamlProperties.put(
          ScopeSettings.SCOPE_CODE_PATH_BASE_PACKAGES,
          getString(yamlCodePathProps, "base_packages"));
      yamlProperties.put(
          ScopeSettings.SCOPE_METADATA, getMapOfValuesAsString(yamlFileProps, "metadata"));
      yamlProperties.put(
          ScopeSettings.SCOPE_CONFIGURATION,
          getStringListSeparatedByCommas(yamlFileProps, "configuration"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TESTING_MODE, getBoolean(yamlFileProps, "testing_mode"));

      yamlProperties.put(
          ScopeSettings.SCOPE_INSTRUMENTATION_ENABLED,
          getBoolean(yamlInstrumentationProps, "enabled"));
      yamlProperties.put(
          ScopeSettings.SCOPE_INSTRUMENTATION_DB_STATEMENT_VALUES,
          getBoolean(yamlInstrumentationDb, "statement_values"));
      yamlProperties.put(
          ScopeSettings.SCOPE_INSTRUMENTATION_DB_STACKTRACE,
          getBoolean(yamlInstrumentationDb, "stacktrace"));
      yamlProperties.put(
          ScopeSettings.SCOPE_INSTRUMENTATION_HTTP_PAYLOADS,
          getBoolean(yamlInstrumentationHttp, "payloads"));
      yamlProperties.put(
          ScopeSettings.SCOPE_INSTRUMENTATION_HTTP_HEADERS,
          getStringListSeparatedByCommas(yamlInstrumentationHttp, "headers"));
      yamlProperties.put(
          ScopeSettings.SCOPE_INSTRUMENTATION_HTTP_STACKTRACE,
          getBoolean(yamlInstrumentationHttp, "stacktrace"));

      yamlProperties.put(ScopeSettings.SCOPE_TRACER_GLOBAL, getBoolean(yamlTracerProps, "global"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TRACER_DISPATCHER_HEALTHCHECK_FREQUENCY,
          getLong(yamlTracerDispatcher, "healthcheck_frecuency"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TRACER_DISPATCHER_HEALTHCHECK_FREQUENCY_IN_TESTMODE,
          getLong(yamlTracerDispatcher, "healthcheck_frecuency_in_testmode"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TRACER_DISPATCHER_SPANS_MAX_BUFFER_SIZE,
          getInt(yamlTracerDispatcherSpans, "max_buffer_size"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TRACER_DISPATCHER_EVENTS_MAX_BUFFER_SIZE,
          getInt(yamlTracerDispatcherEvents, "max_buffer_size"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TRACER_DISPATCHER_CONCURRENCY_LEVEL,
          getLong(yamlTracerDispatcher, "concurrency_level"));
      yamlProperties.put(
          ScopeSettings.SCOPE_TRACER_DISPATCHER_CLOSE_TIMEOUT,
          getLong(yamlTracerDispatcher, "close_timeout"));

      yamlProperties.put(
          ScopeSettings.SCOPE_RUNNER_ENABLED, getBoolean(yamlRunnerProps, "enabled"));
      yamlProperties.put(
          ScopeSettings.SCOPE_RUNNER_INCLUDE_BRANCHES,
          getStringListSeparatedByCommas(yamlRunnerProps, "include_branches"));
      yamlProperties.put(
          ScopeSettings.SCOPE_RUNNER_EXCLUDE_BRANCHES,
          getStringListSeparatedByCommas(yamlRunnerProps, "exclude_branches"));

      return Collections.unmodifiableMap(yamlProperties);
    } catch (final Throwable e) {
      return Collections.unmodifiableMap(new HashMap());
    }
  }

  protected static File findFile(
      final String userDirPath, final String filenameToSearch, final String... extensions) {
    for (final String extension : extensions) {
      final File yamlConfigFile =
          FileUtils.searchBackwardsByFilename(userDirPath, filenameToSearch + "." + extension);
      if (yamlConfigFile != null) {
        return yamlConfigFile;
      }
    }
    return null;
  }

  private static String getMapOfValuesAsString(
      final Map parentMap, final String key) {
    if (parentMap == null) {
      return null;
    }

    final Map map = (Map) parentMap.get(key);
    if (map == null) {
      return null;
    }

    final List values = new ArrayList<>();
    for (final Map.Entry mapEntry : map.entrySet()) {
      values.add(mapEntry.getKey() + "=" + mapEntry.getValue());
    }

    return StringUtils.join(values, ",");
  }

  private static String getStringListSeparatedByCommas(
      final Map parentMap, final String key) {
    if (parentMap == null) {
      return null;
    }

    return StringUtils.join((List) parentMap.get(key), ",");
  }

  private static Map getMap(final Map parentMap, final String key) {
    if (parentMap == null) {
      return null;
    }

    return (Map) parentMap.get(key);
  }

  private static String getString(final Map parentMap, final String key) {
    if (parentMap == null) {
      return null;
    }

    return (String) parentMap.get(key);
  }

  private static Boolean getBoolean(final Map parentMap, final String key) {
    if (parentMap == null) {
      return null;
    }

    return (Boolean) parentMap.get(key);
  }

  private static Long getLong(final Map parentMap, final String key) {
    if (parentMap == null
        || parentMap.get(key) == null
        || !(parentMap.get(key) instanceof Number)) {
      return null;
    }

    return ((Number) parentMap.get(key)).longValue();
  }

  private static Integer getInt(final Map parentMap, final String key) {
    if (parentMap == null
        || parentMap.get(key) == null
        || !(parentMap.get(key) instanceof Number)) {
      return null;
    }

    return ((Number) parentMap.get(key)).intValue();
  }

  @Override
  public Object getProperty(final String key) {
    return YAML_SCOPE_CONFIG_PROPERTIES.get(key);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy